FileTransferService.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. using System.Collections.Concurrent;
  2. using WingServerCommon.Log;
  3. using System.Net;
  4. using System.Net.Http.Headers;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using WingServerCommon.Config;
  8. using WingServerCommon.Config.Parameters;
  9. namespace WingServerCommon.Interfaces.FileTransfer
  10. {
  11. /// <summary>
  12. /// 文件传输服务
  13. /// </summary>
  14. public class FileTransferService
  15. {
  16. private readonly HttpClient _httpClient;
  17. private readonly UploadFile _uploadFile;
  18. private string _folderBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileTransferRecord");
  19. private int _autoSliceSizeForUpload = 0;
  20. private long _manualDivisionForUpload = 0;
  21. /// <summary>
  22. /// Init service
  23. /// </summary>
  24. public FileTransferService()
  25. {
  26. _httpClient = new HttpClient();
  27. _httpClient.Timeout = TimeSpan.FromMinutes(100);
  28. _uploadFile = new UploadFile(_httpClient);
  29. }
  30. /// <summary>
  31. /// copy文件到其他节点
  32. /// </summary>
  33. /// <param name="fileUrl">源文件地址,例如北京节点文件地址,需要源文件而不是cdn</param>
  34. /// <param name="fileName">文件名</param>
  35. /// <returns>是否存在</returns>
  36. public async Task<List<string>> CopyToOtherNode(string fileUrl, string fileName)
  37. {
  38. var res = await _uploadFile.DoCopyFileAsync(fileUrl, fileName);
  39. return res;
  40. }
  41. /// <summary>
  42. /// 请求验证文件是否存在
  43. /// </summary>
  44. /// <param name="oriFileName">FileName</param>
  45. /// <param name="fileSize">文件大小</param>
  46. /// <returns>是否存在</returns>
  47. public async Task<bool> CheckFileIsExist(string oriFileName, long fileSize = 104857600, bool isNeedPartUpload = true, bool isUpgradePackage = false)
  48. {
  49. bool result = true;
  50. if (!isNeedPartUpload)
  51. {
  52. var newFirstName = Path.GetFileNameWithoutExtension(oriFileName);
  53. var newFileExtension = Path.GetExtension(oriFileName);
  54. var newFileName = newFirstName + newFileExtension;
  55. var newAuthTuple = SignatureHelper.GetAuthorizationAsync(newFileName, false, "head", null, null, false, isUpgradePackage);
  56. if (newAuthTuple == null)
  57. {
  58. Logger.WriteLineInfo("Head Create Signature Fail");
  59. return false;
  60. }
  61. var newRequestHeads = new Dictionary<string, string>();
  62. newRequestHeads.Add("Authorization", newAuthTuple.Item1);
  63. var fileUrl = newAuthTuple.Item2;
  64. using (var newRequest = new HttpRequestMessage())
  65. {
  66. newRequest.RequestUri = new Uri(fileUrl);
  67. newRequest.Method = HttpMethod.Head;
  68. foreach (var newHead in newRequestHeads)
  69. {
  70. newRequest.Headers.TryAddWithoutValidation(newHead.Key, newHead.Value);
  71. }
  72. var newResponse = await _httpClient.SendAsync(newRequest);
  73. if (newResponse != null && newResponse.StatusCode == HttpStatusCode.OK)
  74. {
  75. //read response content
  76. if (fileUrl.Contains("/SystemUpgradePackage/"))//表示vinno
  77. {
  78. var resultContent = newResponse.Content.Headers.ContentLength > 0;
  79. if (resultContent)
  80. {
  81. result = result & true;
  82. }
  83. else
  84. {
  85. result = result & false;
  86. }
  87. }
  88. else
  89. {
  90. var newETag = newResponse.Headers?.ETag?.Tag ?? string.Empty;;
  91. if (!string.IsNullOrEmpty(newETag))
  92. {
  93. result = result & true;
  94. }
  95. else
  96. {
  97. result = result & false;
  98. }
  99. }
  100. }
  101. else
  102. {
  103. result = result & false;
  104. }
  105. }
  106. return result;
  107. }
  108. if (_manualDivisionForUpload <= 0)
  109. {
  110. _manualDivisionForUpload = EnvironmentConfigManager.GetParammeter<IntParameter>("Storage", "ManualDivisionForUpload").Value;
  111. if (_manualDivisionForUpload <= 0)
  112. {
  113. _manualDivisionForUpload = 200 * 1024 * 1024;
  114. }
  115. }
  116. var partCount = (int)((fileSize / _manualDivisionForUpload) + (fileSize % _manualDivisionForUpload > 0 ? 1 : 0));
  117. if (partCount > 0)
  118. {
  119. var firstName = Path.GetFileNameWithoutExtension(oriFileName);
  120. var fileExtension = Path.GetExtension(oriFileName);
  121. for (var i = 0; i < partCount; i++)
  122. {
  123. var fileName = "";
  124. if (partCount == 1)
  125. {
  126. fileName = firstName + fileExtension;
  127. }
  128. else
  129. {
  130. fileName = firstName + "_" + i + fileExtension;
  131. }
  132. var authTuple = SignatureHelper.GetAuthorizationAsync(fileName, false, "head", null, null, false, isUpgradePackage);
  133. if (authTuple == null)
  134. {
  135. Logger.WriteLineInfo("Head Create Signature Fail");
  136. return false;
  137. }
  138. var requestHeads = new Dictionary<string, string>();
  139. requestHeads.Add("Authorization", authTuple.Item1);
  140. var fileUrl = authTuple.Item2;
  141. using (var request = new HttpRequestMessage())
  142. {
  143. request.RequestUri = new Uri(fileUrl);
  144. request.Method = HttpMethod.Head;
  145. foreach (var head in requestHeads)
  146. {
  147. request.Headers.TryAddWithoutValidation(head.Key, head.Value);
  148. }
  149. var response = await _httpClient.SendAsync(request);
  150. if (response != null && response.StatusCode == HttpStatusCode.OK)
  151. {
  152. //read response content
  153. if (fileUrl.Contains("/SystemUpgradePackage/"))//表示vinno
  154. {
  155. var resultContent = response.Content.Headers.ContentLength > 0;
  156. if (resultContent)
  157. {
  158. result = result & true;
  159. }
  160. else
  161. {
  162. result = result & false;
  163. }
  164. }
  165. else
  166. {
  167. var eTag = response.Headers?.ETag?.Tag ?? string.Empty;;
  168. if (!string.IsNullOrEmpty(eTag))
  169. {
  170. result = result & true;
  171. }
  172. else
  173. {
  174. result = result & false;
  175. break;
  176. }
  177. }
  178. }
  179. else
  180. {
  181. result = result & false;
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. return result;
  188. }
  189. /// <summary>
  190. /// 分块传输上传文件
  191. /// </summary>
  192. /// <param name="sourceFilePath">源文件地址 </param>
  193. /// <param name="isRechristen">Rename</param>
  194. /// <param name="isCopy">is Copy to OtherNode</param>
  195. /// <returns>FileUrl List</returns>
  196. public async Task<List<string>> FileTransferAsync(string sourceFilePath, bool isRechristen = true, bool isNeedPartUpload = true, bool isCopy = false, string newFileName = "")
  197. {
  198. var returnFileUrlList = new List<string>();
  199. FileTransferRecorder record = new FileTransferRecorder();
  200. try
  201. {
  202. if (string.IsNullOrEmpty(sourceFilePath) || !File.Exists(sourceFilePath))
  203. {
  204. Logger.WriteLineWarn("File Absolute Path Error! ");
  205. return new List<string>();
  206. }
  207. //读取缓存文件, 如果存在就是需要断点upload
  208. var recorderFilePath = Path.Combine(_folderBase, ToMD5(sourceFilePath) + ".json");
  209. if (File.Exists(recorderFilePath))
  210. {
  211. //调用断点续传
  212. return await FileTransferAgainAsync(sourceFilePath, recorderFilePath);
  213. }
  214. string oriFilename = Path.GetFileNameWithoutExtension(sourceFilePath);
  215. var factName = oriFilename;
  216. var factNameExtension = Path.GetExtension(sourceFilePath);
  217. if (string.IsNullOrEmpty(factNameExtension))
  218. {
  219. factNameExtension = ".dat";
  220. }
  221. if (isRechristen)
  222. {
  223. if (!string.IsNullOrEmpty(newFileName))
  224. {
  225. factName = newFileName;
  226. }
  227. else
  228. {
  229. factName = Guid.NewGuid().ToString("N").ToUpper();
  230. }
  231. }
  232. if (_manualDivisionForUpload <= 0)
  233. {
  234. _manualDivisionForUpload = EnvironmentConfigManager.GetParammeter<IntParameter>("Storage", "ManualDivisionForUpload").Value;
  235. if (_manualDivisionForUpload <= 0)
  236. {
  237. _manualDivisionForUpload = 200 * 1024 * 1024;
  238. }
  239. }
  240. if (!isNeedPartUpload)
  241. {
  242. var fileInfo = new System.IO.FileInfo(sourceFilePath);
  243. if (fileInfo.Length >= 5000000000)
  244. {
  245. Logger.WriteLineWarn($"CommonServer FileTransferAsync isNeedPartUpload Error,sourceFilePath: { sourceFilePath }, FileSize: { fileInfo.Length }!");
  246. return new List<string>();
  247. }
  248. }
  249. //读取文件 ConcurrentDictionary
  250. var dic = new Dictionary<int, byte[]>();
  251. dic = ReadFileData(sourceFilePath, _manualDivisionForUpload);
  252. if (dic == null || dic.Count <= 0)
  253. {
  254. Logger.WriteLineWarn("File Data Empty Error!");
  255. return new List<string>();
  256. }
  257. //记录文件信息
  258. record.FilePath = sourceFilePath;
  259. record.FileName = oriFilename;
  260. record.ObjectName = factName;
  261. record.FileExtension = factNameExtension;
  262. record.SliceSizeForUpload = _manualDivisionForUpload;
  263. record.PartList = new List<FileTransferRecorderDetail>();
  264. record.IsNeedPartUpload = isNeedPartUpload;
  265. var partNums = dic.Keys.OrderBy(c => c).ToList();
  266. foreach(var i in partNums)
  267. {
  268. var partDetail = new FileTransferRecorderDetail();
  269. if (partNums.Count == 1)
  270. {
  271. partDetail.PartFileName = factName + factNameExtension;
  272. }
  273. else
  274. {
  275. partDetail.PartFileName = factName + "_" + i + factNameExtension;
  276. }
  277. partDetail.PartNumber = i;
  278. partDetail.IsUploadComplete = false;
  279. partDetail.PartFileSize = dic[i].Length;
  280. record.PartList.Add(partDetail);
  281. record.FileSize += partDetail.PartFileSize;
  282. }
  283. await SaveTransferRecord(record);
  284. if (record?.PartList?.Count > 0)
  285. {
  286. try
  287. {
  288. if (isNeedPartUpload)
  289. {
  290. //todo 可尝试优化为Multi Thread
  291. foreach (var partDetail in record.PartList)
  292. {
  293. var fileUrl = await _uploadFile.DoUploadFileAsync(partDetail.PartFileName, dic[partDetail.PartNumber], false, factNameExtension);
  294. if (!string.IsNullOrEmpty(fileUrl))
  295. {
  296. record.Host = fileUrl.Replace(partDetail.PartFileName, "");
  297. partDetail.IsUploadComplete = true;
  298. if (isCopy)
  299. {
  300. //后续可以改成异步
  301. await _uploadFile.DoCopyFileAsync(fileUrl, partDetail.PartFileName);
  302. }
  303. }
  304. await SaveTransferRecord(record);
  305. }
  306. }
  307. else
  308. {
  309. var fileUrl = await _uploadFile.ManualMultiPartUploadFileAsync(record, dic, false);
  310. if (isCopy)
  311. {
  312. //后续可以改成异步
  313. string fileName = record.ObjectName + record.FileExtension;
  314. await _uploadFile.DoCopyFileAsync(fileUrl, fileName);
  315. }
  316. await SaveTransferRecord(record);
  317. returnFileUrlList = new List<string>() { fileUrl };
  318. }
  319. }
  320. catch (Exception e)
  321. {
  322. await SaveTransferRecord(record);
  323. //文件上传错误
  324. Logger.WriteLineWarn($"File Upload Error, {e}");
  325. }
  326. if (record.PartList.Count(c => c.IsUploadComplete) == record.PartList.Count)
  327. {
  328. record.IsUploadComplete = true;
  329. }
  330. else
  331. {
  332. record.IsUploadComplete = false;
  333. }
  334. }
  335. if (record.IsUploadComplete)
  336. {
  337. //删除缓存记录
  338. if (File.Exists(recorderFilePath))
  339. {
  340. //存在删除
  341. File.Delete(recorderFilePath);
  342. }
  343. //返回多个链接列表
  344. if (isNeedPartUpload)
  345. {
  346. returnFileUrlList = record.PartList.OrderBy(c => c.PartFileName).Select(c => record.Host + c.PartFileName).ToList();
  347. }
  348. }
  349. }
  350. catch (Exception ex)
  351. {
  352. Logger.WriteLineError($"File Transfer Fail, {ex}");
  353. }
  354. finally
  355. {
  356. GC.Collect();
  357. }
  358. return returnFileUrlList;
  359. }
  360. /// <summary>
  361. /// 保存上传记录文件
  362. /// </summary>
  363. /// <param name="request">请求对象</param>
  364. /// <returns>是否成功</returns>
  365. private async Task<bool> SaveTransferRecord(FileTransferRecorder record)
  366. {
  367. if (!Directory.Exists(_folderBase))
  368. {
  369. Directory.CreateDirectory(_folderBase);
  370. }
  371. var recorderFilePath = Path.Combine(_folderBase, ToMD5(record.FilePath) + ".json");
  372. if (File.Exists(recorderFilePath))
  373. {
  374. //存在删除
  375. File.Delete(recorderFilePath);
  376. }
  377. var json = Newtonsoft.Json.JsonConvert.SerializeObject(record);
  378. if (!string.IsNullOrWhiteSpace(json))
  379. {
  380. byte[] fileData = System.Text.Encoding.UTF8.GetBytes(json);
  381. //存储文件
  382. using (var fw = File.Open(recorderFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
  383. {
  384. fw.Write(fileData, 0, fileData.Length);
  385. }
  386. }
  387. return true;
  388. }
  389. /// <summary>
  390. /// 文件续传
  391. /// </summary>
  392. /// <param name="sourceFilePath">源文件地址</param>
  393. /// <returns>Part FileUrl List</returns>
  394. public async Task<List<string>> FileTransferAgainAsync(string sourceFilePath, string recorderFilePath)
  395. {
  396. var returnFileUrlList = new List<string>();
  397. FileTransferRecorder record = new FileTransferRecorder();
  398. try
  399. {
  400. var jsonContent = File.ReadAllText(recorderFilePath);
  401. if (!string.IsNullOrEmpty(jsonContent))
  402. {
  403. record = Newtonsoft.Json.JsonConvert.DeserializeObject<FileTransferRecorder>(jsonContent);
  404. }
  405. //读取文件
  406. if (record?.PartList?.Count > 0)
  407. {
  408. var waitUploadFileList = record.PartList.FindAll(c => c.IsUploadComplete == false);
  409. var dic = new Dictionary<int, byte[]>();
  410. dic = ReadFileData(sourceFilePath, record.SliceSizeForUpload);
  411. if (dic == null || dic.Count <= 0)
  412. {
  413. Logger.WriteLineWarn("File Data Empty Error!");
  414. return new List<string>();
  415. }
  416. var isNeedPartUpload = record.IsNeedPartUpload;
  417. try
  418. {
  419. if (isNeedPartUpload)
  420. {
  421. foreach (var partDetail in waitUploadFileList)
  422. {
  423. var fileUrl = await _uploadFile.DoUploadFileAsync(partDetail.PartFileName, dic[partDetail.PartNumber], false, record.FileExtension);
  424. if (!string.IsNullOrEmpty(fileUrl))
  425. {
  426. record.Host = fileUrl.Replace(partDetail.PartFileName, "");
  427. partDetail.IsUploadComplete = true;
  428. if (record.IsCopy)
  429. {
  430. //后续可以改成异步
  431. await _uploadFile.DoCopyFileAsync(fileUrl, partDetail.PartFileName);
  432. }
  433. }
  434. await SaveTransferRecord(record);
  435. }
  436. }
  437. else
  438. {
  439. var fileUrl = await _uploadFile.ManualMultiPartUploadFileAsync(record, dic, false);
  440. if (record.IsCopy)
  441. {
  442. //后续可以改成异步
  443. string fileName = record.ObjectName + record.FileExtension;
  444. await _uploadFile.DoCopyFileAsync(fileUrl, fileName);
  445. }
  446. await SaveTransferRecord(record);
  447. returnFileUrlList = new List<string>() { fileUrl };
  448. }
  449. }
  450. catch (Exception e)
  451. {
  452. await SaveTransferRecord(record);
  453. //文件上传错误
  454. Logger.WriteLineWarn($"File Upload Again Error, {e}");
  455. }
  456. if (record.PartList.Count(c => c.IsUploadComplete) == record.PartList.Count)
  457. {
  458. record.IsUploadComplete = true;
  459. }
  460. else
  461. {
  462. record.IsUploadComplete = false;
  463. }
  464. if (record.IsUploadComplete)
  465. {
  466. //删除缓存记录
  467. if (File.Exists(recorderFilePath))
  468. {
  469. //存在删除
  470. File.Delete(recorderFilePath);
  471. }
  472. //返回多个链接列表;
  473. if (isNeedPartUpload)
  474. {
  475. returnFileUrlList = record.PartList.OrderBy(c => c.PartFileName).Select(c => record.Host + c.PartFileName).ToList();
  476. }
  477. }
  478. }
  479. }
  480. catch (Exception ex)
  481. {
  482. Logger.WriteLineError($"File Transfer Again Fail, {ex}");
  483. }
  484. finally
  485. {
  486. GC.Collect();
  487. }
  488. return returnFileUrlList;
  489. }
  490. /// <summary>
  491. /// 合并文件并保存到指定目录
  492. /// </summary>
  493. /// <param name="partFileUrlList">分片地址列表</param>
  494. /// <param name="savePath">保存指定文件地址(不包含文件名)</param>
  495. /// <param name="saveFileName">保存指定文件(无需带扩展名)</param>
  496. /// <returns>保存地址文件</returns>
  497. public async Task<string> DownloadFileAsync(List<string> partFileUrlList, string savePath, string saveFileName = "")
  498. {
  499. var newSaveFileName = "";
  500. try
  501. {
  502. if (string.IsNullOrEmpty(savePath))
  503. {
  504. Logger.WriteLineWarn("Save Path Error!");
  505. return "";
  506. }
  507. if (partFileUrlList == null || partFileUrlList.Count <= 0)
  508. {
  509. Logger.WriteLineWarn("Download File List Error!");
  510. return "";
  511. }
  512. if (_autoSliceSizeForUpload <= 0)
  513. {
  514. _autoSliceSizeForUpload = EnvironmentConfigManager.GetParammeter<IntParameter>("Storage", "AutoSliceSizeForUpload").Value;
  515. }
  516. var fileExtension = "";
  517. if (partFileUrlList[0].Split('.').Length > 0)
  518. {
  519. fileExtension = partFileUrlList[0].Split('.')[(partFileUrlList[0].Split('.').Length - 1)];
  520. }
  521. else
  522. {
  523. fileExtension = "dat";
  524. }
  525. //验证文件是否存在
  526. var fileName = saveFileName;
  527. if (string.IsNullOrEmpty(fileName))
  528. {
  529. fileName = Guid.NewGuid().ToString("N").ToUpper() + "." + fileExtension;
  530. }
  531. var recorderFilePath = Path.Combine(savePath, fileName);
  532. if (File.Exists(recorderFilePath))
  533. {
  534. File.Delete(recorderFilePath);
  535. }
  536. if (partFileUrlList.Count == 1)
  537. {
  538. var url = partFileUrlList.FirstOrDefault() ?? string.Empty;
  539. var tempArray = await DownloadPartFileAsync(url);
  540. newSaveFileName = await SaveDownloadFile(savePath, fileExtension, tempArray, saveFileName);
  541. }
  542. else
  543. {
  544. var dic = new ConcurrentDictionary<string, byte[]>();
  545. long dataLength = 0;
  546. partFileUrlList.Sort();
  547. var parallelOption = new ParallelOptions() { MaxDegreeOfParallelism = 3 };
  548. Parallel.ForEach(partFileUrlList, parallelOption, item =>
  549. {
  550. var tempArray = DownloadPartFileAsync(item).GetAwaiter().GetResult();
  551. dataLength += tempArray.Length;
  552. dic.TryAdd(item, tempArray);
  553. });
  554. byte[] btArr = new byte[dataLength];
  555. int index = 0;
  556. foreach (var item in partFileUrlList)
  557. {
  558. Array.Copy(dic[item], 0, btArr, index, dic[item].Length);
  559. index += Convert.ToInt32(dic[item].Length);
  560. }
  561. newSaveFileName = await SaveDownloadFile(savePath, fileExtension, btArr, saveFileName);
  562. }
  563. }
  564. catch (Exception ex)
  565. {
  566. Logger.WriteLineError($"Download And Save File fail, error: {ex}");
  567. return string.Empty;
  568. }
  569. finally
  570. {
  571. GC.Collect();
  572. }
  573. return newSaveFileName;
  574. }
  575. /// <summary>
  576. /// 分块下载文件
  577. /// </summary>
  578. private async Task<byte[]> DownloadPartFileAsync(string fileUrl)
  579. {
  580. byte[] byteArr = null;
  581. using (var request = new HttpRequestMessage())
  582. {
  583. request.RequestUri = new Uri(fileUrl);
  584. request.Method = HttpMethod.Head;
  585. var response = await _httpClient.SendAsync(request);
  586. long allStreamLength = 0;
  587. if (response != null && response.StatusCode == HttpStatusCode.OK && response.Content != null && response.Content.Headers != null)
  588. {
  589. allStreamLength = response.Content.Headers.ContentLength.Value;
  590. }
  591. if (allStreamLength == 0)
  592. {
  593. Logger.WriteLineWarn($"file length is empty, token:{fileUrl}");
  594. return null;
  595. }
  596. byteArr = new byte[allStreamLength];
  597. try
  598. {
  599. for (int fileIndex = 0; true; fileIndex += _autoSliceSizeForUpload)
  600. {
  601. if (fileIndex >= allStreamLength)
  602. {
  603. Logger.WriteLineInfo("download fileIndex:"+ fileIndex);
  604. break;
  605. }
  606. var newChunk = fileIndex + _autoSliceSizeForUpload;
  607. var newLength = newChunk > allStreamLength ? allStreamLength : newChunk;
  608. var doRes = await _uploadFile.DoHttpRequest(fileUrl, byteArr, fileIndex, newLength, allStreamLength);
  609. if (!doRes)
  610. {
  611. Logger.WriteLineInfo("download doRes:"+ doRes);
  612. break;
  613. }
  614. }
  615. }
  616. catch (Exception ex)
  617. {
  618. Logger.WriteLineError($"Downloding file error {ex}");
  619. return null;
  620. }
  621. }
  622. GC.Collect();
  623. return byteArr;
  624. }
  625. /// <summary>
  626. /// 保存下载文件
  627. /// </summary>
  628. /// <param name="srcPath">保存路径</param>
  629. /// <param name="fileExtension">File Name Extension</param>
  630. /// <param name="fileData">数据</param>
  631. /// <param name="saveFileName">指定保存文件</param>
  632. /// <returns>保存文件</returns>
  633. private async Task<string> SaveDownloadFile(string srcPath, string fileExtension, byte[] fileData, string saveFileName = "")
  634. {
  635. if (!Directory.Exists(srcPath))
  636. {
  637. Directory.CreateDirectory(srcPath);
  638. }
  639. var fileName = saveFileName;
  640. if (string.IsNullOrEmpty(fileName))
  641. {
  642. fileName = Guid.NewGuid().ToString("N").ToUpper() + "." + fileExtension;
  643. }
  644. var recorderFilePath = Path.Combine(srcPath, fileName);
  645. if (File.Exists(recorderFilePath))
  646. {
  647. //存在删除
  648. File.Delete(recorderFilePath);
  649. }
  650. //存储文件
  651. using (var fw = File.Open(recorderFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
  652. {
  653. fw.Write(fileData, 0, fileData.Length);
  654. }
  655. return fileName;
  656. }
  657. /// <summary>
  658. /// 读取文件
  659. /// </summary>
  660. private Dictionary<int, byte[]> ReadFileData(string filePath, long manualDivisionForUpload)
  661. {
  662. var dic = new Dictionary<int, byte[]>();
  663. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  664. {
  665. var partCount = (int)((fs.Length / manualDivisionForUpload) + (fs.Length % manualDivisionForUpload > 0 ? 1 : 0));
  666. //大于1.7G的问题,用分块读取File
  667. if (fs.Length > 1782579200)
  668. {
  669. var byteSize = 10;
  670. byte[] buffer = new byte[byteSize * 1024 * 1024];//10M的缓冲byte
  671. int fileCursor = 0;
  672. int readerCursor = 0;
  673. var splitFileTimes = manualDivisionForUpload / buffer.Length + (manualDivisionForUpload % buffer.Length > 0 ? 1 : 0);
  674. var fileSplitFileTimes = fs.Length / buffer.Length + (fs.Length % buffer.Length > 0 ? 1 : 0);
  675. if (fileSplitFileTimes < splitFileTimes)
  676. {
  677. splitFileTimes = fileSplitFileTimes;
  678. }
  679. //超过200就不能读取了,会溢出
  680. if (splitFileTimes > 200)
  681. {
  682. return dic;
  683. }
  684. NextFileBegin:
  685. using (MemoryStream ms = new MemoryStream())
  686. {
  687. int readLength = 0;
  688. while ((readLength = fs.Read(buffer, 0, buffer.Length)) > 0)
  689. {
  690. readerCursor++;
  691. ms.Write(buffer, 0, readLength);
  692. if (readerCursor >= splitFileTimes)
  693. {
  694. var allBytes = ms.ToArray();
  695. dic.Add(fileCursor, allBytes);
  696. readerCursor = 0;
  697. fileCursor++;
  698. break;
  699. }
  700. else
  701. {
  702. if (readLength < buffer.Length && readerCursor > 0 && readerCursor < splitFileTimes)
  703. {
  704. //表示最后一个了
  705. var allBytes = ms.ToArray();
  706. dic.Add(fileCursor, allBytes);
  707. readerCursor = 0;
  708. fileCursor++;
  709. break;
  710. }
  711. }
  712. }
  713. }
  714. if (fileCursor < partCount)
  715. {
  716. goto NextFileBegin;
  717. }
  718. }
  719. else
  720. {
  721. //读取文件
  722. byte[] btArr = new byte[fs.Length];
  723. fs.Read(btArr, 0, btArr.Length);
  724. if (partCount > 1)
  725. {
  726. long offsetIndex = 0;
  727. for (var i = 0; i < partCount; i++)
  728. {
  729. var tempLength = manualDivisionForUpload;
  730. if ((partCount - 1) == i)
  731. {
  732. //最后一个分块File
  733. tempLength = btArr.Length - (partCount - 1) * manualDivisionForUpload;
  734. }
  735. var tempByte = new byte[tempLength];
  736. offsetIndex = i * manualDivisionForUpload;
  737. Array.Copy(btArr, offsetIndex, tempByte, 0, tempByte.Length);
  738. dic.Add(i, tempByte);
  739. }
  740. }
  741. else
  742. {
  743. var partDetail = new FileTransferRecorderDetail();
  744. dic.TryAdd(0, btArr);
  745. }
  746. }
  747. }
  748. return dic;
  749. }
  750. /// <summary>
  751. /// Encrypt to MD5 string
  752. /// </summary>
  753. /// <param name="str">source string</param>
  754. /// <returns></returns>
  755. private string ToMD5(string str)
  756. {
  757. if (string.IsNullOrWhiteSpace(str)) return string.Empty;
  758. System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
  759. byte[] bPwd = Encoding.UTF8.GetBytes(str);
  760. byte[] bMD5 = md5.ComputeHash(bPwd);
  761. md5.Clear();
  762. StringBuilder sbMD5Pwd = new StringBuilder();
  763. for (int i = 0; i < bMD5.Length; i++)
  764. {
  765. sbMD5Pwd.Append(bMD5[i].ToString("x2"));
  766. }
  767. return sbMD5Pwd.ToString();
  768. }
  769. }
  770. }