Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

90 wiersze
4.4 KiB

  1. using BPA.KitChen.GroupMeal.Application.Service.SysService.Dtos;
  2. using BPA.KitChen.GroupMeal.Core.Common.Const;
  3. using BPA.KitChen.GroupMeal.SqlSugar;
  4. using COSXML;
  5. using COSXML.Auth;
  6. using COSXML.Model.Tag;
  7. using Furion;
  8. using Furion.DependencyInjection;
  9. using Furion.DynamicApiController;
  10. using Microsoft.AspNetCore.Mvc;
  11. using SqlSugar;
  12. using System.Text.RegularExpressions;
  13. namespace BPA.KitChen.GroupMeal.Application.Service.SysService
  14. {
  15. [ApiDescriptionSettings("系统管理", Tag = "系统", SplitCamelCase = false)]
  16. public class SysService : SupperRepository, IDynamicApiController, ITransient, ISysService
  17. {
  18. private readonly SqlSugarScope db;
  19. private COSXML.CosXml cosXml;
  20. public SysService()
  21. {
  22. db = SqlSugarDb.Db;
  23. CosXmlConfig config = new CosXmlConfig.Builder()
  24. .SetRegion(App.Configuration["cos_config:Region"]) // 设置默认的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
  25. .Build();
  26. string secretId = App.Configuration["cos_config:SecretId"]; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
  27. string secretKey = App.Configuration["cos_config:SecretKey"]; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
  28. long durationSecond = 6000; //每次请求签名有效时长,单位为秒
  29. QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
  30. secretKey, durationSecond);
  31. this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
  32. }
  33. [HttpPost("/api/SysService/GetPresignUploadUrl")]
  34. public FileOutInfoDto GetPresignUploadUrl(FileInputInfoDto inputDto)
  35. {
  36. // var cosConfig = CosConfig.cosInfoOptions;
  37. string GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  38. string baseUrl = "/Franchisee/";
  39. if (!string.IsNullOrWhiteSpace(GroupId))
  40. {
  41. //var Companycheck = db.Queryable<BPA_Company>().Where(x => x.Id == GroupId).First();
  42. baseUrl = baseUrl + GroupId + "/";
  43. }
  44. DateTime dt = DateTime.Now;
  45. baseUrl = baseUrl + inputDto.Directory + "/" + dt.ToFileTime().ToString() + "." + inputDto.FileExtension;
  46. try
  47. {
  48. PreSignatureStruct preSignatureStruct = new PreSignatureStruct();
  49. preSignatureStruct.appid = App.Configuration["cos_config:AppId"];//腾讯云账号 APPID
  50. preSignatureStruct.region = App.Configuration["cos_config:Region"]; //存储桶地域
  51. preSignatureStruct.bucket = App.Configuration["cos_config:Bucket"]; //存储桶
  52. preSignatureStruct.key = baseUrl; //对象键+".png"
  53. preSignatureStruct.httpMethod = "PUT"; //HTTP 请求方法
  54. preSignatureStruct.isHttps = true; //生成 HTTPS 请求 URL
  55. preSignatureStruct.signDurationSecond = 6000; //请求签名时间为 600s
  56. preSignatureStruct.headers = null;//签名中需要校验的 header
  57. preSignatureStruct.queryParameters = null; //签名中需要校验的 URL 中请求参数
  58. //上传预签名 URL (使用永久密钥方式计算的签名 URL)
  59. string requestSignURL = cosXml.GenerateSignURL(preSignatureStruct);
  60. string[] urls = requestSignURL.Split('?');
  61. Regex reg = new Regex("(http|https)://.+?(?=/)");
  62. Match match = reg.Match(requestSignURL);
  63. FileOutInfoDto fileOut = new FileOutInfoDto();
  64. fileOut.Domain = match.Value;
  65. fileOut.FileUrl = urls[0].Replace(fileOut.Domain, "/cos");
  66. fileOut.Param = urls[1];
  67. fileOut.AllUrl = requestSignURL;
  68. fileOut.SeeUrl = urls[0];
  69. return fileOut;
  70. }
  71. catch (COSXML.CosException.CosClientException clientEx)
  72. {
  73. //请求失败
  74. // Console.WriteLine("CosClientException: " + clientEx);
  75. }
  76. catch (COSXML.CosException.CosServerException serverEx)
  77. {
  78. //请求失败
  79. //Console.WriteLine("CosServerException: " + serverEx.GetInfo());
  80. }
  81. return null;
  82. }
  83. }
  84. }