ApiStreamController.cs 14 KB

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