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