Pass in the parser

This commit is contained in:
Mike Phares 2023-09-07 12:51:42 -07:00
parent a109115d02
commit 213ebe7d31

View File

@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel;
using System.Diagnostics;
namespace File_Folder_Helper.Helpers;
@ -7,10 +6,15 @@ namespace File_Folder_Helper.Helpers;
internal static class HelperPdfStripperWrapper
{
private static (string?, string?) GetDestinationDirectory(List<string> args)
private record Input(string? DestinationDirectory,
string? Key,
char? Parser);
private static Input GetInput(List<string> args)
{
string? d = null;
string? k = null;
string? p = null;
for (int i = 1; i < args.Count; i++)
{
if (args[i].Length == 2 && i + 1 < args.Count)
@ -19,10 +23,18 @@ internal static class HelperPdfStripperWrapper
d = Path.GetFullPath(args[i + 1]);
else if (args[i][1] == 'k')
k = args[i + 1].Trim();
else if (args[i][1] == 'p')
p = args[i + 1];
i++;
}
}
return (d, k);
string directoryName = Path.GetFileName(args[0]);
if (!string.IsNullOrEmpty(d) && !d.EndsWith(directoryName))
d = Path.Combine(d, directoryName);
if (!string.IsNullOrEmpty(d) && !Directory.Exists(d))
_ = Directory.CreateDirectory(d);
char? parser = p is null ? null : p == "S" ? 'S' : p == "G" ? 'G' : p == "K" ? 'K' : null;
return new(d, k, parser);
}
private static string GetGhostTextFromPDF(string ghostPCLFileName, string sourceFileNamePdf, string destinationFileName)
@ -47,7 +59,7 @@ internal static class HelperPdfStripperWrapper
string result;
if (File.Exists(destinationFileName))
File.Delete(destinationFileName);
string arguments = $"-inputFile\"{sourceFileNamePdf}\" -outputFile\"{destinationFileName}\" -TTIF";
string arguments = $"-i\"{sourceFileNamePdf}\" -o\"{destinationFileName}\" -ttxt";
Process? process = Process.Start(kofaxFileName, arguments);
_ = process?.WaitForExit(30000);
if (!File.Exists(destinationFileName))
@ -75,7 +87,7 @@ internal static class HelperPdfStripperWrapper
return result;
}
private static void ParseSave(ILogger log, string pdfTextStripperFileName, string ghostPCLFileName, string kofaxFileName, string destinationDirectory, string[] files)
private static void ParseSave(ILogger log, string pdfTextStripperFileName, string ghostPCLFileName, string kofaxFileName, string destinationDirectory, char parser, string[] files)
{
string text;
string destinationFileName;
@ -84,11 +96,14 @@ internal static class HelperPdfStripperWrapper
foreach (string file in files)
{
destinationFileName = Path.Combine(destinationDirectory, $"{file}.txt");
text = GetTextFromPDF(pdfTextStripperFileName, file, destinationFileName);
if (string.IsNullOrEmpty(text))
if (parser == 'S')
text = GetTextFromPDF(pdfTextStripperFileName, file, destinationFileName);
else if (parser == 'G')
text = GetGhostTextFromPDF(ghostPCLFileName, file, destinationFileName);
if (string.IsNullOrEmpty(text))
else if (parser == 'K')
text = GetKofaxTextFromPDF(kofaxFileName, file, destinationFileName);
else
throw new NotImplementedException();
log.LogInformation("<{file}> == {length}", Path.GetFileName(file), text.Length);
}
}
@ -110,16 +125,21 @@ internal static class HelperPdfStripperWrapper
log.LogInformation("exe <{kofaxFileName}> doesn't exist!", kofaxFileName);
else
{
(string? destinationDirectory, string? _) = GetDestinationDirectory(args);
if (string.IsNullOrEmpty(destinationDirectory))
log.LogInformation("-d <{destinationDirectory}> wasn't supplied!", nameof(destinationDirectory));
Input input = GetInput(args);
if (string.IsNullOrEmpty(input.DestinationDirectory))
log.LogInformation("-d <{DestinationDirectory}> wasn't supplied!", nameof(input.DestinationDirectory));
else
{
string[] files = Directory.GetFiles(args[0], "*.pdf", SearchOption.TopDirectoryOnly);
if (files.Length == 0)
log.LogInformation("Length == {length}", files.Length);
if (input.Parser is null)
log.LogInformation("-p <{Parser}> wasn't supplied!", nameof(input.Parser));
else
ParseSave(log, pdfTextStripperFileName, ghostPCLFileName, kofaxFileName, destinationDirectory, files);
{
string[] files = Directory.GetFiles(args[0], "*.pdf", SearchOption.TopDirectoryOnly);
if (files.Length == 0)
log.LogInformation("Length == {length}", files.Length);
else
ParseSave(log, pdfTextStripperFileName, ghostPCLFileName, kofaxFileName, input.DestinationDirectory, input.Parser.Value, files);
}
}
}
}
@ -157,16 +177,21 @@ internal static class HelperPdfStripperWrapper
log.LogInformation("This helper was a short term helper!");
else
{
(string? destinationDirectory, string? key) = GetDestinationDirectory(args);
if (string.IsNullOrEmpty(key))
log.LogInformation("-k <{key}> wasn't supplied!", nameof(key));
Input input = GetInput(args);
if (string.IsNullOrEmpty(input.DestinationDirectory))
log.LogInformation("-d <{DestinationDirectory}> wasn't supplied!", nameof(input.DestinationDirectory));
else
{
string[] files = Directory.GetFiles(args[0], "*.txt", SearchOption.TopDirectoryOnly);
if (files.Length == 0)
log.LogInformation("Length == {length}", files.Length);
if (string.IsNullOrEmpty(input.Key))
log.LogInformation("-k <{key}> wasn't supplied!", nameof(input.Key));
else
ParseStrip(log, args[0], key, files);
{
string[] files = Directory.GetFiles(input.DestinationDirectory, "*.txt", SearchOption.TopDirectoryOnly);
if (files.Length == 0)
log.LogInformation("Length == {length}", files.Length);
else
ParseStrip(log, input.DestinationDirectory, input.Key, files);
}
}
}
}