ApiStreamController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using AipGateway.API.Application;
  2. using AipGateway.API.Application.Interfaces.Services;
  3. using AipGateway.API.Domain.Models;
  4. using AipGateway.API.Domain.Models.Request;
  5. using AipGateway.API.Domain.Models.Response;
  6. using AipGateway.API.Services;
  7. using AipGateway.API.Services.Interfaces;
  8. using Azure;
  9. using Azure.Core;
  10. using FluentValidation.Validators;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Swashbuckle.AspNetCore.Annotations;
  13. using System.ComponentModel.DataAnnotations;
  14. using System.Diagnostics;
  15. namespace AipGateway.API.Controllers
  16. {
  17. [ApiController]
  18. [Route("/api/v1/stream")]
  19. [Produces("application/json")]
  20. public class ApiStreamController : BaseController
  21. {
  22. private readonly ILogger<ApiStreamController> _log;
  23. private readonly IApiStreamService _service;
  24. private readonly IApiAuthService _authService;
  25. public ApiStreamController(ILogger<ApiStreamController> log, IApiAuthService authService, IApiStreamService apiStreamService)
  26. {
  27. _log = log;
  28. _authService = authService;
  29. _service = apiStreamService;
  30. }
  31. [HttpPost("info")]
  32. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseInfo>))]
  33. public async Task<IResult> GetStreamInfo([FromBody] RequestStreamInfo req)
  34. {
  35. return await CreateResponseAsync(async () =>
  36. {
  37. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_INFO, req.apiKey, req);
  38. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  39. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_INFO);
  40. if (authError != 0)
  41. {
  42. throw ResponseApiKeyValidationError(HttpContext, authError);
  43. }
  44. var response = await _service.GetInfo(req);
  45. return Results.Ok(ResponseSuccess(HttpContext, response));
  46. });
  47. }
  48. [HttpPost("set-label")]
  49. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
  50. public async Task<IResult> SetStreamLabel([FromBody] RequestStreamSet req)
  51. {
  52. return await CreateResponseAsync(async () =>
  53. {
  54. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_LABEL, req.apiKey, req);
  55. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  56. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_SET_LABEL);
  57. if (authError != 0)
  58. {
  59. throw ResponseApiKeyValidationError(HttpContext, authError);
  60. }
  61. var response = await _service.SetLabel(req);
  62. return Results.Ok(ResponseSuccess(HttpContext, response));
  63. });
  64. }
  65. [HttpPost("set-labels")]
  66. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<ResponseStream>>))]
  67. public async Task<IResult> SetStreamLabels([FromBody] RequestMultiStreamSet req)
  68. {
  69. return await CreateResponseAsync(async () =>
  70. {
  71. Stopwatch sw = new Stopwatch();
  72. sw.Start();
  73. _log.LogInformation("*** Start SetStreamLabels: {0} EA.", req.streams.Count);
  74. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_LABELS, req.apiKey, req);
  75. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  76. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_SET_LABELS);
  77. if (authError != 0)
  78. {
  79. throw ResponseApiKeyValidationError(HttpContext, authError);
  80. }
  81. int ii = 0;
  82. int jobs = req.streams.Count;
  83. List<ResponseStream> result = new List<ResponseStream>();
  84. var tasks = new Task<ResponseStream>[jobs];
  85. foreach (var obj in req.streams)
  86. {
  87. RequestStreamSet reqSet = new RequestStreamSet {
  88. apiKey = req.apiKey,
  89. email = req.email,
  90. decryptKey = req.decryptKey,
  91. aipGuid = req.aipGuid,
  92. comment = req.comment,
  93. stream = new RequestStream
  94. {
  95. dispFileName = obj.dispFileName,
  96. fileData = obj.fileData,
  97. }
  98. };
  99. tasks[ii++] = _service.SetLabel(reqSet);
  100. }
  101. await Task.WhenAll(tasks);
  102. foreach (var task in tasks)
  103. {
  104. result.Add(task.Result);
  105. }
  106. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  107. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  108. sw.Stop();
  109. _log.LogInformation("*** ...End SetStreamLabels: {0} EA. {1,6} ms.", req.streams.Count, sw.ElapsedMilliseconds.ToString("#,##0"));
  110. return Results.Ok(new ApiResponseModel<List<ResponseStream>>()
  111. {
  112. success = true,
  113. errorCode = 0,
  114. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  115. result = result,
  116. });
  117. });
  118. }
  119. [HttpPost("delete-label")]
  120. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
  121. public async Task<IResult> DelStreamLabel([FromBody] RequestStreamDel req)
  122. {
  123. return await CreateResponseAsync(async () =>
  124. {
  125. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_LABELS, req.apiKey, req);
  126. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  127. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DELETE_LABELS);
  128. if (authError != 0)
  129. {
  130. throw ResponseApiKeyValidationError(HttpContext, authError);
  131. }
  132. var response = await _service.DelLabel(req);
  133. return Results.Ok(ResponseSuccess(HttpContext, response));
  134. });
  135. }
  136. [HttpPost("delete-labels")]
  137. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<ResponseStream>>))]
  138. public async Task<IResult> DelStreamLabels([FromBody] RequestMultiStreamDel req)
  139. {
  140. return await CreateResponseAsync(async () =>
  141. {
  142. Stopwatch sw = new Stopwatch();
  143. sw.Start();
  144. _log.LogInformation("*** Start DelStreamLabels: {0} EA.", req.streams.Count);
  145. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_LABEL, req.apiKey, req);
  146. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  147. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DELETE_LABEL);
  148. if (authError != 0)
  149. {
  150. throw ResponseApiKeyValidationError(HttpContext, authError);
  151. }
  152. int ii = 0;
  153. int jobs = req.streams.Count;
  154. List<ResponseStream> result = new List<ResponseStream>();
  155. var tasks = new Task<ResponseStream>[jobs];
  156. foreach (var obj in req.streams)
  157. {
  158. RequestStreamDel reqSet = new RequestStreamDel {
  159. apiKey = req.apiKey,
  160. email = req.email,
  161. decryptKey = req.decryptKey,
  162. comment = req.comment,
  163. stream = new RequestStream {
  164. dispFileName = obj.dispFileName,
  165. fileData = obj.fileData,
  166. }
  167. };
  168. tasks[ii++] = _service.DelLabel(reqSet);
  169. }
  170. await Task.WhenAll(tasks);
  171. foreach (var task in tasks)
  172. {
  173. result.Add(task.Result);
  174. }
  175. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  176. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  177. sw.Stop();
  178. _log.LogInformation("*** ...End DelStreamLabels: {0} EA. {1,6} ms.", req.streams.Count, sw.ElapsedMilliseconds.ToString("#,##0"));
  179. return Results.Ok(new ApiResponseModel<List<ResponseStream>>()
  180. {
  181. success = true,
  182. errorCode = 0,
  183. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  184. result = result,
  185. });
  186. });
  187. }
  188. [HttpPost("set-protection")]
  189. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
  190. public async Task<IResult> SetStreamProtection([FromBody] RequestStreamSet req)
  191. {
  192. return await CreateResponseAsync(async () =>
  193. {
  194. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_PROTECTION, req.apiKey, req);
  195. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  196. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_SET_PROTECTION);
  197. if (authError != 0)
  198. {
  199. throw ResponseApiKeyValidationError(HttpContext, authError);
  200. }
  201. var response = await _service.SetProtection(req);
  202. return Results.Ok(ResponseSuccess(HttpContext, response));
  203. });
  204. }
  205. [HttpPost("delete-protection")]
  206. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
  207. public async Task<IResult> DelStreamProtection([FromBody] RequestStreamDel req)
  208. {
  209. return await CreateResponseAsync(async () =>
  210. {
  211. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_PROTECTIN, req.apiKey, req);
  212. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  213. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DELETE_PROTECTIN);
  214. if (authError != 0)
  215. {
  216. throw ResponseApiKeyValidationError(HttpContext, authError);
  217. }
  218. var response = await _service.DelProtection(req);
  219. return Results.Ok(ResponseSuccess(HttpContext, response));
  220. });
  221. }
  222. [HttpPost("set-label-protection")]
  223. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
  224. public async Task<IResult> SetStreamLabelProtection([FromBody] RequestStreamAllSet req)
  225. {
  226. return await CreateResponseAsync(async () =>
  227. {
  228. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_LABEL_PROTECTION, req.apiKey, req);
  229. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  230. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_SET_LABEL_PROTECTION);
  231. if (authError != 0)
  232. {
  233. throw ResponseApiKeyValidationError(HttpContext, authError);
  234. }
  235. var response = await _service.SetLabelProtection(req);
  236. return Results.Ok(ResponseSuccess(HttpContext, response));
  237. });
  238. }
  239. [HttpPost("delete-label-protection")]
  240. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
  241. public async Task<IResult> DelStreamLabelProtection([FromBody] RequestStreamDel req)
  242. {
  243. return await CreateResponseAsync(async () =>
  244. {
  245. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_LABEL_PROTECTION, req.apiKey, req);
  246. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  247. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DELETE_LABEL_PROTECTION);
  248. if (authError != 0)
  249. {
  250. throw ResponseApiKeyValidationError(HttpContext, authError);
  251. }
  252. var response = await _service.DelLabelProtection(req);
  253. return Results.Ok(ResponseSuccess(HttpContext, response));
  254. });
  255. }
  256. [HttpPost("encrypt")]
  257. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
  258. public async Task<IResult> EncryptStream([Required] IFormFile file, [Required] string apiKey, [Required] string email)
  259. {
  260. return await CreateResponseAsync(async () =>
  261. {
  262. RequestBase req = new RequestBase
  263. {
  264. apiKey = apiKey,
  265. email = email,
  266. decryptKey = string.Empty,
  267. apiGuid = string.Empty
  268. };
  269. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_ENCRYPT, apiKey, req);
  270. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  271. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_ENCRYPT);
  272. if (authError != 0)
  273. {
  274. throw ResponseApiKeyValidationError(HttpContext, authError);
  275. }
  276. var response = await _service.EncryptFile(file, req);
  277. return Results.Ok(ResponseSuccess(HttpContext, response));
  278. });
  279. }
  280. [HttpPost("decrypt")]
  281. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
  282. public async Task<IResult> DecryptStream([Required] IFormFile file, [Required] string apiKey, [Required] string email)
  283. {
  284. return await CreateResponseAsync(async () =>
  285. {
  286. RequestBase req = new RequestBase
  287. {
  288. apiKey = apiKey,
  289. email = email,
  290. decryptKey = string.Empty,
  291. apiGuid = string.Empty
  292. };
  293. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DECRYPT, apiKey, req);
  294. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  295. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DECRYPT);
  296. if (authError != 0)
  297. {
  298. throw ResponseApiKeyValidationError(HttpContext, authError);
  299. }
  300. var response = await _service.DecryptFile(file, req);
  301. return Results.Ok(ResponseSuccess(HttpContext, response));
  302. });
  303. }
  304. }
  305. }