123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Threading;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.Storages;
- using Vinno.vCloud.Common.Storage.ObjectStorageInfo;
- namespace Vinno.vCloud.Common.FIS.Helper
- {
- internal class UploadFileHelper
- {
- /// <summary>上传文件</summary>
- /// <param name="fileData"></param>
- /// <param name="requestUrl"></param>
- /// <param name="authorization"></param>
- /// <returns></returns>
- public static Tuple<bool, string> UploadFile(string requestUrl, byte[] fileData, string mimeType, string authorization, Action<double> progressCallback = null, CancellationTokenSource cancelTokenSource = null)
- {
- var contentType = MediaTypeHeaderValue.Parse(mimeType);
- var requestHeads = new Dictionary<string, string>();
- ProductInfoHeaderValue productInfoHeaderValue = null;
- requestHeads.Add("Authorization", authorization);
- return UploadFile(fileData, contentType, requestUrl, requestHeads, productInfoHeaderValue, progressCallback, cancelTokenSource);
- }
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="requestUrl"></param>
- /// <param name="filePath"></param>
- /// <param name="mimeType"></param>
- /// <param name="authorization"></param>
- /// <param name="progressCallback"></param>
- /// <param name="cancelTokenSource"></param>
- /// <returns></returns>
- public static Tuple<bool, string> UploadFile(string requestUrl, string filePath, string mimeType, string authorization, Action<double> progressCallback = null, CancellationTokenSource cancelTokenSource = null)
- {
- var contentType = MediaTypeHeaderValue.Parse(mimeType);
- var requestHeads = new Dictionary<string, string>();
- ProductInfoHeaderValue productInfoHeaderValue = null;
- requestHeads.Add("Authorization", authorization);
- using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- byte[] buffer = new byte[fileStream.Length];
- fileStream.Read(buffer, 0, buffer.Length);
- return UploadFile(buffer, contentType, requestUrl, requestHeads, productInfoHeaderValue, progressCallback, cancelTokenSource);
- }
- }
- /// <summary>
- /// 上传文件(字节流)
- /// </summary>
- /// <param name="fileData"></param>
- /// <param name="mediaTypeHeaderValue"></param>
- /// <param name="requestUrl"></param>
- /// <param name="heads"></param>
- /// <param name="productInfoHeader"></param>
- /// <param name="progressCallback"></param>
- /// <param name="cancelTokenSource"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- private static Tuple<bool, string> UploadFile(byte[] fileData, MediaTypeHeaderValue mediaTypeHeaderValue, string requestUrl, Dictionary<string, string> heads, ProductInfoHeaderValue productInfoHeader, Action<double> progressCallback = null, CancellationTokenSource cancelTokenSource = null)
- {
- if (fileData == null || fileData.Length == 0)
- {
- throw new Exception("FileData is empty");
- }
- using (var request = new HttpRequestMessage())
- {
- using (var content = new UploadContent(fileData, mediaTypeHeaderValue))
- {
- content.ProgressChanged += (sender, progress) => { progressCallback?.Invoke((double)progress / 100); };
- request.RequestUri = new Uri(requestUrl);
- request.Method = HttpMethod.Put;
- request.Content = content;
- if (productInfoHeader != null)
- {
- request.Headers.UserAgent.Add(productInfoHeader);
- }
- foreach (var head in heads)
- {
- request.Headers.TryAddWithoutValidation(head.Key, head.Value);
- }
- return HttpClientHelper.ExecuteRequest(request, GetUploadResult, false, cancelTokenSource);
- }
- }
- }
- private static Tuple<bool, string> GetUploadResult(HttpResponseMessage response)
- {
- if (response != null && response.StatusCode == HttpStatusCode.OK)
- {
- return new Tuple<bool, string>(true, response.Headers?.ETag?.Tag ?? string.Empty);
- }
- Logger.WriteLineError($"GetUploadResult Failed, StatusCode: {response?.StatusCode}");
- return new Tuple<bool, string>(false, "");
- }
- /// <summary>
- /// 分块读取文件,若文件大于500MB,则每次只读500MB
- /// </summary>
- internal static FlieSpliceInfo ReadFileData(string filePath, long manualDivisionForUpload, int offsetCount)
- {
- bool isReadAllBytes = false;
- 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));
- if (partCount > 1)
- {
- var spliceSize = 500 * 1024 * 1024;
- int spliceCount = 0;
- if (fs.Length > spliceSize)//每次只读500M
- {
- long offsetSize = manualDivisionForUpload * offsetCount;
- var restSize = fs.Length - offsetSize;
- if (restSize > spliceSize)
- {
- byte[] btArr = new byte[spliceSize];
- fs.Seek(offsetSize, SeekOrigin.Begin);
- fs.Read(btArr, 0, btArr.Length);
- spliceCount = (int)(spliceSize / manualDivisionForUpload) + (spliceSize % manualDivisionForUpload > 0 ? 1 : 0);
- long offsetIndex = 0;
- for (var i = 0; i < spliceCount; i++)
- {
- var tempLength = manualDivisionForUpload;
- var tempByte = new byte[tempLength];
- offsetIndex = i * manualDivisionForUpload;
- Array.Copy(btArr, offsetIndex, tempByte, 0, tempByte.Length);
- dic.Add(i + offsetCount, tempByte);
- }
- }
- else
- {
- byte[] btArr = new byte[restSize];
- fs.Seek(offsetSize, SeekOrigin.Begin);
- fs.Read(btArr, 0, btArr.Length);
- spliceCount = (int)(restSize / manualDivisionForUpload) + (restSize % manualDivisionForUpload > 0 ? 1 : 0);
- long offsetIndex = 0;
- for (var i = 0; i < spliceCount; i++)
- {
- var tempLength = manualDivisionForUpload;
- if (spliceCount - 1 == i)
- {
- //最后一个分块File
- tempLength = btArr.Length - (spliceCount - 1) * manualDivisionForUpload;
- isReadAllBytes = true;
- }
- var tempByte = new byte[tempLength];
- offsetIndex = i * manualDivisionForUpload;
- Array.Copy(btArr, offsetIndex, tempByte, 0, tempByte.Length);
- dic.Add(i + offsetCount, tempByte);
- }
- }
- }
- 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;
- isReadAllBytes = true;
- }
- var tempByte = new byte[tempLength];
- offsetIndex = i * manualDivisionForUpload;
- Array.Copy(btArr, offsetIndex, tempByte, 0, tempByte.Length);
- dic.Add(i, tempByte);
- }
- }
- }
- }
- else
- {
- byte[] btArr = new byte[fs.Length];
- fs.Read(btArr, 0, btArr.Length);
- dic.Add(0, btArr);
- isReadAllBytes = true;
- }
- return new FlieSpliceInfo(partCount, dic, isReadAllBytes);
- }
- }
- /// <summary>
- /// 分块读取文件
- /// </summary>
- internal static 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
- {
- dic.Add(0, btArr);
- }
- }
- }
- return dic;
- }
- }
- }
|