ApiFileController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using AipGateway.AIP;
  2. using AipGateway.API.Application;
  3. using AipGateway.API.Application.Interfaces.Services;
  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.Core;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Newtonsoft.Json;
  11. using Serilog;
  12. using Swashbuckle.AspNetCore.Annotations;
  13. using System.ComponentModel.DataAnnotations;
  14. using System.Diagnostics;
  15. using System.IO;
  16. namespace AipGateway.API.Controllers
  17. {
  18. [ApiController]
  19. [Route("/api/v1/file")]
  20. [Produces("application/json")]
  21. public class ApiFileController : BaseController
  22. {
  23. private readonly ILogger<ApiFileController> _log;
  24. private readonly IApiFileService _service;
  25. private readonly IApiAuthService _authService;
  26. public ApiFileController(ILogger<ApiFileController> log, IApiAuthService authService, IApiFileService apiFileService)
  27. {
  28. _log = log;
  29. _authService = authService;
  30. _service = apiFileService;
  31. }
  32. [HttpPost("info")]
  33. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseInfo>))]
  34. public async Task<IResult> GetFileInfo([FromBody] RequestFileInfo req)
  35. {
  36. return await CreateResponseAsync(async () =>
  37. {
  38. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_INFO, req.apiKey, req);
  39. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  40. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_INFO);
  41. if (authError != 0)
  42. {
  43. throw ResponseApiKeyValidationError(HttpContext, authError);
  44. }
  45. var response = await _service.GetInfo(req);
  46. return Results.Ok(ResponseSuccess(HttpContext, response));
  47. });
  48. }
  49. [HttpPost("set-label")]
  50. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  51. public async Task<IResult> SetFileLabel([FromBody] RequestFileSet req)
  52. {
  53. return await CreateResponseAsync(async () =>
  54. {
  55. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_SET_LABEL, req.apiKey, req);
  56. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  57. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_SET_LABEL);
  58. if (authError != 0)
  59. {
  60. throw ResponseApiKeyValidationError(HttpContext, authError);
  61. }
  62. var response = await _service.SetLabel(req);
  63. return Results.Ok(ResponseSuccess(HttpContext, response));
  64. });
  65. }
  66. [HttpPost("set-labels")]
  67. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<ResponseFile>>))]
  68. public async Task<IResult> SetFileLabels([FromBody] RequestMultiFileSet req)
  69. {
  70. //await _service.SetAipFileLabels(req);
  71. //return Results.Ok();
  72. #if true
  73. return await CreateResponseAsync(async () =>
  74. {
  75. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_SET_LABELS, req.apiKey, req);
  76. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  77. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_SET_LABELS);
  78. if (authError != 0)
  79. {
  80. throw ResponseApiKeyValidationError(HttpContext, authError);
  81. }
  82. int jobs = req.files.Count;
  83. List<ResponseFile> result = new List<ResponseFile>();
  84. var tasks = new List<Task<ResponseFile>>();
  85. foreach (var obj in req.files)
  86. {
  87. RequestFileSet reqSet = new RequestFileSet
  88. {
  89. apiKey = req.apiKey,
  90. email = req.email,
  91. decryptKey = req.decryptKey,
  92. aipGuid = req.aipGuid,
  93. comment = req.comment,
  94. file = new RequestFile
  95. {
  96. dispFileName = obj.dispFileName,
  97. realFileName = obj.realFileName,
  98. }
  99. };
  100. tasks.Add(_service.SetLabel(reqSet));
  101. }
  102. await Task.WhenAll(tasks);
  103. foreach (var task in tasks)
  104. {
  105. result.Add(task.Result);
  106. }
  107. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  108. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  109. return Results.Ok(new ApiResponseModel<List<ResponseFile>>()
  110. {
  111. success = true,
  112. errorCode = 0,
  113. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  114. result = result,
  115. });
  116. });
  117. #endif
  118. #if false
  119. return await CreateResponseAsync(async () =>
  120. {
  121. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_SET_LABELS, req.apiKey, req);
  122. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  123. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_SET_LABELS);
  124. if (authError != 0)
  125. {
  126. throw ResponseApiKeyValidationError(HttpContext, authError);
  127. }
  128. var result = await _service.SetLabels(req);
  129. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  130. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  131. return Results.Ok(new ApiResponseModel<List<ResponseFile>>()
  132. {
  133. success = true,
  134. errorCode = 0,
  135. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  136. result = result,
  137. });
  138. });
  139. #endif
  140. }
  141. [HttpPost("delete-label")]
  142. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  143. public async Task<IResult> DelFileLabel([FromBody] RequestFileDel req)
  144. {
  145. return await CreateResponseAsync(async () =>
  146. {
  147. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_DELETE_LABEL, req.apiKey, req);
  148. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  149. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_DELETE_LABEL);
  150. if (authError != 0)
  151. {
  152. throw ResponseApiKeyValidationError(HttpContext, authError);
  153. }
  154. var response = await _service.DelLabel(req);
  155. return Results.Ok(ResponseSuccess(HttpContext, response));
  156. });
  157. }
  158. [HttpPost("delete-labels")]
  159. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  160. public async Task<IResult> DelFileLabels([FromBody] RequestMultiFileDel req)
  161. {
  162. return await CreateResponseAsync(async () =>
  163. {
  164. Stopwatch sw = new Stopwatch();
  165. sw.Start();
  166. _log.LogInformation("*** Start DelFileLabels: {0} EA.", req.files.Count);
  167. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_DELETE_LABELS, req.apiKey, req);
  168. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  169. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_DELETE_LABELS);
  170. if (authError != 0)
  171. {
  172. throw ResponseApiKeyValidationError(HttpContext, authError);
  173. }
  174. int ii = 0;
  175. int jobs = req.files.Count;
  176. List<ResponseFile> result = new List<ResponseFile>();
  177. var tasks = new Task<ResponseFile>[jobs];
  178. foreach (var obj in req.files)
  179. {
  180. RequestFileDel reqSet = new RequestFileDel
  181. {
  182. apiKey = req.apiKey,
  183. email = req.email,
  184. decryptKey = req.decryptKey,
  185. comment = req.comment,
  186. file = new RequestFile
  187. {
  188. dispFileName = obj.dispFileName,
  189. realFileName = obj.realFileName,
  190. }
  191. };
  192. tasks[ii++] = _service.DelLabel(reqSet);
  193. }
  194. await Task.WhenAll(tasks);
  195. foreach (var task in tasks)
  196. {
  197. result.Add(task.Result);
  198. }
  199. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  200. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  201. sw.Stop();
  202. _log.LogInformation("*** ...End SetFileLabels: {0} EA. {1,6} ms.", req.files.Count, sw.ElapsedMilliseconds.ToString("#,##0"));
  203. return Results.Ok(new ApiResponseModel<List<ResponseFile>>()
  204. {
  205. success = true,
  206. errorCode = 0,
  207. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  208. result = result,
  209. });
  210. });
  211. }
  212. [HttpPost("set-protection")]
  213. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  214. public async Task<IResult> SetFileProtection([FromBody] RequestFileSet req)
  215. {
  216. return await CreateResponseAsync(async () =>
  217. {
  218. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_SET_PROTECTION, req.apiKey, req);
  219. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  220. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_SET_PROTECTION);
  221. if (authError != 0)
  222. {
  223. throw ResponseApiKeyValidationError(HttpContext, authError);
  224. }
  225. var response = await _service.SetProtection(req);
  226. return Results.Ok(ResponseSuccess(HttpContext, response));
  227. });
  228. }
  229. [HttpPost("delete-protection")]
  230. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  231. public async Task<IResult> DelFileProtection([FromBody] RequestFileDel req)
  232. {
  233. return await CreateResponseAsync(async () =>
  234. {
  235. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_DELETE_PROTECTIN, req.apiKey, req);
  236. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  237. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_DELETE_PROTECTIN);
  238. if (authError != 0)
  239. {
  240. throw ResponseApiKeyValidationError(HttpContext, authError);
  241. }
  242. var response = await _service.DelProtection(req);
  243. return Results.Ok(ResponseSuccess(HttpContext, response));
  244. });
  245. }
  246. [HttpPost("set-label-protection")]
  247. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  248. public async Task<IResult> SetFileLabelProtection([FromBody] RequestFileAllSet req)
  249. {
  250. return await CreateResponseAsync(async () =>
  251. {
  252. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_SET_LABEL_PROTECTION, req.apiKey, req);
  253. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  254. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_SET_LABEL_PROTECTION);
  255. if (authError != 0)
  256. {
  257. throw ResponseApiKeyValidationError(HttpContext, authError);
  258. }
  259. var response = await _service.SetLabelProtection(req);
  260. return Results.Ok(ResponseSuccess(HttpContext, response));
  261. });
  262. }
  263. [HttpPost("delete-label-protection")]
  264. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  265. public async Task<IResult> DelFileLabelProtection([FromBody] RequestFileDel req)
  266. {
  267. return await CreateResponseAsync(async () =>
  268. {
  269. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_DELETE_LABEL_PROTECTION, req.apiKey, req);
  270. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  271. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_DELETE_LABEL_PROTECTION);
  272. if (authError != 0)
  273. {
  274. throw ResponseApiKeyValidationError(HttpContext, authError);
  275. }
  276. var response = await _service.DelLabelProtection(req);
  277. return Results.Ok(ResponseSuccess(HttpContext, response));
  278. });
  279. }
  280. [HttpPost("encrypt")]
  281. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  282. public async Task<IResult> EncryptFile([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_FILE_ENCRYPT, apiKey, req);
  294. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  295. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_ENCRYPT);
  296. if (authError != 0)
  297. {
  298. throw ResponseApiKeyValidationError(HttpContext, authError);
  299. }
  300. var response = await _service.EncryptFile(file, req);
  301. return Results.Ok(ResponseSuccess(HttpContext, response));
  302. });
  303. }
  304. [HttpPost("decrypt")]
  305. [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseFile>))]
  306. public async Task<IResult> DecryptFile([Required] IFormFile file, [Required] string apiKey, [Required] string email)
  307. {
  308. return await CreateResponseAsync(async () =>
  309. {
  310. RequestBase req = new RequestBase
  311. {
  312. apiKey = apiKey,
  313. email = email,
  314. decryptKey = string.Empty,
  315. apiGuid = string.Empty
  316. };
  317. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_DECRYPT, apiKey, req);
  318. HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
  319. int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_FILE_DECRYPT);
  320. if (authError != 0)
  321. {
  322. throw ResponseApiKeyValidationError(HttpContext, authError);
  323. }
  324. var response = await _service.DecryptFile(file, req);
  325. return Results.Ok(ResponseSuccess(HttpContext, response));
  326. });
  327. }
  328. }
  329. }