OfficialController.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using Azure;
  2. using Azure.Data.Tables;
  3. using HTEX.Complex.Models;
  4. using HTEX.Complex.Service;
  5. using HTEX.Complex.Service.AzureRedis;
  6. using HTEX.Complex.Service.AzureStorage;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Azure.Cosmos;
  9. using System.Text.Json;
  10. using TEAMModelOS.SDK;
  11. using TEAMModelOS.SDK.DI.AzureCosmos3;
  12. using TableEntity = Azure.Data.Tables.TableEntity;
  13. namespace HTEX.Complex.Controllers
  14. {
  15. [Route("official")]
  16. [ApiController]
  17. public class OfficialController : ControllerBase
  18. {
  19. private readonly DingDing _dingDing;
  20. // private readonly SnowflakeId _snowflakeId;
  21. // private readonly ServerSentEventsService _sse;
  22. private readonly AzureCosmos3Factory _azureCosmos3Factory;
  23. private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
  24. //private readonly Models.Option _option;
  25. // private readonly MailFactory _mailFactory;
  26. private readonly AzureRedisFactory _azureRedis;
  27. private readonly AzureStorageFactory _azureStorage;
  28. private readonly IWebHostEnvironment _environment;
  29. private readonly IConfiguration _configuration;
  30. // private readonly CoreAPIHttpService _coreAPIHttpService;
  31. private readonly IPSearcher _searcher;
  32. public OfficialController(IWebHostEnvironment environment, IConfiguration configuration,
  33. AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, System.Net.Http.IHttpClientFactory httpClientFactory, AzureCosmos3Factory azureCosmos3Factory,
  34. // IOptionsSnapshot<Option> option,
  35. DingDing dingDing, IPSearcher searcher)
  36. {
  37. _dingDing = dingDing;
  38. _azureCosmos3Factory = azureCosmos3Factory;
  39. _httpClientFactory = httpClientFactory;
  40. _azureRedis = azureRedis;
  41. _azureStorage = azureStorage;
  42. _environment = environment;
  43. _configuration = configuration;
  44. _searcher = searcher;
  45. }
  46. [ProducesDefaultResponseType]
  47. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  48. [HttpPost("video-ls")]
  49. public async Task<IActionResult> VideoUpload()
  50. {
  51. List<string> students = new List<string>();
  52. //var result= await _azureCosmos3Factory.GetCosmosClient().GetContainer("winteachos", Constant.Teacher)
  53. // .GetList<Teacher>( "select value c from c", "Base");
  54. await foreach (var item in _azureCosmos3Factory.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Student)
  55. .GetItemQueryIteratorList<string>(queryText: "select value c.id from c", requestOptions: new QueryRequestOptions {MaxItemCount=5, PartitionKey = new PartitionKey("Base-hbcn") }))
  56. {
  57. students.AddRange(item.list);
  58. }
  59. return Ok(students);
  60. }
  61. [ProducesDefaultResponseType]
  62. [HttpPost("video-list")]
  63. public async Task<IActionResult> VideoList(JsonElement json)
  64. {
  65. List<OfficialVideo> videos = new List<OfficialVideo>();
  66. var table = _azureStorage.TableServiceClient().GetTableClient("ShortUrl");
  67. if (json.TryGetProperty("rowKey", out JsonElement _rowKey) && !string.IsNullOrWhiteSpace($"{_rowKey}"))
  68. {
  69. videos = table.Query<OfficialVideo>($"{Constant.PartitionKey} {Constant.Equal} 'OfficialVideo' and {Constant.RowKey} {Constant.Equal} '{_rowKey}'").ToList();
  70. }
  71. else
  72. {
  73. videos = table.Query<OfficialVideo>(filter: $"{Constant.PartitionKey} eq 'OfficialVideo'").ToList();
  74. }
  75. return Ok(new { videos });
  76. }
  77. [ProducesDefaultResponseType]
  78. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  79. [HttpPost("video-upload")]
  80. public async Task<IActionResult> VideoUpload( [FromForm] string name, [FromForm] string type, [FromForm] string? rowKey=null, [FromForm] IFormFile? videoFile = null)
  81. {
  82. OfficialVideo video = null;
  83. string url = string.Empty;
  84. var table = _azureStorage.TableServiceClient().GetTableClient("ShortUrl");
  85. if (videoFile == null )
  86. {
  87. if (!string.IsNullOrWhiteSpace(rowKey))
  88. {
  89. try
  90. {
  91. video=await table.GetEntityAsync<OfficialVideo>("OfficialVideo", rowKey);
  92. }
  93. catch { return BadRequest(); }
  94. }
  95. else {
  96. return BadRequest();
  97. }
  98. }
  99. else {
  100. if (!string.IsNullOrWhiteSpace(rowKey))
  101. {
  102. try
  103. {
  104. video=await table.GetEntityAsync<OfficialVideo>("OfficialVideo", rowKey);
  105. }
  106. catch { }
  107. }
  108. if (video== null)
  109. {
  110. video= new OfficialVideo { RowKey=$"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", PartitionKey="OfficialVideo" };
  111. }
  112. string fileExt = FileType.GetExtention(videoFile.FileName).ToLower();
  113. if (fileExt.Equals("mp4", StringComparison.OrdinalIgnoreCase))
  114. {
  115. var url_blob = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(videoFile.OpenReadStream(), "official", $"{video.RowKey}.{fileExt}", false);
  116. url=url_blob.url;
  117. }
  118. }
  119. video.name= name;
  120. video.type= type;
  121. video.url=string.IsNullOrWhiteSpace(url)?video.url:url;
  122. table.UpsertEntity<OfficialVideo>(video);
  123. return Ok(new { video });
  124. }
  125. }
  126. //[TableName(Name = "ShortUrl")]
  127. public class OfficialVideo : HTableEntity
  128. {
  129. ///// <summary>
  130. ///// OfficialVideo
  131. ///// </summary>
  132. //public string PartitionKey { get; set; }
  133. ///// <summary>
  134. ///// 视频id
  135. ///// </summary>
  136. //public string RowKey { get; set; }
  137. /// <summary>
  138. /// 名称
  139. /// </summary>
  140. public string? name { get; set; }
  141. /// <summary>
  142. /// 类型
  143. /// </summary>
  144. public string?type { get; set; }
  145. /// <summary>
  146. /// 地址
  147. /// </summary>
  148. public string? url { get; set; }
  149. }
  150. public class Student : CosmosEntity
  151. {
  152. public string mail { get; set; }
  153. public string mobile { get; set; }
  154. public string country { get; set; }
  155. public string name { get; set; }
  156. public string picture { get; set; }
  157. public string schoolId { get; set; }
  158. public string pw { get; set; }
  159. public string salt { get; set; }
  160. public int year { get; set; }
  161. //座位号
  162. public string no { get; set; } //座位号
  163. public string irs { get; set; }
  164. //绑定班级Id
  165. public string classId { get; set; }
  166. //分组信息
  167. public string groupId { get; set; }
  168. public string groupName { get; set; }
  169. public string periodId { get; set; }
  170. /// <summary>
  171. /// 性别 M( male,男) F (female 女) N(secret 保密)
  172. /// </summary>
  173. public string gender { get; set; }
  174. //补充留级信息
  175. //0在校,1毕业
  176. public int graduate { get; set; } = 0;
  177. /// <summary>
  178. /// 创建时间 十位 时间戳
  179. /// </summary>
  180. public long createTime { get; set; }
  181. /// <summary>
  182. /// 学生的专业id
  183. /// </summary>
  184. public string majorId { get; set; }
  185. /// <summary>
  186. /// 學生的OpenID (TW教育雲綁定ID)
  187. /// </summary>
  188. public string openId { get; set; }
  189. }
  190. /// <summary>
  191. /// 教师
  192. /// </summary>
  193. public class Teacher : CosmosEntity
  194. {
  195. public Teacher()
  196. {
  197. pk = "Teacher";
  198. }
  199. /// <summary>
  200. /// 系统权限信息
  201. /// </summary>
  202. public HashSet<string> permissions { get; set; } = new HashSet<string>();
  203. /// <summary>
  204. /// 组织信息
  205. /// </summary>
  206. //常用设备信息,及IP登录信息。
  207. }
  208. }