public static void StartDownload(IClient c, string remotePath) { try { string downloadLocation = string.Empty; using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.FileName = Path.GetFileName(remotePath); if (sfd.ShowDialog() != DialogResult.OK) { return; } downloadLocation = sfd.FileName; } string id = Guid.NewGuid().ToString("n"); while (DownloadHandler.ContainsKey(id)) { id = Guid.NewGuid().ToString("n"); } FileStream fs = new FileStream(downloadLocation, FileMode.Create, FileAccess.Write); DownloadingFileInfo dfi = new DownloadingFileInfo(); dfi.DownloadLocation = downloadLocation; dfi.Stream = fs; DownloadHandler.Add(id, dfi); c.Send((byte)NetworkCommand.FileManager, (byte)FileManagerCommand.StartDownload, id, remotePath); } catch { MessageBox.Show("Failed to start download"); } }
public async Task <ApiResponse <DownloadingFileInfo> > GetFileByCid(string cid) { using (var scope = GlobalServices.Container.BeginLifetimeScope()) { var repository = scope.Resolve <ICloudSpeedRepository>(); var fileCid = await repository.GetFileCidsByCid(cid); var downloadingFileInfo = new DownloadingFileInfo(); if (fileCid != null) { var path = _uploadSetting.GetStoragePath(fileCid.Id); var fileName = await GetFileName(fileCid.Id); var mimeType = fileName.GetMimeType(); var fileDeal = await repository.GetFileDealByCid(cid); var uploadLog = await repository.GetUploadLogByDataKey(fileCid.Id); var lsInfo = new LocalStroeInfo() { MimeType = mimeType, FileName = fileName, FileSize = new FileInfo(path).ToFileSize(), Date = fileCid.Created, Publisher = "ming", Miner = fileDeal != null ? (fileDeal.Miner + "-" + fileDeal.Status) : string.Empty, LogId = uploadLog.Id }; downloadingFileInfo.LocalStroeInfo = lsInfo; if (fileDeal != null && (fileDeal.Status == FileDealStatus.Success || fileDeal.Status == FileDealStatus.Processing)) { var queryOffers = await _lotusClient.ClientFindData(new ClientFindDataRequest { Root = new Cid() { Value = cid } }); if (queryOffers.Success) { var orderInfos = new List <RetrievalOrderInfo>(); foreach (var item in queryOffers.Result) { if (orderInfos.Any(o => o.Miner == item.Miner)) { continue; } var orderInfo = new RetrievalOrderInfo() { Miner = item.Miner, MinerPeerId = item.MinerPeer.ID, OfferMinPrice = item.MinPrice, OfferSize = ((long)item.Size).ToFileSize(), Err = item.Err }; orderInfos.Add(orderInfo); } downloadingFileInfo.RetrievalOrderInfos = orderInfos.ToArray(); } else { _logger.LogWarning(queryOffers.Error?.Message); } } } return(ApiResponse.Ok(downloadingFileInfo)); } }