BlobController.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Threading.Tasks;
  10. using TEAMModeBI.Filter;
  11. using TEAMModelOS.Models;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. using static TEAMModelOS.SDK.DI.AzureStorageBlobExtensions;
  16. namespace TEAMModeBI.Controllers.Core
  17. {
  18. [Route("blob")]
  19. [ApiController]
  20. public class BlobController : ControllerBase
  21. {
  22. private readonly AzureStorageFactory _azureStorage;
  23. private readonly IHttpClientFactory _clientFactory;
  24. private readonly AzureRedisFactory _azureRedis;
  25. private readonly AzureServiceBusFactory _serviceBus;
  26. private readonly DingDing _dingDing;
  27. private readonly Option _option;
  28. private readonly AzureCosmosFactory _azureCosmos;
  29. public IConfiguration _configuration { get; set; }
  30. public BlobController(AzureStorageFactory azureStorage, AzureServiceBusFactory serviceBus, IHttpClientFactory clientFactory, AzureRedisFactory azureRedis, IConfiguration configuration,
  31. DingDing dingDing,
  32. IOptionsSnapshot<Option> option, AzureCosmosFactory azureCosmos)
  33. {
  34. _azureStorage = azureStorage;
  35. _clientFactory = clientFactory;
  36. _serviceBus = serviceBus;
  37. _azureRedis = azureRedis;
  38. _configuration = configuration;
  39. _dingDing = dingDing;
  40. _option = option?.Value;
  41. _azureCosmos = azureCosmos;
  42. }
  43. /// <summary>
  44. /// BI顾问上传文件到指定的0-public
  45. /// </summary>
  46. /// <param name="file"></param>
  47. /// <returns></returns>
  48. [HttpPost("upload-public")]
  49. [AuthToken(Roles = "assist")]
  50. [RequestSizeLimit(102_400_000_00)]//最大10000m左右
  51. public async Task<IActionResult> UploadPublic([FromForm] IFormFile file)
  52. {
  53. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  54. string fileExt = FileType.GetExtention(file.FileName).ToLower();
  55. if (ContentTypeDict.dict.ContainsKey($".{fileExt}"))
  56. {
  57. var url = await _azureStorage.UploadFileByContainer("0-public", file.OpenReadStream(), "school", $"{Guid.NewGuid()}.{fileExt}", false);
  58. return Ok(new { url });
  59. }
  60. else
  61. {
  62. return BadRequest();
  63. }
  64. }
  65. }
  66. }