forked from ddrilling/AsbCloudServer
Format code.
This commit is contained in:
parent
d5fd53595e
commit
1d618302f8
@ -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)
|
||||||
{
|
{
|
||||||
@ -33,14 +33,14 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram.Convert
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
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,21 +50,22 @@ 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
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user