public override void Download() { if (Downloading) { throw new Exception("Can't download, since the previous download is not complete."); } DoneSize = 0; Success = false; IsCancelled = false; Error = null; Downloading = true; SliceFileName = null; SliceList = null; bigfile = null; try { SliceFileName = "download-" + from.md5 + ".slice"; SliceFileName = Path.Combine(Path.Combine(WorkFolder, pcs.getUID()), SliceFileName); StateFileNameDecideEventArgs args = new StateFileNameDecideEventArgs() { SliceFileName = SliceFileName }; fireStateFileNameDecide(args); SliceFileName = args.SliceFileName; CreateOrRestoreSliceList(); // 创建或还原分片列表 CreateDirectory(to); //创建目录 CreateLocalFile(); // 如果需要则创建本地文件 bigfile = new BigFileHelper(to, from.size); //映射文件到内存 foreach (Slice slice in SliceList) { if (slice.status != SliceStatus.Successed) { slice.status = SliceStatus.Pending; //重新下载未成功的分片 slice.doneSize = 0; } else { DoneSize += slice.doneSize; } } DownloadSliceList(); // 启动线程来下载分片 Wait(); // 等待所有线程退出 CheckResult(); // 检查下载结果 } catch (Exception ex) { Success = false; IsCancelled = false; Error = ex; } if (bigfile != null) { bigfile.Dispose(); bigfile = null; } if (Success) { SliceHelper.DeleteSliceFile(SliceFileName); } Downloading = false; fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error)); }
public override void Upload() { if (Uploading) { throw new Exception("Can't upload, since the previous upload is not complete."); } DoneSize = 0; Success = false; IsCancelled = false; Error = null; Uploading = true; SliceFileName = null; SliceList = null; fromFileInfo = null; try { BaiduPCS pcs = this.pcs.clone(); string key; if (string.IsNullOrEmpty(FileMD5)) { string validate_md5, validate2_md5; fromFileInfo = new FileInfo(from); if (!pcs.md5(from, 0, MIN_SLICE_SIZE, out validate_md5)) { throw new Exception("Can't calculate md5 for " + from + "."); } if (!pcs.md5(from, fromFileInfo.Length - MIN_SLICE_SIZE, MIN_SLICE_SIZE, out validate2_md5)) { throw new Exception("Can't calculate md5 for " + from + "."); } key = from + " => " + to + ", size=" + fromFileInfo.Length.ToString() + ", validate1_md5=" + validate_md5 + ", validate2_md5=" + validate2_md5; key = Utils.md5(key.ToLower()); } else { key = FileMD5; } SliceFileName = "upload-" + key + ".slice"; SliceFileName = Path.Combine(WorkFolder, pcs.getUID(), SliceFileName); StateFileNameDecideEventArgs args = new StateFileNameDecideEventArgs() { SliceFileName = SliceFileName }; fireStateFileNameDecide(args); SliceFileName = args.SliceFileName; CreateOrRestoreSliceList(); // 创建或还原分片列表 foreach (Slice slice in SliceList) { if (slice.status != SliceStatus.Successed) { slice.status = SliceStatus.Pending; //重新上传未成功的分片 slice.doneSize = 0; } else { DoneSize += slice.doneSize; } } UploadSliceList(); // 启动线程来下载分片 Wait(); // 等待所有线程退出 List <string> md5list = new List <string>(); if (CheckResult(md5list)) // 检查下载结果 { Result = pcs.create_superfile(to, md5list.ToArray(), IsOverWrite); if (!Result.IsEmpty) { Success = true; IsCancelled = false; } else { Success = false; IsCancelled = false; Error = new Exception(pcs.getError()); } } else { Success = false; } } catch (Exception ex) { Success = false; IsCancelled = false; Error = ex; } if (Success) { SliceHelper.DeleteSliceFile(SliceFileName); } Uploading = false; fireCompleted(new CompletedEventArgs(Success, IsCancelled, Error)); }