123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
-
- using Aip.Service.Models.Request;
- using Aip.Service.Models.Response;
- using Aip.Service.Repositories;
- using Aip.Service.Services.Interfaces;
- using Microsoft.AspNetCore.Mvc;
- using Swashbuckle.AspNetCore.Annotations;
- using System.ComponentModel.DataAnnotations;
- using System.Diagnostics;
- namespace Aip.Service.Controllers;
- [ApiController]
- [Route("/api/v1/stream")]
- [Produces("application/json")]
- public class ApiStreamController : BaseController
- {
- private readonly ILogger<ApiStreamController> _log;
- private readonly IApiStreamService _service;
- private readonly IApiAuthService _authService;
- public ApiStreamController(ILogger<ApiStreamController> log, IApiAuthService authService, IApiStreamService apiStreamService)
- {
- _log = log;
- _authService = authService;
- _service = apiStreamService;
- }
- [HttpPost("info")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseInfo>))]
- public async Task<IResult> GetStreamInfo([FromBody] RequestStreamInfo req)
- {
- return await CreateResponseAsync(async () =>
- {
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_INFO, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_INFO);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.GetInfo(req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
- [HttpPost("set-label")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
- public async Task<IResult> SetStreamLabel([FromBody] RequestStreamSet req)
- {
- return await CreateResponseAsync(async () =>
- {
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_LABEL, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_SET_LABEL);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.SetLabel(req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
- [HttpPost("set-labels")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<List<ResponseStream>>))]
- public async Task<IResult> SetStreamLabels([FromBody] RequestMultiStreamSet req)
- {
- return await CreateResponseAsync(async () =>
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- _log.LogInformation("*** Start SetStreamLabels: {0} EA.", req.streams.Count);
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_LABELS, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_SET_LABELS);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- int ii = 0;
- int jobs = req.streams.Count;
- List<ResponseStream> result = new List<ResponseStream>();
- var tasks = new Task<ResponseStream>[jobs];
- foreach (var obj in req.streams)
- {
- RequestStreamSet reqSet = new RequestStreamSet {
- apiKey = req.apiKey,
- email = req.email,
- decryptKey = req.decryptKey,
- aipGuid = req.aipGuid,
- comment = req.comment,
- stream = new RequestStream
- {
- dispFileName = obj.dispFileName,
- fileData = obj.fileData,
- }
- };
- tasks[ii++] = _service.SetLabel(reqSet);
- }
- await Task.WhenAll(tasks);
- foreach (var task in tasks)
- {
- result.Add(task.Result);
- }
- HttpContext.Items[GlobalConstants.API_RESULT_CODE] = GlobalConstants.API_RESULT_SUCCESS_CODE;
- HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
- sw.Stop();
- _log.LogInformation("*** ...End SetStreamLabels: {0} EA. {1,6} ms.", req.streams.Count, sw.ElapsedMilliseconds.ToString("#,##0"));
- return Results.Ok(new ApiResponseModel<List<ResponseStream>>()
- {
- success = true,
- errorCode = 0,
- errorMessage = GlobalConstants.API_RESULT_SUCCESS,
- result = result,
- });
- });
- }
- [HttpPost("delete-label")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
- public async Task<IResult> DelStreamLabel([FromBody] RequestStreamDel req)
- {
- return await CreateResponseAsync(async () =>
- {
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_LABELS, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DELETE_LABELS);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.DelLabel(req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
-
- [HttpPost("delete-labels")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<List<ResponseStream>>))]
- public async Task<IResult> DelStreamLabels([FromBody] RequestMultiStreamDel req)
- {
- return await CreateResponseAsync(async () =>
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- _log.LogInformation("*** Start DelStreamLabels: {0} EA.", req.streams.Count);
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_LABEL, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DELETE_LABEL);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- int ii = 0;
- int jobs = req.streams.Count;
- List<ResponseStream> result = new List<ResponseStream>();
- var tasks = new Task<ResponseStream>[jobs];
- foreach (var obj in req.streams)
- {
- RequestStreamDel reqSet = new RequestStreamDel {
- apiKey = req.apiKey,
- email = req.email,
- decryptKey = req.decryptKey,
- comment = req.comment,
- stream = new RequestStream {
- dispFileName = obj.dispFileName,
- fileData = obj.fileData,
- }
- };
- tasks[ii++] = _service.DelLabel(reqSet);
- }
- await Task.WhenAll(tasks);
- foreach (var task in tasks)
- {
- result.Add(task.Result);
- }
- HttpContext.Items[GlobalConstants.API_RESULT_CODE] = GlobalConstants.API_RESULT_SUCCESS_CODE;
- HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
- sw.Stop();
- _log.LogInformation("*** ...End DelStreamLabels: {0} EA. {1,6} ms.", req.streams.Count, sw.ElapsedMilliseconds.ToString("#,##0"));
- return Results.Ok(new ApiResponseModel<List<ResponseStream>>()
- {
- success = true,
- errorCode = 0,
- errorMessage = GlobalConstants.API_RESULT_SUCCESS,
- result = result,
- });
- });
- }
- [HttpPost("set-protection")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
- public async Task<IResult> SetStreamProtection([FromBody] RequestStreamSet req)
- {
- return await CreateResponseAsync(async () =>
- {
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_PROTECTION, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_SET_PROTECTION);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.SetProtection(req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
- [HttpPost("delete-protection")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
- public async Task<IResult> DelStreamProtection([FromBody] RequestStreamDel req)
- {
- return await CreateResponseAsync(async () =>
- {
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_PROTECTIN, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DELETE_PROTECTIN);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.RemoveProtection(req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
- [HttpPost("set-label-protection")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
- public async Task<IResult> SetStreamLabelProtection([FromBody] RequestStreamAllSet req)
- {
- return await CreateResponseAsync(async () =>
- {
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_SET_LABEL_PROTECTION, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_SET_LABEL_PROTECTION);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.SetLabelProtection(req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
- [HttpPost("delete-label-protection")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
- public async Task<IResult> DelStreamLabelProtection([FromBody] RequestStreamDel req)
- {
- return await CreateResponseAsync(async () =>
- {
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DELETE_LABEL_PROTECTION, req.apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DELETE_LABEL_PROTECTION);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.RemoveLabelProtection(req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
- [HttpPost("encrypt")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
- public async Task<IResult> EncryptStream([Required] IFormFile file, [Required] string apiKey, [Required] string email)
- {
- return await CreateResponseAsync(async () =>
- {
- RequestBase req = new RequestBase
- {
- apiKey = apiKey,
- email = email,
- decryptKey = string.Empty,
- apiGuid = string.Empty
- };
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_ENCRYPT, apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_ENCRYPT);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.EncryptFile(file, req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
- [HttpPost("decrypt")]
- [SwaggerResponse(200, type: typeof(ApiResponseModel<ResponseStream>))]
- public async Task<IResult> DecryptStream([Required] IFormFile file, [Required] string apiKey, [Required] string email)
- {
- return await CreateResponseAsync(async () =>
- {
- RequestBase req = new RequestBase
- {
- apiKey = apiKey,
- email = email,
- decryptKey = string.Empty,
- apiGuid = string.Empty
- };
- GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_STREAM_DECRYPT, apiKey, req);
- HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
- int authError = _authService.CheckApiKeyValidation(HttpContext, req.apiKey, GlobalConstants.API_STREAM_DECRYPT);
- if (authError != 0)
- {
- throw ResponseApiKeyValidationError(HttpContext, authError);
- }
- var response = await _service.DecryptFile(file, req);
- return Results.Ok(ResponseSuccess(HttpContext, response));
- });
- }
- }
|