diff --git a/Controllers/FileManagerController.cs b/Controllers/FileManagerController.cs index 1bf26a9..5167d63 100644 --- a/Controllers/FileManagerController.cs +++ b/Controllers/FileManagerController.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http.HttpResults; using CORRESPONSALBackend.Contracts.Auth; +using CORRESPONSALBackend.Contracts.Corresponsalias; namespace CORRESPONSALBackend.Controllers { @@ -23,6 +24,7 @@ namespace CORRESPONSALBackend.Controllers private readonly IFilePaths4ProcessRepository _RepoRelativePath; private readonly IConfiguration _config; private readonly IAuthService _authService; + private readonly ICorresponsaliasTraficosRepository _traficosRepository; private readonly string RootPathCorresponsales; @@ -31,7 +33,8 @@ namespace CORRESPONSALBackend.Controllers IConfiguration config, IUsuariosRepository RepoUsuarios, ICasaCuervoRepository RepoCasaCuervo, - IAuthService authService) + IAuthService authService, + ICorresponsaliasTraficosRepository traficosRepository) { _config = config; _Repo = Repo; @@ -40,6 +43,7 @@ namespace CORRESPONSALBackend.Controllers _RepoRelativePath = RepoRelativePath; RootPathCorresponsales = _config.GetValue("AllFiles"); _authService = authService; + _traficosRepository = traficosRepository; } [Route("GetFileInfoByProcess")] @@ -310,5 +314,51 @@ namespace CORRESPONSALBackend.Controllers return false; } } + + [HttpGet("GetClientesZip")] + public async Task GetClientesZip(int id){ + try{ + var trafico = await _traficosRepository.Get(id);//Se obtiene el modelo del trafico. + FilePaths4Process tempFilesRelativePath = await _RepoRelativePath.getPaths4ProcessById(40); // Path: Corresponsales\Zips\Facturacion\Entregas\ + var tempFilesPath = RootPathCorresponsales + tempFilesRelativePath.Path;// Path: C:data\Corresponsales\Zips\Facturacion\Entregas\ + var zipFilePath = tempFilesPath + trafico.FolioGemco + ".zip"; + if(!System.IO.File.Exists(zipFilePath)){//Se valida si no existe el archivo + var procesos = new int[]{2,37,38,39};//Se crea un array con los procesos de los cuales se van a necesitar los archivos + string folderName = string.Concat(trafico.FolioGemco, "\\"); // "23-000xxxC" -> "23-000xxxC\" + if(!System.IO.Directory.Exists(tempFilesPath + folderName)) + System.IO.Directory.CreateDirectory(tempFilesPath + folderName);// Path: C:data\Corresponsales\Zips\Facturacion\Entregas\23-000xxxC\ + //Se copian los archivos necesarios a la ruta donde se van a comprimir. + foreach (var proceso in procesos){ + var fileLog = await _Repo.getFileByProcess(id, proceso);//Se busca el registro del archivo en la BD + if(fileLog.Tags == "" && proceso != 39)//Se valida que exista el registro de la BD de los archivos obligatorios + throw new Exception("No se han encontrado uno o más de los archivos obligatorios"); + if(fileLog.Tags == "" && proceso == 39) break; //Si es un archivo opcional se salta la ejecucion del resto del foreach + //Se obtienen las rutas relativas donde se alojan los archivos. + FilePaths4Process fileRelativePath = await _RepoRelativePath.getPaths4ProcessById(proceso); + var filePath = RootPathCorresponsales + fileRelativePath.Path + fileLog!.NombreArchivo; + if(System.IO.File.Exists(filePath)){ + var bytes = await System.IO.File.ReadAllBytesAsync(filePath); + var originalFileStream = new MemoryStream(bytes); + using(var tempFileStream = System.IO.File.Create(tempFilesPath + folderName + fileLog.NombreArchivo)){ + await originalFileStream.CopyToAsync(tempFileStream); + } + }else{ + throw new Exception("No se han encontrado uno o más de los archivos obligatorios"); + } + } + string zipFileName = tempFilesPath + trafico.FolioGemco + ".zip"; + + //Se comprime el folder con el nombre del trafico para crear el zip en la ruta: c:data\Corresponsales\Zips\Facturacion\Entregas\{NombreDelZip.zip} + System.IO.Compression.ZipFile.CreateFromDirectory(tempFilesPath + folderName, zipFileName); + //Se borra el folder que contenia los archivos temporales + Directory.Delete(tempFilesPath + folderName, true); + } + var zipBytes = await System.IO.File.ReadAllBytesAsync(zipFilePath); + var zipStream = new MemoryStream(zipBytes); + return File(zipStream, "application/zip", trafico.FolioGemco + ".zip"); + }catch(Exception ex){ + return BadRequest(ex.Message); + } + } } } \ No newline at end of file