BlobController.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Configuration;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.Context.Configuration;
  9. using TEAMModelOS.SDK.Context.Exception;
  10. using TEAMModelOS.SDK;
  11. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  12. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  13. using TEAMModelOS.SDK.DI;
  14. using System.Net.Http;
  15. using TEAMModelOS.SDK.Helper.Security.ShaHash;
  16. using TEAMModelOS.SDK.Extension;
  17. namespace TEAMModelOS.Controllers.Core
  18. {
  19. [Route("api/[controller]")]
  20. [ApiController]
  21. public class BlobController : BaseController
  22. {
  23. private readonly AzureStorageFactory _azureStorage;
  24. private readonly SnowflakeId _snowflakeId;
  25. private readonly IHttpClientFactory _clientFactory;
  26. public BlobController(AzureStorageFactory azureStorage, SnowflakeId snowflakeId, IHttpClientFactory clientFactory)
  27. {
  28. _azureStorage = azureStorage;
  29. _snowflakeId = snowflakeId;
  30. _clientFactory = clientFactory;
  31. }
  32. /// <summary>
  33. /// 获取某个容器的只读权限
  34. /// </summary>
  35. /// <param name="request"></param>
  36. /// <returns></returns>
  37. [HttpPost("blobSasR")]
  38. public BaseResponse BlobSasR(BlobSas request)
  39. {
  40. ///返回金钥过期时间
  41. ResponseBuilder builder = new ResponseBuilder();
  42. // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
  43. // dict.Add(d.Key, d.Value);
  44. return builder.Data(_azureStorage.GetContainerSasUri(request, true)).build();
  45. }
  46. /// <summary>
  47. /// 某个文件的上传SAS rcw权限
  48. /// </summary>
  49. /// <param name="request"></param>
  50. /// <returns></returns>
  51. [HttpPost("blobSasRCW")]
  52. public BaseResponse BlobSasRCW(BlobSas request)
  53. {
  54. ///返回金钥过期时间
  55. ResponseBuilder builder = new ResponseBuilder();
  56. // Dictionary<string,object> dict= await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
  57. // Dictionary<string, object> dict = ;
  58. //dict.Add(d.Key, d.Value);
  59. return builder.Data(_azureStorage.GetContainerSasUri(request, false)).build();
  60. }
  61. /// <summary>
  62. /// 链接只读(读)
  63. /// </summary>
  64. /// <param name="azureBlobSASDto"></param>
  65. /// <returns></returns>
  66. [HttpPost("urlSasR")]
  67. public BaseResponse GetContainerSASRead(JsonElement azureBlobSASDto)
  68. {
  69. ResponseBuilder responseBuilder = new ResponseBuilder();
  70. azureBlobSASDto.TryGetProperty("params", out JsonElement azureBlobSAS);
  71. //string azureBlobSAS = azureBlobSASDto;
  72. (string, string) a = BlobUrlString(azureBlobSAS.ToString());
  73. string ContainerName = a.Item1;
  74. string BlobName = a.Item2;
  75. bool flg = IsBlobName(BlobName);
  76. if (flg)
  77. {
  78. return responseBuilder.Data(_azureStorage.GetBlobSasUriRead(ContainerName, BlobName)).build();
  79. }
  80. else
  81. {
  82. return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
  83. };
  84. }
  85. /// <summary>
  86. /// 测试单个文本内容的上传
  87. /// </summary>
  88. /// <param name="azureBlobSASDto"></param>
  89. /// <returns></returns>
  90. [HttpPost("uploadText")]
  91. public async Task<BaseResponse> UploadText(JsonElement request)
  92. {
  93. ResponseBuilder responseBuilder = new ResponseBuilder();
  94. return responseBuilder.Data(await _azureStorage.UploadFileByContainer("hbcn", request.ToJsonString(), "exam", _snowflakeId.NextId() + ".json")).build();
  95. }
  96. /// <summary>
  97. /// 获取文件内容
  98. /// </summary>
  99. /// <param name="azureBlobSASDto"></param>
  100. /// <returns></returns>
  101. [HttpPost("getText")]
  102. public async Task<BaseResponse> GetText(JsonElement request)
  103. {
  104. request.TryGetProperty("code", out JsonElement code);
  105. ResponseBuilder responseBuilder = new ResponseBuilder();
  106. string azureBlobSAS = System.Web.HttpUtility.UrlDecode(code.ToString(), Encoding.UTF8);
  107. (string, string) a = BlobUrlString(azureBlobSAS);
  108. string ContainerName = a.Item1;
  109. string BlobName = a.Item2;
  110. bool flg = IsBlobName(BlobName);
  111. if (flg)
  112. {
  113. //TODO 需驗證
  114. BlobAuth blobAuth= _azureStorage.GetBlobSasUriRead(ContainerName, BlobName);
  115. var response= await _clientFactory.CreateClient().GetAsync(new Uri(blobAuth.url + blobAuth.sas));
  116. response.EnsureSuccessStatusCode();
  117. using var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
  118. return responseBuilder.Data(json.RootElement).build();
  119. }
  120. else
  121. {
  122. return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
  123. };
  124. }
  125. /// <summary>
  126. /// 测试单个文本内容的上传
  127. /// </summary>
  128. /// <param name="azureBlobSASDto"></param>
  129. /// <returns></returns>
  130. [HttpPost("deleteBlob")]
  131. public async Task<BaseResponse> DeleteBlob(JsonElement request)
  132. {
  133. ResponseBuilder responseBuilder = new ResponseBuilder();
  134. var client = _azureStorage.GetBlobBatchClient().DeleteBlobs(new Uri[] {new Uri("https://teammodelstorage.blob.core.chinacloudapi.cn/ydzt/%E6%96%B0%E5%BB%BA%E6%96%87%E4%BB%B6%E5%A4%B9%2FAnimation.xml") });
  135. return responseBuilder.Data(client[0].Status).build();
  136. }
  137. /// <summary>
  138. /// 测试单个文本内容的上传
  139. /// </summary>
  140. /// <param name="azureBlobSASDto"></param>
  141. /// <returns></returns>
  142. [HttpPost("get-blobsize")]
  143. public async Task<ActionResult> GetBlobsSize(JsonElement request)
  144. {
  145. request.TryGetProperty("containerName", out JsonElement containerName);
  146. var name =containerName.GetString();
  147. var client = _azureStorage.GetBlobContainerClient(name);
  148. var size = await client.GetBlobsSize();
  149. return Ok(new { size });
  150. }
  151. /// <summary>
  152. /// 测试单个文本内容的上传
  153. /// </summary>
  154. /// <param name="azureBlobSASDto"></param>
  155. /// <returns></returns>
  156. [HttpPost("testjson")]
  157. public async Task<BaseResponse> Uploadtest(JsonElement request)
  158. {
  159. JsonElement json= await RedisHelper.CacheShellAsync("test:" + "test", "bbbbbbbbbbbbbbbbbbbbb", 3600, () => { return test(request); });
  160. ResponseBuilder responseBuilder = new ResponseBuilder();
  161. return responseBuilder.Data(RedisHelper.HGet<JsonElement>("test:" + "test", "aaaaaaaaaaaaaaaaa")).build();
  162. }
  163. private Task<JsonElement> test(JsonElement request)
  164. {
  165. return Task.FromResult(request);
  166. }
  167. private static string ContainerUrlString(string sasUrl)
  168. {
  169. ///移除http://
  170. sasUrl = sasUrl.Substring(8);
  171. string[] sasUrls = sasUrl.Split("/");
  172. string ContainerName;
  173. ContainerName = sasUrls[1].Clone().ToString();
  174. return ContainerName;
  175. }
  176. private static (string, string) BlobUrlString(string sasUrl)
  177. {
  178. sasUrl = sasUrl.Substring(8);
  179. string[] sasUrls = sasUrl.Split("/");
  180. string ContainerName;
  181. ContainerName = sasUrls[1].Clone().ToString();
  182. string item = sasUrls[0] + "/" + sasUrls[1] + "/";
  183. string blob = sasUrl.Replace(item, "");
  184. return (ContainerName, blob);
  185. }
  186. public static bool IsBlobName(string BlobName)
  187. {
  188. return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
  189. @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
  190. }
  191. }
  192. }