using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Cosmos; using System.Text.Json; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK; namespace HTEX.Complex.Controllers { [Route("official")] [ApiController] public class OfficialController : ControllerBase { private readonly DingDing _dingDing; private readonly System.Net.Http.IHttpClientFactory _httpClientFactory; private readonly AzureRedisFactory _azureRedis; private readonly AzureStorageFactory _azureStorage; private readonly IConfiguration _configuration; private readonly IPSearcher _searcher; public OfficialController(IConfiguration configuration, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, System.Net.Http.IHttpClientFactory httpClientFactory, DingDing dingDing, IPSearcher searcher) { _dingDing = dingDing; _httpClientFactory = httpClientFactory; _azureRedis = azureRedis; _azureStorage = azureStorage; _configuration = configuration; _searcher = searcher; } [ProducesDefaultResponseType] [HttpPost("video-list")] public IActionResult VideoList(JsonElement json) { List videos = new List(); var table = _azureStorage.TableServiceClient().GetTableClient("ShortUrl"); if (json.TryGetProperty("rowKey", out JsonElement _rowKey) && !string.IsNullOrWhiteSpace($"{_rowKey}")) { videos = table.Query($"{Constant.PartitionKey} {Constant.Equal} 'OfficialVideo' and {Constant.RowKey} {Constant.Equal} '{_rowKey}'").ToList(); } else { videos = table.Query(filter: $"{Constant.PartitionKey} eq 'OfficialVideo'").ToList(); } return Ok(new { videos }); } [ProducesDefaultResponseType] [RequestSizeLimit(102_400_000_00)] //最大10000m左右 [HttpPost("video-upload")] public async Task VideoUpload([FromForm] string name, [FromForm] string type, [FromForm] string? rowKey = null, [FromForm] IFormFile? videoFile = null) { OfficialVideo video = null; string url = string.Empty; var table = _azureStorage.TableServiceClient().GetTableClient("ShortUrl"); if (videoFile == null) { if (!string.IsNullOrWhiteSpace(rowKey)) { try { video=await table.GetEntityAsync("OfficialVideo", rowKey); } catch { return BadRequest(); } } else { return BadRequest(); } } else { if (!string.IsNullOrWhiteSpace(rowKey)) { try { video=await table.GetEntityAsync("OfficialVideo", rowKey); } catch { } } if (video== null) { video= new OfficialVideo { RowKey=$"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", PartitionKey="OfficialVideo" }; } string fileExt = FileType.GetExtention(videoFile.FileName).ToLower(); if (fileExt.Equals("mp4", StringComparison.OrdinalIgnoreCase)) { var url_blob = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(videoFile.OpenReadStream(), "official", $"{video.RowKey}.{fileExt}", false); url=url_blob; } } video.name= name; video.type= type; video.url=string.IsNullOrWhiteSpace(url) ? video.url : url; table.UpsertEntity(video); return Ok(new { video }); } } //[TableName(Name = "ShortUrl")] public class OfficialVideo : HTableEntity { ///// ///// OfficialVideo ///// //public string PartitionKey { get; set; } ///// ///// 视频id ///// //public string RowKey { get; set; } /// /// 名称 /// public string? name { get; set; } /// /// 类型 /// public string? type { get; set; } /// /// 地址 /// public string? url { get; set; } } }