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 _log; private readonly IApiStreamService _service; private readonly IApiAuthService _authService; public ApiStreamController(ILogger log, IApiAuthService authService, IApiStreamService apiStreamService) { _log = log; _authService = authService; _service = apiStreamService; } [HttpPost("info")] [SwaggerResponse(200, type: typeof(ApiResponseModel))] public async Task 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))] public async Task 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>))] public async Task 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 result = new List(); var tasks = new Task[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>() { success = true, errorCode = 0, errorMessage = GlobalConstants.API_RESULT_SUCCESS, result = result, }); }); } [HttpPost("delete-label")] [SwaggerResponse(200, type: typeof(ApiResponseModel))] public async Task 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>))] public async Task 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 result = new List(); var tasks = new Task[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>() { success = true, errorCode = 0, errorMessage = GlobalConstants.API_RESULT_SUCCESS, result = result, }); }); } [HttpPost("set-protection")] [SwaggerResponse(200, type: typeof(ApiResponseModel))] public async Task 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))] public async Task 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))] public async Task 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))] public async Task 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))] public async Task 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))] public async Task 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)); }); } }