Skip to content

Commit

Permalink
Merge pull request #1002 from bcgov/foimod-3165
Browse files Browse the repository at this point in the history
[FOIMOD-3165] 2 Attachments With Identical Names Attached to An Email Cause an Upload Error
  • Loading branch information
JieunSon96 authored Jun 4, 2024
2 parents e18602a + 90421e0 commit 7b3f2aa
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> attachmentInfo = new Dictionary<string, string>();


// 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);
Expand All @@ -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<string, string> attachmentInfo = new Dictionary<string, string>();


// 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());
Expand Down

0 comments on commit 7b3f2aa

Please sign in to comment.