Skip to content

Commit

Permalink
Merge pull request #659 from bcgov/main
Browse files Browse the repository at this point in the history
merge down to dev from main for recent hotfixes
  • Loading branch information
abin-aot authored Dec 14, 2023
2 parents 4628b4e + 5e3f8d6 commit 0cf9a0e
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MsgReader.Outlook;
using MsgReader.Outlook;
using MsgReader;
using Serilog;
using Syncfusion.HtmlConverter;
Expand Down Expand Up @@ -140,6 +140,7 @@ public MSGFileProcessor(Stream sourceStream)
inlineAttachments.Add(_attachment);
}
}
var startAt = 0;
foreach (var inlineAttachment in inlineAttachments.OrderBy(m => m.GetType().GetProperty("RenderingPosition").GetValue(m, null)))
{
if (rtfInline)
Expand Down Expand Up @@ -172,7 +173,31 @@ public MSGFileProcessor(Stream sourceStream)
else if (htmlInline)
{
var _inlineAttachment = (Storage.Attachment)inlineAttachment;
bodyreplaced = Regex.Replace(bodyreplaced, "src=\"cid:" + _inlineAttachment.ContentId, "style=\"max-width: 700px\" src=\"data:" + _inlineAttachment.MimeType + ";base64," + Convert.ToBase64String(_inlineAttachment.Data));
Regex regex = new Regex("<img(.|\\n)*cid:" + _inlineAttachment.ContentId + "(.|\\n)*?>");
Match match = regex.Match(bodyreplaced, startAt);
if (match.Success)
{
const float maxSize = 700;
Regex.Match(match.Value, "width=(\"|\')?(?<width>\\d+)(\"|\')?").Groups.TryGetValue("width", out var w);
float width = float.Parse(w.Value);
Regex.Match(match.Value, "height=(\"|\')?(?<height>\\d+)(\"|\')?").Groups.TryGetValue("height", out var h);
float height = float.Parse(h.Value);
if (width > maxSize && width >= height)
{
float scale = maxSize / width;
width = (int) (width * scale);
height = (int) (height * scale);
}
if (height > maxSize)
{
float scale = maxSize / height;
width = (int) (width * scale);
height = (int) (height * scale);
}
string imgReplacementString = "<img width=\"" + width + "\" height=\"" + height + "\" style=\"margin: 1px;\" src=\"data:" + _inlineAttachment.MimeType + ";base64," + Convert.ToBase64String(_inlineAttachment.Data) + "\"/>";
bodyreplaced = regex.Replace(bodyreplaced, imgReplacementString, 1, startAt);
startAt = match.Index + imgReplacementString.Length;
}
foreach (KeyValuePair<MemoryStream, Dictionary<string, string>> attachment in attachmentsObj)
{
if (attachment.Value.ContainsKey("cid") && attachment.Value["cid"] == _inlineAttachment.ContentId)
Expand Down Expand Up @@ -597,4 +622,4 @@ static string RemoveContentBetweenTags(string inputString)
}

}
}
}

0 comments on commit 0cf9a0e

Please sign in to comment.