Format code.

This commit is contained in:
ngfrolov 2023-01-27 10:11:04 +05:00
parent d5fd53595e
commit 1d618302f8
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 27 additions and 32 deletions

View File

@ -12,7 +12,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert
#nullable enable #nullable enable
sealed internal class ConvertToPdf 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<string> inputFiles, string outFile) private static void MergeFiles(IEnumerable<string> inputFiles, string outFile)
{ {
@ -20,27 +20,27 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert
using var doc = new Document(); using var doc = new Document();
using var pdf = new PdfCopy(doc, stream); using var pdf = new PdfCopy(doc, stream);
doc.Open(); doc.Open();
var inputFilesList = inputFiles.ToList(); var inputFilesList = inputFiles.ToList();
foreach (var file in inputFilesList) foreach (var file in inputFilesList)
{ {
var reader = new PdfReader(file); var reader = new PdfReader(file);
for (int i = 0; i < reader.NumberOfPages; i++) for (int i = 0; i < reader.NumberOfPages; i++)
{ {
pdf.AddPage(pdf.GetImportedPage(reader, i + 1)); pdf.AddPage(pdf.GetImportedPage(reader, i + 1));
} }
pdf.FreeReader(reader); 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") var command = Cli.Wrap("/usr/bin/soffice")
.WithArguments($"--headless --convert-to pdf {inputFileName} --outdir {resultFileDir}"); .WithArguments($"--headless --convert-to pdf {inputFileName} --outdir {resultFileDir}");
await command.ExecuteAsync(token); await command.ExecuteAsync(token);
} }
public static async Task GetConverteAndMergedFileAsync(IEnumerable<string> files, string resultPath, string convertedFilesDir,CancellationToken token) public static async Task GetConverteAndMergedFileAsync(IEnumerable<string> files, string resultPath, string convertedFilesDir, CancellationToken token)
{ {
var badFiles = files.Where(f => !filesExtensions.Contains(Path.GetExtension(f))); var badFiles = files.Where(f => !filesExtensions.Contains(Path.GetExtension(f)));
if (badFiles.Any()) if (badFiles.Any())
@ -50,22 +50,23 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert
} }
var listFiles = files var listFiles = files
.Distinct() .Distinct()
.Select(f => new { .Select(f => new
{
inputFile = f, 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(); .ToList();
foreach (var file in listFiles) foreach (var file in listFiles)
{ {
var fileExt = Path.GetExtension(file.inputFile); var fileExt = Path.GetExtension(file.inputFile).ToLower();
if (fileExt != ".pdf") 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); MergeFiles(listFiles.Select(c => c.convertedFile), resultPath);
Directory.Delete(Path.Combine(convertedFilesDir,"pdf"),true); Directory.Delete(Path.Combine(convertedFilesDir, "pdf"), true);
} }
} }
#nullable disable #nullable disable
} }

View File

@ -51,7 +51,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
private const int idStateReady = 3; private const int idStateReady = 3;
private const int idStateError = 4; 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( public DrillingProgramService(
IAsbCloudDbContext context, IAsbCloudDbContext context,
@ -163,18 +163,17 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
await EnqueueMakeProgramWorkAsync(idWell, state, token); await EnqueueMakeProgramWorkAsync(idWell, state, token);
return state; return state;
} }
private bool ValidExtFile(string file)
private static bool IsFileExtensionValid(string file)
{ {
var fileExt = Path.GetExtension(file); var fileExt = Path.GetExtension(file).ToLower();
var badExt = Array.Find(filePermittedExt, e => e == fileExt); return validFileExtensions.Contains(fileExt);
if (badExt is null)
return false;
return true;
} }
public async Task<int> AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default)
public async Task<int> AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, Stream fileStream, CancellationToken token = default)
{ {
if (!ValidExtFile(fileFullName)) if (!IsFileExtensionValid(fileFullName))
throw new FileFormatException($"Файл {fileFullName} - неподдерживаемого формата. Файл не может быть загружен. "); throw new FileFormatException($"Файл {fileFullName} - неподдерживаемого формата. Файл не может быть загружен.");
var part = await context.DrillingProgramParts var part = await context.DrillingProgramParts
.Include(p => p.RelatedUsers) .Include(p => p.RelatedUsers)
@ -346,9 +345,6 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
await NotifyPublisherOnFullAccepAsync(fileMarkDto, token); await NotifyPublisherOnFullAccepAsync(fileMarkDto, token);
} }
} }
return result; return result;
} }
@ -477,8 +473,6 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
return part; return part;
} }
private async Task EnqueueMakeProgramWorkAsync(int idWell, DrillingProgramStateDto state, CancellationToken token) private async Task EnqueueMakeProgramWorkAsync(int idWell, DrillingProgramStateDto state, CancellationToken token)
{ {
if (state.IdState == idStateCreating) if (state.IdState == idStateCreating)