OfficialController.cs 8.0 KB

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