From 90421e05830b1f46c8078098e51e13ac1f532e0b Mon Sep 17 00:00:00 2001 From: JieunSon96 Date: Thu, 30 May 2024 16:39:35 -0700 Subject: [PATCH] Changed filenames by appending incrementing numbers to duplicates --- .../MCS.FOI.MSGToPDF/MSGFileProcessor.cs | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs b/MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs index 4259b34f0..792937853 100644 --- a/MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs +++ b/MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs @@ -56,15 +56,25 @@ public MSGFileProcessor(Stream sourceStream) var _attachment = (Storage.Message)attachment; var filename = _attachment.FileName; var extension = Path.GetExtension(filename); + var baseFilename = Path.GetFileNameWithoutExtension(filename); if (!string.IsNullOrEmpty(extension)) { _attachment.Save(attachmentStream); Dictionary attachmentInfo = new Dictionary(); - + + // If the filename already exists, increment the duplicate count to create a unique filename if (fileNameHash.ContainsKey(filename)) { - - filename = Path.GetFileNameWithoutExtension(filename) + '1' + extension; + int duplicateCount = 1; // Initialize the duplicate count + string newFilename; + + // Loop until a unique filename is found + do + { + newFilename = baseFilename + duplicateCount.ToString() + extension; + duplicateCount++; + } while (fileNameHash.ContainsKey(newFilename)); + filename = newFilename; } fileNameHash.Add(filename, true); attachmentInfo.Add("filename", _attachment.FileName); @@ -80,19 +90,27 @@ public MSGFileProcessor(Stream sourceStream) var _attachment = (Storage.Attachment)attachment; var filename = _attachment.FileName; var extension = Path.GetExtension(filename); - + var baseFilename = Path.GetFileNameWithoutExtension(filename); if (!string.IsNullOrEmpty(extension)) { attachmentStream.Write(_attachment.Data, 0, _attachment.Data.Length); Dictionary attachmentInfo = new Dictionary(); - + + // If the filename already exists, increment the duplicate count to create a unique filename if (fileNameHash.ContainsKey(filename)) { - - filename = Path.GetFileNameWithoutExtension(filename) + '1' + extension; + int duplicateCount = 1; // Initialize the duplicate count + string newFilename; + // Loop until a unique filename is found + do + { + newFilename = baseFilename + '-' +duplicateCount.ToString() + extension; + duplicateCount++; + } while (fileNameHash.ContainsKey(newFilename)); + filename = newFilename; // Set the unique filename } fileNameHash.Add(filename, true); - attachmentInfo.Add("filename", _attachment.FileName); + attachmentInfo.Add("filename", filename); attachmentInfo.Add("s3filename", filename); attachmentInfo.Add("cid", _attachment.ContentId); attachmentInfo.Add("size", _attachment.Data.Length.ToString());