123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- 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;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using TEAMModelOS.SDK.Helper.Network.HttpHelper;
- using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
- using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
- namespace TEAMModelOS.Controllers.Core
- {
- [Route("api/[controller]")]
- [ApiController]
- public class BlobController : BaseController
- {
-
- private readonly IAzureBlobDBRepository azureBlobDBRepository;
- public BlobController(IAzureBlobDBRepository _azureBlobDBRepository) {
- azureBlobDBRepository = _azureBlobDBRepository;
- }
- /// <summary>
- /// 获取某个容器的只读权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("blobSasR")]
- public async Task<BaseResponse> BlobSasR(JosnRequest<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(await azureBlobDBRepository.GetContainerSasUri(request.@params, true)).build() ;
- }
- /// <summary>
- /// 某个文件的上传SAS rcw权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("blobSasRCW")]
- public async Task<BaseResponse> BlobSasRCW(JosnRequest<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(await azureBlobDBRepository.GetContainerSasUri(request.@params, false)).build();
- }
- /// <summary>
- /// 链接只读(读)
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("urlSasR")]
- public async Task<BaseResponse> GetContainerSASRead(JosnRequest<string> azureBlobSASDto)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- string azureBlobSAS = azureBlobSASDto.@params;
- (string, string) a = BlobUrlString(azureBlobSAS);
- string ContainerName = a.Item1;
- string BlobName = a.Item2;
- bool flg = IsBlobName(BlobName);
- if (flg)
- {
- return responseBuilder.Data(await 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(JosnRequest<string> request)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- string azureBlobSAS = System.Web.HttpUtility.UrlDecode(request.@params, Encoding.UTF8);
- (string, string) a = BlobUrlString(azureBlobSAS);
- string ContainerName = a.Item1;
- string BlobName = a.Item2;
- bool flg = IsBlobName(BlobName);
- if (flg)
- {
- BlobAuth blobAuth= await 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(JosnRequest<string> request)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- return responseBuilder.Data(await azureBlobDBRepository.UploadFileByContainer("hbcn",request.@params,"exam",Guid.NewGuid().ToString()+".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}$)");
- }
- }
- }
|