OfficialController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Azure.Cosmos;
  3. using System.Text.Json;
  4. using TEAMModelOS.SDK.DI;
  5. using TEAMModelOS.SDK.Models;
  6. using TEAMModelOS.SDK;
  7. namespace HTEX.Complex.Controllers
  8. {
  9. [Route("official")]
  10. [ApiController]
  11. public class OfficialController : ControllerBase
  12. {
  13. private readonly DingDing _dingDing;
  14. private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
  15. private readonly AzureRedisFactory _azureRedis;
  16. private readonly AzureStorageFactory _azureStorage;
  17. private readonly IConfiguration _configuration;
  18. private readonly IPSearcher _searcher;
  19. public OfficialController(IConfiguration configuration,
  20. AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, System.Net.Http.IHttpClientFactory httpClientFactory,
  21. DingDing dingDing, IPSearcher searcher)
  22. {
  23. _dingDing = dingDing;
  24. _httpClientFactory = httpClientFactory;
  25. _azureRedis = azureRedis;
  26. _azureStorage = azureStorage;
  27. _configuration = configuration;
  28. _searcher = searcher;
  29. }
  30. [ProducesDefaultResponseType]
  31. [HttpPost("video-list")]
  32. public IActionResult VideoList(JsonElement json)
  33. {
  34. List<OfficialVideo> videos = new List<OfficialVideo>();
  35. var table = _azureStorage.TableServiceClient().GetTableClient("ShortUrl");
  36. if (json.TryGetProperty("rowKey", out JsonElement _rowKey) && !string.IsNullOrWhiteSpace($"{_rowKey}"))
  37. {
  38. videos = table.Query<OfficialVideo>($"{Constant.PartitionKey} {Constant.Equal} 'OfficialVideo' and {Constant.RowKey} {Constant.Equal} '{_rowKey}'").ToList();
  39. }
  40. else
  41. {
  42. videos = table.Query<OfficialVideo>(filter: $"{Constant.PartitionKey} eq 'OfficialVideo'").ToList();
  43. }
  44. return Ok(new { videos });
  45. }
  46. [ProducesDefaultResponseType]
  47. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  48. [HttpPost("video-upload")]
  49. public async Task<IActionResult> VideoUpload([FromForm] string name, [FromForm] string type, [FromForm] string? rowKey = null, [FromForm] IFormFile? videoFile = null)
  50. {
  51. OfficialVideo video = null;
  52. string url = string.Empty;
  53. var table = _azureStorage.TableServiceClient().GetTableClient("ShortUrl");
  54. if (videoFile == null)
  55. {
  56. if (!string.IsNullOrWhiteSpace(rowKey))
  57. {
  58. try
  59. {
  60. video=await table.GetEntityAsync<OfficialVideo>("OfficialVideo", rowKey);
  61. }
  62. catch { return BadRequest(); }
  63. }
  64. else
  65. {
  66. return BadRequest();
  67. }
  68. }
  69. else
  70. {
  71. if (!string.IsNullOrWhiteSpace(rowKey))
  72. {
  73. try
  74. {
  75. video=await table.GetEntityAsync<OfficialVideo>("OfficialVideo", rowKey);
  76. }
  77. catch { }
  78. }
  79. if (video== null)
  80. {
  81. video= new OfficialVideo { RowKey=$"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", PartitionKey="OfficialVideo" };
  82. }
  83. string fileExt = FileType.GetExtention(videoFile.FileName).ToLower();
  84. if (fileExt.Equals("mp4", StringComparison.OrdinalIgnoreCase))
  85. {
  86. var url_blob = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(videoFile.OpenReadStream(), "official", $"{video.RowKey}.{fileExt}", false);
  87. url=url_blob;
  88. }
  89. }
  90. video.name= name;
  91. video.type= type;
  92. video.url=string.IsNullOrWhiteSpace(url) ? video.url : url;
  93. table.UpsertEntity<OfficialVideo>(video);
  94. return Ok(new { video });
  95. }
  96. }
  97. //[TableName(Name = "ShortUrl")]
  98. public class OfficialVideo : HTableEntity
  99. {
  100. ///// <summary>
  101. ///// OfficialVideo
  102. ///// </summary>
  103. //public string PartitionKey { get; set; }
  104. ///// <summary>
  105. ///// 视频id
  106. ///// </summary>
  107. //public string RowKey { get; set; }
  108. /// <summary>
  109. /// 名称
  110. /// </summary>
  111. public string? name { get; set; }
  112. /// <summary>
  113. /// 类型
  114. /// </summary>
  115. public string? type { get; set; }
  116. /// <summary>
  117. /// 地址
  118. /// </summary>
  119. public string? url { get; set; }
  120. }
  121. }