123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787 |
- using System.Collections.Concurrent;
- using WingServerCommon.Log;
- using System.Net;
- using System.Net.Http.Headers;
- using System.Security.Cryptography;
- using System.Text;
- using WingServerCommon.Config;
- using WingServerCommon.Config.Parameters;
- namespace WingServerCommon.Interfaces.FileTransfer
- {
- /// <summary>
- /// 文件传输服务
- /// </summary>
- public class FileTransferService
- {
- private readonly HttpClient _httpClient;
- private readonly UploadFile _uploadFile;
- private string _folderBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileTransferRecord");
- private int _autoSliceSizeForUpload = 0;
- private long _manualDivisionForUpload = 0;
-
- /// <summary>
- /// Init service
- /// </summary>
- public FileTransferService()
- {
- _httpClient = new HttpClient();
- _httpClient.Timeout = TimeSpan.FromMinutes(100);
- _uploadFile = new UploadFile(_httpClient);
- }
- /// <summary>
- /// copy文件到其他节点
- /// </summary>
- /// <param name="fileUrl">源文件地址,例如北京节点文件地址,需要源文件而不是cdn</param>
- /// <param name="fileName">文件名</param>
- /// <returns>是否存在</returns>
- public async Task<List<string>> CopyToOtherNode(string fileUrl, string fileName)
- {
- var res = await _uploadFile.DoCopyFileAsync(fileUrl, fileName);
- return res;
- }
- /// <summary>
- /// 请求验证文件是否存在
- /// </summary>
- /// <param name="oriFileName">FileName</param>
- /// <param name="fileSize">文件大小</param>
- /// <returns>是否存在</returns>
- public async Task<bool> CheckFileIsExist(string oriFileName, long fileSize = 104857600, bool isNeedPartUpload = true, bool isUpgradePackage = false)
- {
- bool result = true;
- if (!isNeedPartUpload)
- {
- var newFirstName = Path.GetFileNameWithoutExtension(oriFileName);
- var newFileExtension = Path.GetExtension(oriFileName);
- var newFileName = newFirstName + newFileExtension;
- var newAuthTuple = SignatureHelper.GetAuthorizationAsync(newFileName, false, "head", null, null, false, isUpgradePackage);
- if (newAuthTuple == null)
- {
- Logger.WriteLineInfo("Head Create Signature Fail");
- return false;
- }
- var newRequestHeads = new Dictionary<string, string>();
- newRequestHeads.Add("Authorization", newAuthTuple.Item1);
- var fileUrl = newAuthTuple.Item2;
- using (var newRequest = new HttpRequestMessage())
- {
- newRequest.RequestUri = new Uri(fileUrl);
- newRequest.Method = HttpMethod.Head;
- foreach (var newHead in newRequestHeads)
- {
- newRequest.Headers.TryAddWithoutValidation(newHead.Key, newHead.Value);
- }
- var newResponse = await _httpClient.SendAsync(newRequest);
- if (newResponse != null && newResponse.StatusCode == HttpStatusCode.OK)
- {
- //read response content
- if (fileUrl.Contains("/SystemUpgradePackage/"))//表示vinno
- {
- var resultContent = newResponse.Content.Headers.ContentLength > 0;
- if (resultContent)
- {
- result = result & true;
- }
- else
- {
- result = result & false;
- }
- }
- else
- {
- var newETag = newResponse.Headers?.ETag?.Tag ?? string.Empty;;
- if (!string.IsNullOrEmpty(newETag))
- {
- result = result & true;
- }
- else
- {
- result = result & false;
- }
- }
- }
- else
- {
- result = result & false;
- }
- }
- return result;
- }
- if (_manualDivisionForUpload <= 0)
- {
- _manualDivisionForUpload = EnvironmentConfigManager.GetParammeter<IntParameter>("Storage", "ManualDivisionForUpload").Value;
- if (_manualDivisionForUpload <= 0)
- {
- _manualDivisionForUpload = 200 * 1024 * 1024;
- }
- }
- var partCount = (int)((fileSize / _manualDivisionForUpload) + (fileSize % _manualDivisionForUpload > 0 ? 1 : 0));
- if (partCount > 0)
- {
- var firstName = Path.GetFileNameWithoutExtension(oriFileName);
- var fileExtension = Path.GetExtension(oriFileName);
- for (var i = 0; i < partCount; i++)
- {
- var fileName = "";
- if (partCount == 1)
- {
- fileName = firstName + fileExtension;
- }
- else
- {
- fileName = firstName + "_" + i + fileExtension;
- }
- var authTuple = SignatureHelper.GetAuthorizationAsync(fileName, false, "head", null, null, false, isUpgradePackage);
- if (authTuple == null)
- {
- Logger.WriteLineInfo("Head Create Signature Fail");
- return false;
- }
- var requestHeads = new Dictionary<string, string>();
- requestHeads.Add("Authorization", authTuple.Item1);
- var fileUrl = authTuple.Item2;
- using (var request = new HttpRequestMessage())
- {
- request.RequestUri = new Uri(fileUrl);
- request.Method = HttpMethod.Head;
- foreach (var head in requestHeads)
- {
- request.Headers.TryAddWithoutValidation(head.Key, head.Value);
- }
- var response = await _httpClient.SendAsync(request);
- if (response != null && response.StatusCode == HttpStatusCode.OK)
- {
- //read response content
- if (fileUrl.Contains("/SystemUpgradePackage/"))//表示vinno
- {
- var resultContent = response.Content.Headers.ContentLength > 0;
- if (resultContent)
- {
- result = result & true;
- }
- else
- {
- result = result & false;
- }
- }
- else
- {
- var eTag = response.Headers?.ETag?.Tag ?? string.Empty;;
- if (!string.IsNullOrEmpty(eTag))
- {
- result = result & true;
- }
- else
- {
- result = result & false;
- break;
- }
- }
- }
- else
- {
- result = result & false;
- break;
- }
- }
- }
- }
- return result;
- }
- /// <summary>
- /// 分块传输上传文件
- /// </summary>
- /// <param name="sourceFilePath">源文件地址 </param>
- /// <param name="isRechristen">Rename</param>
- /// <param name="isCopy">is Copy to OtherNode</param>
- /// <returns>FileUrl List</returns>
- public async Task<List<string>> FileTransferAsync(string sourceFilePath, bool isRechristen = true, bool isNeedPartUpload = true, bool isCopy = false, string newFileName = "")
- {
- var returnFileUrlList = new List<string>();
- FileTransferRecorder record = new FileTransferRecorder();
- try
- {
- if (string.IsNullOrEmpty(sourceFilePath) || !File.Exists(sourceFilePath))
- {
- Logger.WriteLineWarn("File Absolute Path Error! ");
- return new List<string>();
- }
- //读取缓存文件, 如果存在就是需要断点upload
- var recorderFilePath = Path.Combine(_folderBase, ToMD5(sourceFilePath) + ".json");
- if (File.Exists(recorderFilePath))
- {
- //调用断点续传
- return await FileTransferAgainAsync(sourceFilePath, recorderFilePath);
- }
- string oriFilename = Path.GetFileNameWithoutExtension(sourceFilePath);
- var factName = oriFilename;
- var factNameExtension = Path.GetExtension(sourceFilePath);
- if (string.IsNullOrEmpty(factNameExtension))
- {
- factNameExtension = ".dat";
- }
- if (isRechristen)
- {
- if (!string.IsNullOrEmpty(newFileName))
- {
- factName = newFileName;
- }
- else
- {
- factName = Guid.NewGuid().ToString("N").ToUpper();
- }
- }
- if (_manualDivisionForUpload <= 0)
- {
- _manualDivisionForUpload = EnvironmentConfigManager.GetParammeter<IntParameter>("Storage", "ManualDivisionForUpload").Value;
- if (_manualDivisionForUpload <= 0)
- {
- _manualDivisionForUpload = 200 * 1024 * 1024;
- }
- }
- if (!isNeedPartUpload)
- {
- var fileInfo = new System.IO.FileInfo(sourceFilePath);
- if (fileInfo.Length >= 5000000000)
- {
- Logger.WriteLineWarn($"CommonServer FileTransferAsync isNeedPartUpload Error,sourceFilePath: { sourceFilePath }, FileSize: { fileInfo.Length }!");
- return new List<string>();
- }
- }
- //读取文件 ConcurrentDictionary
- var dic = new Dictionary<int, byte[]>();
- dic = ReadFileData(sourceFilePath, _manualDivisionForUpload);
- if (dic == null || dic.Count <= 0)
- {
- Logger.WriteLineWarn("File Data Empty Error!");
- return new List<string>();
- }
- //记录文件信息
- record.FilePath = sourceFilePath;
- record.FileName = oriFilename;
- record.ObjectName = factName;
- record.FileExtension = factNameExtension;
- record.SliceSizeForUpload = _manualDivisionForUpload;
- record.PartList = new List<FileTransferRecorderDetail>();
- record.IsNeedPartUpload = isNeedPartUpload;
-
- var partNums = dic.Keys.OrderBy(c => c).ToList();
- foreach(var i in partNums)
- {
- var partDetail = new FileTransferRecorderDetail();
- if (partNums.Count == 1)
- {
- partDetail.PartFileName = factName + factNameExtension;
- }
- else
- {
- partDetail.PartFileName = factName + "_" + i + factNameExtension;
- }
- partDetail.PartNumber = i;
- partDetail.IsUploadComplete = false;
- partDetail.PartFileSize = dic[i].Length;
- record.PartList.Add(partDetail);
- record.FileSize += partDetail.PartFileSize;
- }
- await SaveTransferRecord(record);
- if (record?.PartList?.Count > 0)
- {
- try
- {
- if (isNeedPartUpload)
- {
- //todo 可尝试优化为Multi Thread
- foreach (var partDetail in record.PartList)
- {
- var fileUrl = await _uploadFile.DoUploadFileAsync(partDetail.PartFileName, dic[partDetail.PartNumber], false, factNameExtension);
- if (!string.IsNullOrEmpty(fileUrl))
- {
- record.Host = fileUrl.Replace(partDetail.PartFileName, "");
- partDetail.IsUploadComplete = true;
- if (isCopy)
- {
- //后续可以改成异步
- await _uploadFile.DoCopyFileAsync(fileUrl, partDetail.PartFileName);
- }
- }
- await SaveTransferRecord(record);
- }
- }
- else
- {
- var fileUrl = await _uploadFile.ManualMultiPartUploadFileAsync(record, dic, false);
- if (isCopy)
- {
- //后续可以改成异步
- string fileName = record.ObjectName + record.FileExtension;
- await _uploadFile.DoCopyFileAsync(fileUrl, fileName);
- }
- await SaveTransferRecord(record);
- returnFileUrlList = new List<string>() { fileUrl };
- }
- }
- catch (Exception e)
- {
- await SaveTransferRecord(record);
- //文件上传错误
- Logger.WriteLineWarn($"File Upload Error, {e}");
- }
- if (record.PartList.Count(c => c.IsUploadComplete) == record.PartList.Count)
- {
- record.IsUploadComplete = true;
- }
- else
- {
- record.IsUploadComplete = false;
- }
- }
- if (record.IsUploadComplete)
- {
- //删除缓存记录
- if (File.Exists(recorderFilePath))
- {
- //存在删除
- File.Delete(recorderFilePath);
- }
- //返回多个链接列表
- if (isNeedPartUpload)
- {
- returnFileUrlList = record.PartList.OrderBy(c => c.PartFileName).Select(c => record.Host + c.PartFileName).ToList();
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"File Transfer Fail, {ex}");
- }
- finally
- {
- GC.Collect();
- }
- return returnFileUrlList;
- }
- /// <summary>
- /// 保存上传记录文件
- /// </summary>
- /// <param name="request">请求对象</param>
- /// <returns>是否成功</returns>
- private async Task<bool> SaveTransferRecord(FileTransferRecorder record)
- {
- if (!Directory.Exists(_folderBase))
- {
- Directory.CreateDirectory(_folderBase);
- }
- var recorderFilePath = Path.Combine(_folderBase, ToMD5(record.FilePath) + ".json");
- if (File.Exists(recorderFilePath))
- {
- //存在删除
- File.Delete(recorderFilePath);
- }
- var json = Newtonsoft.Json.JsonConvert.SerializeObject(record);
- if (!string.IsNullOrWhiteSpace(json))
- {
- byte[] fileData = System.Text.Encoding.UTF8.GetBytes(json);
- //存储文件
- using (var fw = File.Open(recorderFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
- {
- fw.Write(fileData, 0, fileData.Length);
- }
- }
- return true;
- }
- /// <summary>
- /// 文件续传
- /// </summary>
- /// <param name="sourceFilePath">源文件地址</param>
- /// <returns>Part FileUrl List</returns>
- public async Task<List<string>> FileTransferAgainAsync(string sourceFilePath, string recorderFilePath)
- {
- var returnFileUrlList = new List<string>();
- FileTransferRecorder record = new FileTransferRecorder();
- try
- {
- var jsonContent = File.ReadAllText(recorderFilePath);
- if (!string.IsNullOrEmpty(jsonContent))
- {
- record = Newtonsoft.Json.JsonConvert.DeserializeObject<FileTransferRecorder>(jsonContent);
- }
- //读取文件
- if (record?.PartList?.Count > 0)
- {
- var waitUploadFileList = record.PartList.FindAll(c => c.IsUploadComplete == false);
- var dic = new Dictionary<int, byte[]>();
- dic = ReadFileData(sourceFilePath, record.SliceSizeForUpload);
- if (dic == null || dic.Count <= 0)
- {
- Logger.WriteLineWarn("File Data Empty Error!");
- return new List<string>();
- }
- var isNeedPartUpload = record.IsNeedPartUpload;
- try
- {
- if (isNeedPartUpload)
- {
- foreach (var partDetail in waitUploadFileList)
- {
- var fileUrl = await _uploadFile.DoUploadFileAsync(partDetail.PartFileName, dic[partDetail.PartNumber], false, record.FileExtension);
- if (!string.IsNullOrEmpty(fileUrl))
- {
- record.Host = fileUrl.Replace(partDetail.PartFileName, "");
- partDetail.IsUploadComplete = true;
- if (record.IsCopy)
- {
- //后续可以改成异步
- await _uploadFile.DoCopyFileAsync(fileUrl, partDetail.PartFileName);
- }
- }
- await SaveTransferRecord(record);
- }
- }
- else
- {
- var fileUrl = await _uploadFile.ManualMultiPartUploadFileAsync(record, dic, false);
- if (record.IsCopy)
- {
- //后续可以改成异步
- string fileName = record.ObjectName + record.FileExtension;
- await _uploadFile.DoCopyFileAsync(fileUrl, fileName);
- }
- await SaveTransferRecord(record);
- returnFileUrlList = new List<string>() { fileUrl };
- }
- }
- catch (Exception e)
- {
- await SaveTransferRecord(record);
- //文件上传错误
- Logger.WriteLineWarn($"File Upload Again Error, {e}");
- }
- if (record.PartList.Count(c => c.IsUploadComplete) == record.PartList.Count)
- {
- record.IsUploadComplete = true;
- }
- else
- {
- record.IsUploadComplete = false;
- }
-
- if (record.IsUploadComplete)
- {
- //删除缓存记录
- if (File.Exists(recorderFilePath))
- {
- //存在删除
- File.Delete(recorderFilePath);
- }
- //返回多个链接列表;
- if (isNeedPartUpload)
- {
- returnFileUrlList = record.PartList.OrderBy(c => c.PartFileName).Select(c => record.Host + c.PartFileName).ToList();
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"File Transfer Again Fail, {ex}");
- }
- finally
- {
- GC.Collect();
- }
- return returnFileUrlList;
- }
- /// <summary>
- /// 合并文件并保存到指定目录
- /// </summary>
- /// <param name="partFileUrlList">分片地址列表</param>
- /// <param name="savePath">保存指定文件地址(不包含文件名)</param>
- /// <param name="saveFileName">保存指定文件(无需带扩展名)</param>
- /// <returns>保存地址文件</returns>
- public async Task<string> DownloadFileAsync(List<string> partFileUrlList, string savePath, string saveFileName = "")
- {
- var newSaveFileName = "";
- try
- {
- if (string.IsNullOrEmpty(savePath))
- {
- Logger.WriteLineWarn("Save Path Error!");
- return "";
- }
- if (partFileUrlList == null || partFileUrlList.Count <= 0)
- {
- Logger.WriteLineWarn("Download File List Error!");
- return "";
- }
- if (_autoSliceSizeForUpload <= 0)
- {
- _autoSliceSizeForUpload = EnvironmentConfigManager.GetParammeter<IntParameter>("Storage", "AutoSliceSizeForUpload").Value;
- }
- var fileExtension = "";
- if (partFileUrlList[0].Split('.').Length > 0)
- {
- fileExtension = partFileUrlList[0].Split('.')[(partFileUrlList[0].Split('.').Length - 1)];
- }
- else
- {
- fileExtension = "dat";
- }
- //验证文件是否存在
- var fileName = saveFileName;
- if (string.IsNullOrEmpty(fileName))
- {
- fileName = Guid.NewGuid().ToString("N").ToUpper() + "." + fileExtension;
- }
- var recorderFilePath = Path.Combine(savePath, fileName);
- if (File.Exists(recorderFilePath))
- {
- File.Delete(recorderFilePath);
- }
- if (partFileUrlList.Count == 1)
- {
- var url = partFileUrlList.FirstOrDefault() ?? string.Empty;
- var tempArray = await DownloadPartFileAsync(url);
- newSaveFileName = await SaveDownloadFile(savePath, fileExtension, tempArray, saveFileName);
- }
- else
- {
- var dic = new ConcurrentDictionary<string, byte[]>();
- long dataLength = 0;
- partFileUrlList.Sort();
- var parallelOption = new ParallelOptions() { MaxDegreeOfParallelism = 3 };
- Parallel.ForEach(partFileUrlList, parallelOption, item =>
- {
- var tempArray = DownloadPartFileAsync(item).GetAwaiter().GetResult();
- dataLength += tempArray.Length;
- dic.TryAdd(item, tempArray);
- });
-
- byte[] btArr = new byte[dataLength];
- int index = 0;
- foreach (var item in partFileUrlList)
- {
- Array.Copy(dic[item], 0, btArr, index, dic[item].Length);
- index += Convert.ToInt32(dic[item].Length);
- }
- newSaveFileName = await SaveDownloadFile(savePath, fileExtension, btArr, saveFileName);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Download And Save File fail, error: {ex}");
- return string.Empty;
- }
- finally
- {
- GC.Collect();
- }
- return newSaveFileName;
- }
- /// <summary>
- /// 分块下载文件
- /// </summary>
- private async Task<byte[]> DownloadPartFileAsync(string fileUrl)
- {
- byte[] byteArr = null;
- using (var request = new HttpRequestMessage())
- {
- request.RequestUri = new Uri(fileUrl);
- request.Method = HttpMethod.Head;
- var response = await _httpClient.SendAsync(request);
- long allStreamLength = 0;
- if (response != null && response.StatusCode == HttpStatusCode.OK && response.Content != null && response.Content.Headers != null)
- {
- allStreamLength = response.Content.Headers.ContentLength.Value;
- }
- if (allStreamLength == 0)
- {
- Logger.WriteLineWarn($"file length is empty, token:{fileUrl}");
- return null;
- }
- byteArr = new byte[allStreamLength];
- try
- {
- for (int fileIndex = 0; true; fileIndex += _autoSliceSizeForUpload)
- {
- if (fileIndex >= allStreamLength)
- {
- Logger.WriteLineInfo("download fileIndex:"+ fileIndex);
- break;
- }
- var newChunk = fileIndex + _autoSliceSizeForUpload;
- var newLength = newChunk > allStreamLength ? allStreamLength : newChunk;
- var doRes = await _uploadFile.DoHttpRequest(fileUrl, byteArr, fileIndex, newLength, allStreamLength);
- if (!doRes)
- {
- Logger.WriteLineInfo("download doRes:"+ doRes);
- break;
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Downloding file error {ex}");
- return null;
- }
- }
- GC.Collect();
- return byteArr;
- }
- /// <summary>
- /// 保存下载文件
- /// </summary>
- /// <param name="srcPath">保存路径</param>
- /// <param name="fileExtension">File Name Extension</param>
- /// <param name="fileData">数据</param>
- /// <param name="saveFileName">指定保存文件</param>
- /// <returns>保存文件</returns>
- private async Task<string> SaveDownloadFile(string srcPath, string fileExtension, byte[] fileData, string saveFileName = "")
- {
- if (!Directory.Exists(srcPath))
- {
- Directory.CreateDirectory(srcPath);
- }
- var fileName = saveFileName;
- if (string.IsNullOrEmpty(fileName))
- {
- fileName = Guid.NewGuid().ToString("N").ToUpper() + "." + fileExtension;
- }
- var recorderFilePath = Path.Combine(srcPath, fileName);
- if (File.Exists(recorderFilePath))
- {
- //存在删除
- File.Delete(recorderFilePath);
- }
- //存储文件
- using (var fw = File.Open(recorderFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
- {
- fw.Write(fileData, 0, fileData.Length);
- }
- return fileName;
- }
- /// <summary>
- /// 读取文件
- /// </summary>
- private Dictionary<int, byte[]> ReadFileData(string filePath, long manualDivisionForUpload)
- {
- var dic = new Dictionary<int, byte[]>();
- using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
- {
- var partCount = (int)((fs.Length / manualDivisionForUpload) + (fs.Length % manualDivisionForUpload > 0 ? 1 : 0));
- //大于1.7G的问题,用分块读取File
- if (fs.Length > 1782579200)
- {
- var byteSize = 10;
- byte[] buffer = new byte[byteSize * 1024 * 1024];//10M的缓冲byte
- int fileCursor = 0;
- int readerCursor = 0;
- var splitFileTimes = manualDivisionForUpload / buffer.Length + (manualDivisionForUpload % buffer.Length > 0 ? 1 : 0);
- var fileSplitFileTimes = fs.Length / buffer.Length + (fs.Length % buffer.Length > 0 ? 1 : 0);
- if (fileSplitFileTimes < splitFileTimes)
- {
- splitFileTimes = fileSplitFileTimes;
- }
- //超过200就不能读取了,会溢出
- if (splitFileTimes > 200)
- {
- return dic;
- }
- NextFileBegin:
- using (MemoryStream ms = new MemoryStream())
- {
- int readLength = 0;
- while ((readLength = fs.Read(buffer, 0, buffer.Length)) > 0)
- {
- readerCursor++;
- ms.Write(buffer, 0, readLength);
- if (readerCursor >= splitFileTimes)
- {
- var allBytes = ms.ToArray();
- dic.Add(fileCursor, allBytes);
- readerCursor = 0;
- fileCursor++;
- break;
- }
- else
- {
- if (readLength < buffer.Length && readerCursor > 0 && readerCursor < splitFileTimes)
- {
- //表示最后一个了
- var allBytes = ms.ToArray();
- dic.Add(fileCursor, allBytes);
- readerCursor = 0;
- fileCursor++;
- break;
- }
- }
- }
- }
- if (fileCursor < partCount)
- {
- goto NextFileBegin;
- }
- }
- else
- {
- //读取文件
- byte[] btArr = new byte[fs.Length];
- fs.Read(btArr, 0, btArr.Length);
- if (partCount > 1)
- {
- long offsetIndex = 0;
- for (var i = 0; i < partCount; i++)
- {
- var tempLength = manualDivisionForUpload;
- if ((partCount - 1) == i)
- {
- //最后一个分块File
- tempLength = btArr.Length - (partCount - 1) * manualDivisionForUpload;
- }
- var tempByte = new byte[tempLength];
- offsetIndex = i * manualDivisionForUpload;
- Array.Copy(btArr, offsetIndex, tempByte, 0, tempByte.Length);
- dic.Add(i, tempByte);
- }
- }
- else
- {
- var partDetail = new FileTransferRecorderDetail();
- dic.TryAdd(0, btArr);
- }
- }
- }
- return dic;
- }
- /// <summary>
- /// Encrypt to MD5 string
- /// </summary>
- /// <param name="str">source string</param>
- /// <returns></returns>
- private string ToMD5(string str)
- {
- if (string.IsNullOrWhiteSpace(str)) return string.Empty;
- System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
- byte[] bPwd = Encoding.UTF8.GetBytes(str);
- byte[] bMD5 = md5.ComputeHash(bPwd);
- md5.Clear();
- StringBuilder sbMD5Pwd = new StringBuilder();
- for (int i = 0; i < bMD5.Length; i++)
- {
- sbMD5Pwd.Append(bMD5[i].ToString("x2"));
- }
- return sbMD5Pwd.ToString();
- }
- }
- }
|