BlobController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.Helper.Network.HttpHelper;
  13. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  14. using TEAMModelOS.SDK.DI;
  15. namespace TEAMModelOS.Controllers.Core
  16. {
  17. [Route("api/[controller]")]
  18. [ApiController]
  19. public class BlobController : BaseController
  20. {
  21. private readonly AzureStorageFactory azureBlobDBRepository;
  22. private readonly SnowflakeId SnowflakeId;
  23. public BlobController(AzureStorageFactory _azureBlobDBRepository, SnowflakeId _SnowflakeId) {
  24. azureBlobDBRepository = _azureBlobDBRepository;
  25. SnowflakeId = _SnowflakeId;
  26. }
  27. /// <summary>
  28. /// 获取某个容器的只读权限
  29. /// </summary>
  30. /// <param name="request"></param>
  31. /// <returns></returns>
  32. [HttpPost("blobSasR")]
  33. public BaseResponse BlobSasR(BlobSas request)
  34. {
  35. ///返回金钥过期时间
  36. ResponseBuilder builder = new ResponseBuilder();
  37. // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
  38. // dict.Add(d.Key, d.Value);
  39. return builder.Data( azureBlobDBRepository.GetContainerSasUri(request, true)).build() ;
  40. }
  41. /// <summary>
  42. /// 某个文件的上传SAS rcw权限
  43. /// </summary>
  44. /// <param name="request"></param>
  45. /// <returns></returns>
  46. [HttpPost("blobSasRCW")]
  47. public BaseResponse BlobSasRCW(BlobSas request)
  48. {
  49. ///返回金钥过期时间
  50. ResponseBuilder builder = new ResponseBuilder();
  51. // Dictionary<string,object> dict= await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
  52. // Dictionary<string, object> dict = ;
  53. //dict.Add(d.Key, d.Value);
  54. return builder.Data(azureBlobDBRepository.GetContainerSasUri(request, false)).build();
  55. }
  56. /// <summary>
  57. /// 链接只读(读)
  58. /// </summary>
  59. /// <param name="azureBlobSASDto"></param>
  60. /// <returns></returns>
  61. [HttpPost("urlSasR")]
  62. public BaseResponse GetContainerSASRead(string azureBlobSASDto)
  63. {
  64. ResponseBuilder responseBuilder = new ResponseBuilder();
  65. string azureBlobSAS = azureBlobSASDto;
  66. (string, string) a = BlobUrlString(azureBlobSAS);
  67. string ContainerName = a.Item1;
  68. string BlobName = a.Item2;
  69. bool flg = IsBlobName(BlobName);
  70. if (flg)
  71. {
  72. return responseBuilder.Data(azureBlobDBRepository.GetBlobSasUriRead(ContainerName, BlobName)).build();
  73. }
  74. else
  75. {
  76. return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
  77. };
  78. }
  79. /// <summary>
  80. /// 获取文件内容
  81. /// </summary>
  82. /// <param name="azureBlobSASDto"></param>
  83. /// <returns></returns>
  84. [HttpPost("getText")]
  85. public async Task<BaseResponse> GetText(string request)
  86. {
  87. ResponseBuilder responseBuilder = new ResponseBuilder();
  88. string azureBlobSAS = System.Web.HttpUtility.UrlDecode(request, Encoding.UTF8);
  89. (string, string) a = BlobUrlString(azureBlobSAS);
  90. string ContainerName = a.Item1;
  91. string BlobName = a.Item2;
  92. bool flg = IsBlobName(BlobName);
  93. if (flg)
  94. {
  95. BlobAuth blobAuth= azureBlobDBRepository.GetBlobSasUriRead(ContainerName, BlobName);
  96. string text= await HttpHelper.HttpGetAsync(blobAuth.url + blobAuth.sas);
  97. JsonElement json = text.FromApiJson<JsonElement>();
  98. return responseBuilder.Data(json).build();
  99. }
  100. else
  101. {
  102. return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
  103. };
  104. }
  105. /// <summary>
  106. /// 测试单个文本内容的上传
  107. /// </summary>
  108. /// <param name="azureBlobSASDto"></param>
  109. /// <returns></returns>
  110. [HttpPost("uploadText")]
  111. public async Task<BaseResponse> UploadText(string request)
  112. {
  113. ResponseBuilder responseBuilder = new ResponseBuilder();
  114. return responseBuilder.Data(await azureBlobDBRepository.UploadFileByContainer("hbcn", request, "exam", SnowflakeId.NextId() + ".json")).build();
  115. }
  116. private static string ContainerUrlString(string sasUrl)
  117. {
  118. ///移除http://
  119. sasUrl = sasUrl.Substring(8);
  120. string[] sasUrls = sasUrl.Split("/");
  121. string ContainerName;
  122. ContainerName = sasUrls[1].Clone().ToString();
  123. return ContainerName;
  124. }
  125. private static (string, string) BlobUrlString(string sasUrl)
  126. {
  127. sasUrl = sasUrl.Substring(8);
  128. string[] sasUrls = sasUrl.Split("/");
  129. string ContainerName;
  130. ContainerName = sasUrls[1].Clone().ToString();
  131. string item = sasUrls[0] + "/" + sasUrls[1] + "/";
  132. string blob = sasUrl.Replace(item, "");
  133. return (ContainerName, blob);
  134. }
  135. public static bool IsBlobName(string BlobName)
  136. {
  137. return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
  138. @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
  139. }
  140. }
  141. }