From 350759f1c6ddb5731bea55dce3d5fe538010ca30 Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Fri, 20 Jan 2023 08:31:38 +0500 Subject: [PATCH] =?UTF-8?q?-=D1=82=D0=B8=D0=BF=D1=8B=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BD=D0=B2=D0=B5=D1=80=D1=82=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81?= =?UTF-8?q?=D0=BA=D0=B8=D0=B5=20-=D1=83=D0=B4=D0=B0=D0=BB=D1=8F=D1=8E?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D0=BE=D0=B1=D1=80=D0=B0=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrillingProgram/Convert/ConvertToPdf.cs | 20 +++++++++++++------ .../DrillingProgram/Convert/ReadMe.md | 6 ++++++ .../DrillingProgram/DrillingProgramService.cs | 11 ++++++---- .../Controllers/DrillingProgramController.cs | 7 +++++-- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs b/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs index 6454b560..89e63335 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs @@ -10,9 +10,9 @@ using System.Threading; namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert { #nullable enable - internal class ConvertToPdf + sealed internal class ConvertToPdf { - private readonly string[] filesExtensions = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" }; + private static readonly string[] filesExtensions = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" }; private static void MergeFiles(IEnumerable inputFiles, string outFile) { @@ -35,12 +35,14 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert private static async Task StartConvertProcessAsync(string inputFileName, string outFileName, CancellationToken token) { - var result = Cli.Wrap("/usr/bin/soffice") - .WithArguments($"--headless --convert-to pdf {inputFileName} --outdir {outFileName}"); + //var result = Cli.Wrap("/usr/bin/soffice") + // .WithArguments($"--headless --convert-to pdf {inputFileName} --outdir {outFileName}"); + var result = Cli.Wrap("C:\\Program Files\\LibreOffice\\program\\soffice.exe") + .WithArguments($"-headless -convert-to pdf {inputFileName} --outdir {outFileName}"); await result.ExecuteAsync(token); } - - public async Task GetConverteAndMergedFileAsync(IEnumerable filesNames, string resultPath, CancellationToken token) + + public static async Task GetConverteAndMergedFileAsync(IEnumerable filesNames, string resultPath, CancellationToken token) { var badFiles = filesNames.Where(f => !filesExtensions.Contains(Path.GetExtension(f))); if (badFiles.Any()) @@ -55,15 +57,21 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert convertedFile = Path.ChangeExtension(f, ".pdf") }) .ToList(); + var listFilesToDelete = new List(); foreach (var fileName in listFileNames) { var fileExt = Path.GetExtension(fileName.inputFile); if (fileExt != ".pdf") { await StartConvertProcessAsync(fileName.inputFile, fileName.convertedFile, token); + listFilesToDelete.Add(fileName.convertedFile); } } MergeFiles(listFileNames.Select(c => c.convertedFile), resultPath); + foreach (var file in listFilesToDelete) + { + File.Delete(file); + } } } #nullable disable diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ReadMe.md b/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ReadMe.md index 6f493be8..952fada8 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ReadMe.md +++ b/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ReadMe.md @@ -12,6 +12,12 @@ Windows - C:\Program Files\LibreOffice\program\soffice.exe изначально обозначенные в задаче. При необходимости список можно расширить. +4. Если для тестирования исходники необходимо запустить на машине под управлением Windows +необходимо поменять содержание переменной result в методе StartConvertProcessAsync на : + + var result = Cli.Wrap("C:\\Program Files\\LibreOffice\\program\\soffice.exe") + .WithArguments($"-headless -convert-to pdf {inputFileName} --outdir {outFileName}"); + diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs index ef1075d8..c27dfb2d 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs @@ -30,6 +30,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram private readonly IConfiguration configuration; private readonly BackgroundWorker backgroundWorker; private readonly IEmailService emailService; + private readonly IServiceProvider serviceProvider; private const int idFileCategoryDrillingProgram = 1000; private const int idFileCategoryDrillingProgramPartsStart = 1001; @@ -58,7 +59,8 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram IWellService wellService, IConfiguration configuration, BackgroundWorker backgroundWorker, - IEmailService emailService) + IEmailService emailService, + IServiceProvider serviceProvider) { this.context = context; this.fileService = fileService; @@ -67,6 +69,8 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram this.configuration = configuration; this.backgroundWorker = backgroundWorker; this.emailService = emailService; + this.serviceProvider = serviceProvider; + } public async Task> GetAvailableUsers(int idWell, CancellationToken token = default) @@ -474,14 +478,13 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram { var well = (await wellService.GetOrDefaultAsync(idWell, token))!; var resultFileName = $"Программа бурения {well.Cluster} {well.Caption}.xlsx"; - var tempResultFilePath = Path.Combine(Path.GetTempPath(), "drillingProgram", resultFileName); - var converter = new ConvertToPdf(); + var tempResultFilePath = Path.Combine(Path.GetTempPath(), "drillingProgram", resultFileName); var workAction = async (string workId, IServiceProvider serviceProvider, CancellationToken token) => { var context = serviceProvider.GetRequiredService(); var fileService = serviceProvider.GetRequiredService(); var files = state.Parts.Select(p => fileService.GetUrl(p.File)); - await converter.GetConverteAndMergedFileAsync(files, tempResultFilePath, token); + await ConvertToPdf.GetConverteAndMergedFileAsync(files, tempResultFilePath, token); await fileService.MoveAsync(idWell, null, idFileCategoryDrillingProgram, resultFileName, tempResultFilePath, token); }; diff --git a/AsbCloudWebApi/Controllers/DrillingProgramController.cs b/AsbCloudWebApi/Controllers/DrillingProgramController.cs index 5b7f2c99..2be612bd 100644 --- a/AsbCloudWebApi/Controllers/DrillingProgramController.cs +++ b/AsbCloudWebApi/Controllers/DrillingProgramController.cs @@ -126,8 +126,11 @@ namespace AsbCloudWebApi.Controllers var fileName = files[0].FileName; - if (!fileName.EndsWith(".xlsx")) - return BadRequest(ArgumentInvalidException.MakeValidationError("file", "Файл должен быть xlsx")); + // TODO: в текущем контексте доработки по ковертации + // данная проверка будет некорректна, т.к. планируется загружать + // файлы разных форматов + //if (!fileName.EndsWith(".xlsx")) + // return BadRequest(ArgumentInvalidException.MakeValidationError("file", "Файл должен быть xlsx")); var fileStream = files[0].OpenReadStream(); var result = await drillingProgramService.AddFile(idWell, idFileCategory, (int)idUser, fileName, fileStream, token);