Refactor WSRequest to streamline attachment handling and improve error checking for PDF file counts

This commit is contained in:
2025-12-11 09:02:36 -07:00
parent 8ee374b2a8
commit 35f5873e22

View File

@ -662,52 +662,34 @@ public class WSRequest
internal static void PostOpenInsightMetrologyViewerAttachments(IFileRead fileRead, Logistics logistics, string openInsightMetrologyViewerAPI, string ghostPCLFileName, List<txt.Description> descriptions, string matchDirectory, WS.Results results, string headerIdDirectory)
#pragma warning restore IDE0060
{
string extension;
string pdfFile;
string extension = ".pdf";
List<string> attachmentFiles = new();
string uniqueId = Logistics.GetUniqueId(logistics);
string[] summaryFiles = Directory.GetFiles(matchDirectory, "*.txt", SearchOption.TopDirectoryOnly);
if (summaryFiles.Length != 1)
throw new Exception($"Invalid source file count for <{results.HeaderId}>!");
string[] xpsFiles = Directory.GetFiles(matchDirectory, "*.xps", SearchOption.TopDirectoryOnly);
if (xpsFiles.Length > 0)
string[] prnFiles = Directory.GetFiles(matchDirectory, "WaferMap*.prn", SearchOption.TopDirectoryOnly);
foreach (string prnFile in prnFiles.OrderBy(l => l))
{
extension = ".xps";
foreach (string xpsFile in xpsFiles.OrderBy(l => l))
attachmentFiles.Add(xpsFile);
if (attachmentFiles.Count == 0 || attachmentFiles.Count != descriptions.Count)
throw new Exception("Invalid *.pdf file count!");
}
else
{
string pdfFile;
extension = ".pdf";
string[] prnFiles = Directory.GetFiles(matchDirectory, "WaferMap*.prn", SearchOption.TopDirectoryOnly);
foreach (string prnFile in prnFiles.OrderBy(l => l))
{
pdfFile = ConvertSourceFileToPdf(ghostPCLFileName, prnFile);
attachmentFiles.Add(pdfFile);
}
if (attachmentFiles.Count == 0 || attachmentFiles.Count != descriptions.Count)
throw new Exception("Invalid *.pdf file count!");
pdfFile = ConvertSourceFileToPdf(ghostPCLFileName, prnFile);
attachmentFiles.Add(pdfFile);
}
if (attachmentFiles.Count == 0)
throw new Exception("Invalid *.pdf file count!");
List<WS.Attachment> dataAttachments = new();
List<WS.Attachment> headerAttachments = new()
{
new WS.Attachment(results, headerIdDirectory, uniqueId, "Data.txt", summaryFiles[0])
};
int count;
if (attachmentFiles.Count < descriptions.Count)
count = attachmentFiles.Count;
else
count = descriptions.Count;
for (int i = 0; i < count; i++)
for (int i = 0; i < attachmentFiles.Count; i++)
{
if (!string.IsNullOrEmpty(attachmentFiles[i]))
dataAttachments.Add(new WS.Attachment(results, headerIdDirectory, $"{uniqueId}_Item-{i + 1}", $"Image{extension}", attachmentFiles[i]));
}
if (dataAttachments.Count == 0 || dataAttachments.Count != descriptions.Count)
throw new Exception($"Invalid attachment count! {dataAttachments.Count} != {descriptions.Count}");
WS.AttachFiles(openInsightMetrologyViewerAPI, headerAttachments, dataAttachments);
if (attachmentFiles.Count == 0 || attachmentFiles.Count != descriptions.Count)
throw new Exception("Invalid *.pdf file count!");
}
}