2023-02-15 17:15:09 +05:00
|
|
|
|
using System.Collections.Generic;
|
2023-01-13 17:28:04 +05:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using CliWrap;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-01-17 11:11:40 +05:00
|
|
|
|
using System.Threading;
|
2023-01-27 17:31:43 +05:00
|
|
|
|
using System;
|
2023-02-15 17:15:09 +05:00
|
|
|
|
using iText.Kernel.Pdf;
|
|
|
|
|
using iText.Kernel.Utils;
|
2023-01-13 17:28:04 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert
|
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2023-01-20 08:31:38 +05:00
|
|
|
|
sealed internal class ConvertToPdf
|
2023-01-13 17:28:04 +05:00
|
|
|
|
{
|
2023-01-27 10:11:04 +05:00
|
|
|
|
internal static readonly string[] filesExtensions = { ".xlsx", ".xls", ".ods", ".odt", ".doc", ".docx", ".pdf" };
|
2023-01-13 17:28:04 +05:00
|
|
|
|
|
2023-01-18 11:31:58 +05:00
|
|
|
|
private static void MergeFiles(IEnumerable<string> inputFiles, string outFile)
|
2023-01-13 17:28:04 +05:00
|
|
|
|
{
|
2023-02-15 17:15:09 +05:00
|
|
|
|
using var docResult = new PdfDocument(new PdfWriter(outFile));
|
|
|
|
|
var merger = new PdfMerger(docResult);
|
|
|
|
|
|
|
|
|
|
foreach (var fileName in inputFiles)
|
2023-01-27 10:11:04 +05:00
|
|
|
|
{
|
2023-02-15 17:15:09 +05:00
|
|
|
|
using var doc = new PdfDocument(new PdfReader(fileName));
|
|
|
|
|
merger.Merge(doc, 1, doc.GetNumberOfPages());
|
|
|
|
|
doc.Close();
|
2023-01-18 11:31:58 +05:00
|
|
|
|
};
|
2023-02-15 17:15:09 +05:00
|
|
|
|
|
|
|
|
|
docResult.Close();
|
2023-01-13 17:28:04 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 17:06:22 +05:00
|
|
|
|
private static (string programFile, string programArg) GetOptionsStartupProcess (string inputFileName, string resultFileDir)
|
2023-01-27 17:31:43 +05:00
|
|
|
|
{
|
|
|
|
|
(string programFile, string programArg) startupOptions;
|
|
|
|
|
if (OperatingSystem.IsWindows())
|
|
|
|
|
{
|
|
|
|
|
startupOptions.programFile = "C:\\Program Files\\LibreOffice\\program\\soffice.exe";
|
|
|
|
|
startupOptions.programArg = $"-headless -convert-to pdf {inputFileName} --outdir {resultFileDir}";
|
|
|
|
|
return startupOptions;
|
|
|
|
|
}
|
|
|
|
|
if(OperatingSystem.IsLinux())
|
|
|
|
|
{
|
|
|
|
|
startupOptions.programFile = "/usr/bin/soffice";
|
|
|
|
|
startupOptions.programArg = $"--headless --convert-to pdf {inputFileName} --outdir {resultFileDir}";
|
|
|
|
|
return (startupOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NotSupportedException ("Вызов процесса в текущей операционной системе не возможен");
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 10:11:04 +05:00
|
|
|
|
private static async Task StartConvertProcessAsync(string inputFileName, string resultFileDir, CancellationToken token)
|
|
|
|
|
{
|
2023-01-30 17:06:22 +05:00
|
|
|
|
var (programFile, programArg) = GetOptionsStartupProcess(inputFileName, resultFileDir);
|
|
|
|
|
var command = Cli.Wrap(programFile)
|
|
|
|
|
.WithArguments(programArg);
|
2023-01-25 05:32:32 +05:00
|
|
|
|
await command.ExecuteAsync(token);
|
2023-01-13 17:28:04 +05:00
|
|
|
|
}
|
2023-01-27 10:11:04 +05:00
|
|
|
|
|
|
|
|
|
public static async Task GetConverteAndMergedFileAsync(IEnumerable<string> files, string resultPath, string convertedFilesDir, CancellationToken token)
|
2023-01-13 17:28:04 +05:00
|
|
|
|
{
|
2023-01-25 05:32:32 +05:00
|
|
|
|
var badFiles = files.Where(f => !filesExtensions.Contains(Path.GetExtension(f)));
|
2023-01-18 11:31:58 +05:00
|
|
|
|
if (badFiles.Any())
|
2023-01-17 05:14:04 +05:00
|
|
|
|
{
|
2023-01-18 11:31:58 +05:00
|
|
|
|
throw new FileFormatException($"Файлы: {string.Join(", ", badFiles)} - неподдерживаемого формата. " +
|
|
|
|
|
$"Они не могут быть добавлены в список файлов для конвертации и слияния в общий файл программы бурения.");
|
2023-01-17 05:14:04 +05:00
|
|
|
|
}
|
2023-01-25 05:32:32 +05:00
|
|
|
|
var listFiles = files
|
2023-01-13 17:28:04 +05:00
|
|
|
|
.Distinct()
|
2023-01-27 10:11:04 +05:00
|
|
|
|
.Select(f => new
|
|
|
|
|
{
|
2023-01-18 11:31:58 +05:00
|
|
|
|
inputFile = f,
|
2023-01-27 10:11:04 +05:00
|
|
|
|
convertedFile = Path.Combine(convertedFilesDir, "pdf", Path.ChangeExtension(Path.GetFileName(f), ".pdf"))
|
2023-01-17 05:14:04 +05:00
|
|
|
|
})
|
|
|
|
|
.ToList();
|
2023-01-25 05:32:32 +05:00
|
|
|
|
foreach (var file in listFiles)
|
2023-01-27 10:11:04 +05:00
|
|
|
|
{
|
|
|
|
|
var fileExt = Path.GetExtension(file.inputFile).ToLower();
|
2023-01-17 11:11:40 +05:00
|
|
|
|
if (fileExt != ".pdf")
|
2023-01-27 10:11:04 +05:00
|
|
|
|
{
|
|
|
|
|
await StartConvertProcessAsync(file.inputFile, Path.GetDirectoryName(file.convertedFile)!, token);
|
2023-01-17 11:11:40 +05:00
|
|
|
|
}
|
2023-01-13 17:28:04 +05:00
|
|
|
|
}
|
2023-01-25 05:32:32 +05:00
|
|
|
|
MergeFiles(listFiles.Select(c => c.convertedFile), resultPath);
|
2023-01-27 10:11:04 +05:00
|
|
|
|
Directory.Delete(Path.Combine(convertedFilesDir, "pdf"), true);
|
|
|
|
|
}
|
2023-01-13 17:28:04 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2023-01-13 17:28:04 +05:00
|
|
|
|
}
|