public BlockTransferResponsed CheckBlockMessage(BlockTransferRequest blockMessage) { bool isError = true; using (var fs = new FileStream(FileNameTools.GetDownloadingFullPath(blockMessage.FileName), FileMode.Open, FileAccess.ReadWrite)) { try { byte[] buffer = new byte[blockMessage.BlockSize]; fs.Position = blockMessage.SeekOffset; fs.Read(buffer, 0, buffer.Length); string md5 = Md5.GetMd5WithBytes(buffer); if (md5 == blockMessage.BlockMd5) { isError = false; } } catch { // ignored } } return(new BlockTransferResponsed() { IsError = isError }); }
public override FileTransferResponsed Handle(ContextRequest request, FileWriteHandleContext context) { var r = request.FileRequest; //Console.WriteLine("StateFileLengthEqual"); var fs = request.WorkingStream; //bug : filestream appeared that has been closed string md5 = fs.CanRead ? Md5.GetMd5WithFileStream(fs, fs.Position) : Md5.GetMd5WithFilePath(request.WorkingPath); var progress = request.ProgressDic[r.FileMd5]; FileTransferResponsed responsed = new FileTransferResponsed(request.FileRequest) { IsSendingOver = true, FileMd5CheckResult = md5 == request.FileRequest.FileMd5 //文件长度相等,则视为已传输完毕 }; if (responsed.FileMd5CheckResult) { fs.Close(); var path = FileNameTools.GetDownloadedFullPath(request.WorkingPath); File.Move(request.WorkingPath, path);//modify filename progress.ProgressValue = progress.MaxValue; progress.StateMsg = "校验成功!"; try { request?.ReceiveProgressHandler?.OnReceiveEnd(r.FileMd5, true); //PubSubEvents.Singleton.Publish(new FileReceiveProgressCompleteEvent() { IsChecked = true,FileName = r.FileName }); } catch { // ignored } } else { request?.ReceiveProgressHandler?.OnReceiveEnd(r.FileMd5, false); progress.StateMsg = "校验失败!正在重新检查,请耐心等待。"; //PubSubEvents.Singleton.GetEvent<FileReceiveProgressCompleteEvent>().Publish(new FileReceiveProgressCompleteEvent() { IsChecked = false, FileName = r.FileName }); context.State = new StateFileNormalTransfer();//等待单个Block文件写入 } return(responsed); }
public FileTransferResponsed WriteFile(FileTransferRequest transferData) { var id = transferData.RequestId; if (!_contextDic.ContainsKey(transferData.RequestId))//若同一个服务同时上传多个文件,则创建多个维护状态模式的Context,通过RequestID识别 { _contextDic.Add(id, new FileWriteHandleContext(new StateFileFirstUpdate())); ContextRequest contextRequest = new ContextRequest(this._receiveProgressHandler) { WorkingPath = FileNameTools.GetDownloadingFullPath(transferData.FileName) }; _contextRequestDic.Add(id, contextRequest); } _contextRequestDic[id].FileRequest = transferData; #if DEBUG //Console.WriteLine($"Data ID={id}"); #endif var responsed = _contextDic[id].Request(_contextRequestDic[id]); transferData.BlockData = null;//销毁缓存 return(responsed); }