CrazyIter_Bin 1 year ago
parent
commit
ef35b65bf8
1 changed files with 132 additions and 0 deletions
  1. 132 0
      TEAMModelOS/Controllers/System/OfficialController.cs

+ 132 - 0
TEAMModelOS/Controllers/System/OfficialController.cs

@@ -0,0 +1,132 @@
+using Lib.AspNetCore.ServerSentEvents;
+using Microsoft.AspNetCore.Cors;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Options;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.Filter;
+using TEAMModelOS.SDK.DI.Mail;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK;
+using Microsoft.Azure.Cosmos.Table;
+using TEAMModelOS.Models;
+using TEAMModelOS.SDK.Context.Attributes.Azure;
+using HTEXLib;
+using System.Collections.Generic;
+using Microsoft.AspNetCore.Http;
+using System;
+
+namespace TEAMModelOS.Controllers
+{
+    [Route("official")]
+    [ApiController]
+    public class OfficialController : ControllerBase
+    {
+
+        private readonly DingDing _dingDing;
+        private readonly SnowflakeId _snowflakeId;
+        private readonly ServerSentEventsService _sse;
+        private readonly AzureCosmosFactory _azureCosmosFactory;
+        private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
+        private readonly Models.Option _option;
+        private readonly MailFactory _mailFactory;
+        private readonly AzureRedisFactory _azureRedis;
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly IWebHostEnvironment _environment;
+        private readonly IConfiguration _configuration;
+        private readonly CoreAPIHttpService _coreAPIHttpService;
+        private readonly IPSearcher _searcher;
+        public OfficialController(IWebHostEnvironment environment, CoreAPIHttpService coreAPIHttpService, IConfiguration configuration, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, MailFactory mailFactory, System.Net.Http.IHttpClientFactory httpClientFactory,
+            IOptionsSnapshot<Option> option,  SnowflakeId snowflakeId,
+            DingDing dingDing, ServerSentEventsService sse, AzureCosmosFactory azureCosmosFactory, IPSearcher searcher)
+        {
+            _snowflakeId=snowflakeId;
+            _dingDing = dingDing;
+            _sse = sse;
+            _azureCosmosFactory = azureCosmosFactory;
+            _option = option?.Value;
+            _httpClientFactory = httpClientFactory;
+            _mailFactory=mailFactory;
+            _azureRedis = azureRedis;
+            _azureStorage = azureStorage;
+            _environment = environment;
+            _configuration = configuration;
+            _coreAPIHttpService = coreAPIHttpService;
+            _searcher = searcher;
+        }
+        [EnableCors("AllowSpecificOrigin")]
+        [ProducesDefaultResponseType]
+        [HttpPost("video-list")]
+        public async Task<IActionResult> VideoList(JsonElement json)
+        {
+            List<OfficialVideo> videos =new List<OfficialVideo>();
+            var table = _azureStorage.GetCloudTableClient().GetTableReference("ShortUrl");
+            if (json.TryGetProperty("rowKey", out JsonElement _rowKey) && !string.IsNullOrWhiteSpace($"{_rowKey}"))
+            {
+                videos = await table.FindListByDict<OfficialVideo>(new Dictionary<string, object> { { "PartitionKey", "OfficialVideo" } });
+            }
+            else {
+                videos = await table.FindListByDict<OfficialVideo>(new Dictionary<string, object> { { "PartitionKey", "OfficialVideo" } ,{ "RowKey",$"{_rowKey}" } });
+            }
+            return Ok(new { videos });
+        }
+        [EnableCors("AllowSpecificOrigin")]
+        [ProducesDefaultResponseType]
+        [RequestSizeLimit(102_400_000_00)] //最大10000m左右
+        [HttpPost("video-upload")]
+        public async Task<IActionResult> VideoUpload([FromForm] IFormFile videoFile, [FromForm] string Name,  [FromForm] string Type, [FromForm] string RowKey)
+        {
+            OfficialVideo video = null;
+            string fileExt = FileType.GetExtention(videoFile.FileName).ToLower();
+            if (fileExt.Equals("mp4", StringComparison.OrdinalIgnoreCase)) 
+            {
+               
+                var table = _azureStorage.GetCloudTableClient().GetTableReference("ShortUrl");
+                if (!string.IsNullOrWhiteSpace(RowKey))
+                {
+                    try
+                    {
+                        video= table.Get<OfficialVideo>(rowKey: RowKey, partitionKey: "OfficialVideo");
+                    }
+                    catch { }
+                }
+                if (video== null) {
+                    video= new OfficialVideo { RowKey=$"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", PartitionKey="OfficialVideo" };
+                }
+                var url = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(videoFile.OpenReadStream(), "official", $"{video.RowKey}.{fileExt}",false);
+                video.Name= Name;
+                video.Type= Type;
+                video.Url=url;
+               await table.SaveOrUpdate<OfficialVideo>(video);
+            }
+            return Ok(new { video });
+        }
+    }
+    [TableName(Name = "ShortUrl")]
+    public class OfficialVideo :TableEntity
+    {
+        ///// <summary>
+        ///// OfficialVideo
+        ///// </summary>
+        //public string PartitionKey { get; set; }
+        ///// <summary>
+        ///// 视频id
+        ///// </summary>
+        //public string RowKey { get; set; }
+        /// <summary>
+        /// 名称
+        /// </summary>
+        public string Name { get; set; }
+        /// <summary>
+        /// 类型
+        /// </summary>
+        public string Type { get; set; }
+        /// <summary>
+        /// 地址
+        /// </summary>
+        public string Url { get; set; }
+
+    }
+}