BlobController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 : ControllerBase
  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("getText")]
  91. public async Task<BaseResponse> GetText(JsonElement request)
  92. {
  93. request.TryGetProperty("code", out JsonElement code);
  94. ResponseBuilder responseBuilder = new ResponseBuilder();
  95. string azureBlobSAS = System.Web.HttpUtility.UrlDecode(code.ToString(), Encoding.UTF8);
  96. (string, string) a = BlobUrlString(azureBlobSAS);
  97. string ContainerName = a.Item1;
  98. string BlobName = a.Item2;
  99. bool flg = IsBlobName(BlobName);
  100. if (flg)
  101. {
  102. //TODO 需驗證
  103. BlobAuth blobAuth= _azureStorage.GetBlobSasUriRead(ContainerName, BlobName);
  104. var response= await _clientFactory.CreateClient().GetAsync(new Uri(blobAuth.url + blobAuth.sas));
  105. response.EnsureSuccessStatusCode();
  106. using var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
  107. return responseBuilder.Data(json.RootElement).build();
  108. }
  109. else
  110. {
  111. return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
  112. };
  113. }
  114. /// <summary>
  115. /// 测试单个文本内容的上传
  116. /// </summary>
  117. /// <param name="azureBlobSASDto"></param>
  118. /// <returns></returns>
  119. [HttpPost("deleteBlob")]
  120. public async Task<BaseResponse> DeleteBlob(JsonElement request)
  121. {
  122. ResponseBuilder responseBuilder = new ResponseBuilder();
  123. 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") });
  124. return responseBuilder.Data(client[0].Status).build();
  125. }
  126. /// <summary>
  127. /// 测试单个文本内容的上传
  128. /// </summary>
  129. /// <param name="azureBlobSASDto"></param>
  130. /// <returns></returns>
  131. [HttpPost("get-blobsize")]
  132. public async Task<ActionResult> GetBlobsSize(JsonElement request)
  133. {
  134. request.TryGetProperty("containerName", out JsonElement containerName);
  135. var name =containerName.GetString();
  136. var client = _azureStorage.GetBlobContainerClient(name);
  137. var size = await client.GetBlobsSize();
  138. return Ok(new { size });
  139. }
  140. /// <summary>
  141. /// 测试单个文本内容的上传
  142. /// </summary>
  143. /// <param name="azureBlobSASDto"></param>
  144. /// <returns></returns>
  145. [HttpPost("testjson")]
  146. public async Task<BaseResponse> Uploadtest(JsonElement request)
  147. {
  148. JsonElement json= await RedisHelper.CacheShellAsync("test:" + "test", "bbbbbbbbbbbbbbbbbbbbb", 3600, () => { return test(request); });
  149. ResponseBuilder responseBuilder = new ResponseBuilder();
  150. return responseBuilder.Data(RedisHelper.HGet<JsonElement>("test:" + "test", "aaaaaaaaaaaaaaaaa")).build();
  151. }
  152. private Task<JsonElement> test(JsonElement request)
  153. {
  154. return Task.FromResult(request);
  155. }
  156. private static string ContainerUrlString(string sasUrl)
  157. {
  158. ///移除http://
  159. sasUrl = sasUrl.Substring(8);
  160. string[] sasUrls = sasUrl.Split("/");
  161. string ContainerName;
  162. ContainerName = sasUrls[1].Clone().ToString();
  163. return ContainerName;
  164. }
  165. private static (string, string) BlobUrlString(string sasUrl)
  166. {
  167. sasUrl = sasUrl.Substring(8);
  168. string[] sasUrls = sasUrl.Split("/");
  169. string ContainerName;
  170. ContainerName = sasUrls[1].Clone().ToString();
  171. string item = sasUrls[0] + "/" + sasUrls[1] + "/";
  172. string blob = sasUrl.Replace(item, "");
  173. return (ContainerName, blob);
  174. }
  175. public static bool IsBlobName(string BlobName)
  176. {
  177. return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
  178. @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
  179. }
  180. }
  181. }