123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Context.Configuration;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using TEAMModelOS.SDK.Helper.Network.HttpHelper;
- using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
- using TEAMModelOS.SDK.DI;
- namespace TEAMModelOS.Controllers.Core
- {
- [Route("api/[controller]")]
- [ApiController]
- public class BlobController : BaseController
- {
-
- private readonly AzureStorageFactory azureBlobDBRepository;
- private readonly SnowflakeId SnowflakeId;
- public BlobController(AzureStorageFactory _azureBlobDBRepository, SnowflakeId _SnowflakeId) {
- azureBlobDBRepository = _azureBlobDBRepository;
- SnowflakeId = _SnowflakeId;
- }
- /// <summary>
- /// 获取某个容器的只读权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("blobSasR")]
- public BaseResponse BlobSasR(BlobSas request)
- {
- ///返回金钥过期时间
- ResponseBuilder builder = new ResponseBuilder();
- // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
- // dict.Add(d.Key, d.Value);
- return builder.Data( azureBlobDBRepository.GetContainerSasUri(request, true)).build() ;
- }
- /// <summary>
- /// 某个文件的上传SAS rcw权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("blobSasRCW")]
- public BaseResponse BlobSasRCW(BlobSas request)
- {
- ///返回金钥过期时间
- ResponseBuilder builder = new ResponseBuilder();
- // Dictionary<string,object> dict= await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
- // Dictionary<string, object> dict = ;
- //dict.Add(d.Key, d.Value);
- return builder.Data(azureBlobDBRepository.GetContainerSasUri(request, false)).build();
- }
- /// <summary>
- /// 链接只读(读)
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("urlSasR")]
- public BaseResponse GetContainerSASRead(string azureBlobSASDto)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- string azureBlobSAS = azureBlobSASDto;
- (string, string) a = BlobUrlString(azureBlobSAS);
- string ContainerName = a.Item1;
- string BlobName = a.Item2;
- bool flg = IsBlobName(BlobName);
- if (flg)
- {
- return responseBuilder.Data(azureBlobDBRepository.GetBlobSasUriRead(ContainerName, BlobName)).build();
- }
- else
- {
- return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
- };
- }
- /// <summary>
- /// 获取文件内容
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("getText")]
- public async Task<BaseResponse> GetText(string request)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- string azureBlobSAS = System.Web.HttpUtility.UrlDecode(request, Encoding.UTF8);
- (string, string) a = BlobUrlString(azureBlobSAS);
- string ContainerName = a.Item1;
- string BlobName = a.Item2;
- bool flg = IsBlobName(BlobName);
- if (flg)
- {
- BlobAuth blobAuth= azureBlobDBRepository.GetBlobSasUriRead(ContainerName, BlobName);
- string text= await HttpHelper.HttpGetAsync(blobAuth.url + blobAuth.sas);
- JsonElement json = text.FromApiJson<JsonElement>();
- return responseBuilder.Data(json).build();
- }
- else
- {
- return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
- };
- }
- /// <summary>
- /// 测试单个文本内容的上传
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("uploadText")]
- public async Task<BaseResponse> UploadText(string request)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- return responseBuilder.Data(await azureBlobDBRepository.UploadFileByContainer("hbcn", request, "exam", SnowflakeId.NextId() + ".json")).build();
-
- }
- private static string ContainerUrlString(string sasUrl)
- {
- ///移除http://
- sasUrl = sasUrl.Substring(8);
- string[] sasUrls = sasUrl.Split("/");
- string ContainerName;
- ContainerName = sasUrls[1].Clone().ToString();
- return ContainerName;
- }
- private static (string, string) BlobUrlString(string sasUrl)
- {
- sasUrl = sasUrl.Substring(8);
- string[] sasUrls = sasUrl.Split("/");
- string ContainerName;
- ContainerName = sasUrls[1].Clone().ToString();
- string item = sasUrls[0] + "/" + sasUrls[1] + "/";
- string blob = sasUrl.Replace(item, "");
- return (ContainerName, blob);
- }
- public static bool IsBlobName(string BlobName)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
- @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
- }
- }
- }
|