diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs b/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs index 4c659e41..b2043a10 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/Convert/ConvertToPdf.cs @@ -12,7 +12,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert #nullable enable sealed internal class ConvertToPdf { - private static readonly string[] filesExtensions = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" }; + internal static readonly string[] filesExtensions = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" }; private static void MergeFiles(IEnumerable inputFiles, string outFile) { @@ -20,27 +20,27 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert using var doc = new Document(); using var pdf = new PdfCopy(doc, stream); doc.Open(); - var inputFilesList = inputFiles.ToList(); - foreach (var file in inputFilesList) - { + var inputFilesList = inputFiles.ToList(); + foreach (var file in inputFilesList) + { var reader = new PdfReader(file); for (int i = 0; i < reader.NumberOfPages; i++) { pdf.AddPage(pdf.GetImportedPage(reader, i + 1)); } pdf.FreeReader(reader); - reader.Close(); + reader.Close(); }; } - private static async Task StartConvertProcessAsync(string inputFileName, string resultFileDir,CancellationToken token) - { + private static async Task StartConvertProcessAsync(string inputFileName, string resultFileDir, CancellationToken token) + { var command = Cli.Wrap("/usr/bin/soffice") .WithArguments($"--headless --convert-to pdf {inputFileName} --outdir {resultFileDir}"); await command.ExecuteAsync(token); } - - public static async Task GetConverteAndMergedFileAsync(IEnumerable files, string resultPath, string convertedFilesDir,CancellationToken token) + + public static async Task GetConverteAndMergedFileAsync(IEnumerable files, string resultPath, string convertedFilesDir, CancellationToken token) { var badFiles = files.Where(f => !filesExtensions.Contains(Path.GetExtension(f))); if (badFiles.Any()) @@ -50,22 +50,23 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert } var listFiles = files .Distinct() - .Select(f => new { + .Select(f => new + { inputFile = f, - convertedFile = Path.Combine(convertedFilesDir,"pdf",Path.ChangeExtension(Path.GetFileName(f), ".pdf")) + convertedFile = Path.Combine(convertedFilesDir, "pdf", Path.ChangeExtension(Path.GetFileName(f), ".pdf")) }) .ToList(); foreach (var file in listFiles) - { - var fileExt = Path.GetExtension(file.inputFile); + { + var fileExt = Path.GetExtension(file.inputFile).ToLower(); if (fileExt != ".pdf") - { - await StartConvertProcessAsync(file.inputFile, Path.GetDirectoryName(file.convertedFile)!,token); + { + await StartConvertProcessAsync(file.inputFile, Path.GetDirectoryName(file.convertedFile)!, token); } } MergeFiles(listFiles.Select(c => c.convertedFile), resultPath); - Directory.Delete(Path.Combine(convertedFilesDir,"pdf"),true); - } + Directory.Delete(Path.Combine(convertedFilesDir, "pdf"), true); + } } #nullable disable } diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs index 32773c5d..00d2c46e 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs @@ -51,7 +51,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram private const int idStateReady = 3; private const int idStateError = 4; - private static readonly string[] filePermittedExt = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" }; + private static readonly string[] validFileExtensions = ConvertToPdf.filesExtensions; public DrillingProgramService( IAsbCloudDbContext context, @@ -163,18 +163,17 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram await EnqueueMakeProgramWorkAsync(idWell, state, token); return state; } - private bool ValidExtFile(string file) + + private static bool IsFileExtensionValid(string file) { - var fileExt = Path.GetExtension(file); - var badExt = Array.Find(filePermittedExt, e => e == fileExt); - if (badExt is null) - return false; - return true; + var fileExt = Path.GetExtension(file).ToLower(); + return validFileExtensions.Contains(fileExt); } - public async Task AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default) + + public async Task AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, Stream fileStream, CancellationToken token = default) { - if (!ValidExtFile(fileFullName)) - throw new FileFormatException($"Файл {fileFullName} - неподдерживаемого формата. Файл не может быть загружен. "); + if (!IsFileExtensionValid(fileFullName)) + throw new FileFormatException($"Файл {fileFullName} - неподдерживаемого формата. Файл не может быть загружен."); var part = await context.DrillingProgramParts .Include(p => p.RelatedUsers) @@ -346,9 +345,6 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram await NotifyPublisherOnFullAccepAsync(fileMarkDto, token); } } - - - return result; } @@ -477,8 +473,6 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram return part; } - - private async Task EnqueueMakeProgramWorkAsync(int idWell, DrillingProgramStateDto state, CancellationToken token) { if (state.IdState == idStateCreating)