UploadHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using Vinno.vCloud.Common.Storage.ObjectStorageInfo;
  9. namespace Vinno.vCloud.Disk.UFile
  10. {
  11. public class UploadHelper
  12. {
  13. private static StorageUploadConfig _cloudStorageConfig;
  14. public static void Init()
  15. {
  16. var ufileUploaderConfig =
  17. "{\n"
  18. + " \"UrlBucket\": \"" + "http://flyinsono-test.cn-bj.ufileos.com/" + "\",\n"
  19. + " \"StorageUrl\": \"\",\n"
  20. + " \"Heads\": {\n"
  21. + " \"UserAgent\": {\n"
  22. + " \"ProductInfoHeaderValue\": {\n"
  23. + " \"ProductName\": \"UCloudCSharp\",\n"
  24. + " \"Version\": \"1.0.2\"\n"
  25. + " }\n"
  26. + " },\n"
  27. + " \"RequestHeads\": [\n"
  28. + " {\n"
  29. + " \"HeadKey\": \"X-Ufile-Storage-Class\",\n"
  30. + " \"HeadValue\": \"Standard\"\n"
  31. + " }\n"
  32. + " ]\n"
  33. + " }\n"
  34. + "}\n";
  35. _cloudStorageConfig = JsonConvert.DeserializeObject<StorageUploadConfig>(ufileUploaderConfig);
  36. }
  37. /// <summary>
  38. /// 下载文件
  39. /// </summary>
  40. /// <param name="requestUrl">文件下载地址</param>
  41. /// <returns></returns>
  42. public static byte[] DownloadFile(string requestUrl)
  43. {
  44. return ObjectStorageSingle.Instance.GetFile(requestUrl);
  45. }
  46. /// <summary>
  47. /// 上传文件
  48. /// </summary>
  49. /// <param name="fileData"></param>
  50. /// <param name="requestUrl"></param>
  51. /// <param name="authorization"></param>
  52. /// <returns></returns>
  53. public static string UploadFile(byte[] fileData,string fileName, string mimeType, Action<double> progressCallback = null)
  54. {
  55. var contentType = MediaTypeHeaderValue.Parse(mimeType);
  56. var requestHeads = new Dictionary<string, string>();
  57. ProductInfoHeaderValue productInfoHeaderValue = null;
  58. var authorization = GetAuthorization(fileName);
  59. requestHeads.Add("Authorization", authorization);
  60. var requestUrl = _cloudStorageConfig.UrlBucket + fileName;
  61. var heads = _cloudStorageConfig.Heads;
  62. if (heads != null)
  63. {
  64. var httpHeads = heads.RequestHeads;
  65. foreach (var httpHead in httpHeads)
  66. {
  67. requestHeads.Add(httpHead.HeadKey, httpHead.HeadValue);
  68. }
  69. var userAgent = heads.UserAgent;
  70. if (userAgent != null)
  71. {
  72. var productInfoHeader = userAgent.ProductInfoHeaderValue;
  73. productInfoHeaderValue = new ProductInfoHeaderValue(productInfoHeader.ProductName, productInfoHeader.Version);
  74. }
  75. }
  76. var result = ObjectStorageSingle.Instance.UploadFile(fileData, contentType, requestUrl, requestHeads, productInfoHeaderValue, progressCallback);
  77. if (result)
  78. {
  79. return requestUrl;
  80. }
  81. return string.Empty;
  82. }
  83. /// <summary>
  84. /// 删除文件
  85. /// </summary>
  86. /// <param name="requestUrl"></param>
  87. /// <param name="authorization"></param>
  88. /// <returns></returns>
  89. public static bool DeleteFile(string fileName)
  90. {
  91. var authorization = GetAuthorization(fileName);
  92. var requestUrl = _cloudStorageConfig.UrlBucket + fileName;
  93. return ObjectStorageSingle.Instance.DeleteFile(requestUrl, authorization);
  94. }
  95. private static string GetAuthorization(string fileName)
  96. {
  97. var contentType = string.Empty;
  98. if (fileName.LastIndexOf(".") > -1)
  99. {
  100. contentType = HttpClientHelper.GetMimeType(fileName.Substring(fileName.LastIndexOf(".")));
  101. }
  102. var authorization = string.Empty;
  103. authorization += "UCloud ";
  104. authorization += "TOKEN_2840e067-0874-4c72-81d3-baa43c7d510d";
  105. authorization += ":";
  106. var stringToSign = HttpMethod.Put.Method + "\n";
  107. stringToSign += "\n";
  108. stringToSign += contentType;
  109. stringToSign += "\n";
  110. stringToSign += "\n";
  111. stringToSign += $"/flyinsono-test/" + fileName;
  112. var hmac = new HMACSHA1(Encoding.ASCII.GetBytes("fd099d8b-200c-4a37-9b91-27875b0340e5"));
  113. var hashValue = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign));
  114. var signature = Convert.ToBase64String(hashValue);
  115. return authorization + signature;
  116. }
  117. }
  118. }