public static FileChecksumInfo GetFileCheckSum(Stream fileStream, int checkSize = 1024 * 1024) { FileChecksumInfo data = new FileChecksumInfo() { ReadBufferCount = checkSize }; List <CheckSumItemInfo> items = new List <CheckSumItemInfo>(); while (fileStream.Position != fileStream.Length) { CheckSumItemInfo checkSum = new CheckSumItemInfo(); checkSum.StartPosition = fileStream.Position; var bytes = GetBytesPerBuffer(fileStream, checkSize); checkSum.EndPosition = fileStream.Position == fileStream.Length ? fileStream.Length : checkSum.StartPosition + checkSize; checkSum.Hash = GetMD5(bytes); items.Add(checkSum); ProgressAction?.Invoke(fileStream.Position, fileStream.Length); } data.CheckSums = items; return(data); }
//public static void SaveToFile(FileCheckSumData data, string fileName) //{ // //SerializeStream.SaveSerializeStream(fileName, data); //} public static List <CheckSumItemInfo> GetErrorsFromTwoCheckSum(FileChecksumInfo downloadData, FileChecksumInfo trueData) { List <CheckSumItemInfo> items = new List <CheckSumItemInfo>(); if (trueData.CheckSums.Count != downloadData.CheckSums.Count) { throw new Exception("Not true"); } for (int i = 0; i < trueData.CheckSums.Count; i++) { if (trueData.CheckSums[i].Hash != downloadData.CheckSums[i].Hash) { if (items.Count > 0 && trueData.CheckSums[i].StartPosition == items.Last().EndPosition) { items.Last().EndPosition = trueData.CheckSums[i].EndPosition; } else { items.Add(trueData.CheckSums[i]); } } } return(items); }
public async Task <StreamIdentifierFileUploadResult> StartUpload(FileInfo <TId> fileInfo, Stream streamForRead, long startPosition, long length, Action <long> wrotePositionAction = null, bool trowException = false) { try { string folderPath = GetFolderPath(fileInfo.Id); if (!CrossDirectoryInfo.Current.Exists(folderPath)) { CrossDirectoryInfo.Current.CreateDirectory(folderPath); } string filePath = Path.Combine(folderPath, "file"); string dataPath = Path.Combine(folderPath, "data"); SaveFileInfoToFile(fileInfo, dataPath, true); using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { fileStream.Seek(startPosition, SeekOrigin.Begin); byte[] readBytes = new byte[1024 * 512]; long writed = 0; while (true) { int readCount = 0; try { if (readBytes.Length > length - writed) { readBytes = new byte[length - writed]; } readCount = await streamForRead.ReadAsync(readBytes, 0, readBytes.Length); } catch (Exception ex) { Console.WriteLine("read exception : " + ex.ToString()); break; } if (readCount <= 0) { break; } fileStream.Write(readBytes, 0, readCount); writed += readCount; wrotePositionAction?.Invoke(fileStream.Position); if (writed == length) { break; } } if (fileStream.Length > fileInfo.FileSize) { fileStream.SetLength(fileInfo.FileSize); } if (fileStream.Length == fileInfo.FileSize) { fileStream.Seek(0, SeekOrigin.Begin); if (fileInfo.FileChecksumInfo != null) { FileChecksumInfo checkSum = FileChecksumMaker.GetFileCheckSum(fileStream); if (checkSum != null) { List <CheckSumItemInfo> errors = FileChecksumMaker.GetErrorsFromTwoCheckSum(checkSum, fileInfo.FileChecksumInfo); if (errors.Count == 0) { fileInfo.IsComplete = true; if (!IgnoreLastUpdateDateTime) { fileInfo.LastUpdateDateTime = DateTime.Now; } SaveFileInfoToFile(fileInfo, dataPath, false); return(StreamIdentifierFileUploadResult.Success); } else { return(StreamIdentifierFileUploadResult.FileCheckSumIsWrong); } } else { fileInfo.IsComplete = true; if (!IgnoreLastUpdateDateTime) { fileInfo.LastUpdateDateTime = DateTime.Now; } SaveFileInfoToFile(fileInfo, dataPath, false); return(StreamIdentifierFileUploadResult.Success); } } else { fileInfo.IsComplete = true; if (!IgnoreLastUpdateDateTime) { fileInfo.LastUpdateDateTime = DateTime.Now; } SaveFileInfoToFile(fileInfo, dataPath, false); return(StreamIdentifierFileUploadResult.Success); } } else { return(StreamIdentifierFileUploadResult.FileSizeIsWrong); } } } catch (Exception ex) { if (trowException) { throw; } return(StreamIdentifierFileUploadResult.Exception); } finally { } }