Skip to content

Commit

Permalink
Merge pull request #502 from bcgov/dev-marshal-NK-4339
Browse files Browse the repository at this point in the history
msg conversion to handle inline images and attachments
  • Loading branch information
nkan-aot authored Sep 29, 2023
2 parents b694669 + 933c617 commit eb5b7ff
Showing 1 changed file with 53 additions and 35 deletions.
88 changes: 53 additions & 35 deletions MCS.FOI.S3FileConversion/MCS.FOI.MSGToPDF/MSGFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using Syncfusion.Pdf.HtmlToPdf;
using System.ComponentModel.DataAnnotations;
using System;

namespace MCS.FOI.MSGToPDF
{
Expand Down Expand Up @@ -102,39 +103,20 @@ public MSGFileProcessor(Stream sourceStream)
fileNameHash.Add(filename, true);
attachmentInfo.Add("filename", _attachment.FileName);
attachmentInfo.Add("s3filename", filename);
attachmentInfo.Add("cid", _attachment.ContentId);
attachmentInfo.Add("size", _attachment.Data.Length.ToString());
attachmentInfo.Add("lastmodified", _attachment.LastModificationTime.ToString());
attachmentInfo.Add("created", _attachment.CreationTime.ToString());
attachmentsObj.Add(attachmentStream, attachmentInfo);
}
}
}

//msg.CharsetDetectionEncodingConfidenceLevel = 0.7f;
if (msg.BodyRtf != null)
{
//using var fs0 = new FileStream("C:\\folder\\log.txt", FileMode.Create, FileAccess.Write);
var msgReader = new Reader();

var inputStream = new MemoryStream();
//SourceStream.CopyTo(inputStream);
//List<MemoryStream> result = msgReader.ExtractToStream(inputStream, true);


//using var htmlfs = new FileStream("C:\\folder\\test.txt", FileMode.Create, FileAccess.Write);
//result[0].WriteTo(htmlfs);
//string bin = BitConverter.ToString(((Storage.Attachment)msg.Attachments[0]).Data).Replace("-", string.Empty);
//Console.WriteLine(bin);
string body = msgReader.ExtractMsgEmailBody(SourceStream, ReaderHyperLinks.Both, "text/html; charset=utf-8", false);
//var ms = new MemoryStream();
//var success = false;
//(ms, success) = ConvertHTMLtoPDF(body, ms);

//using var fs1 = new FileStream("C:\\folder\\blinkconverted.pdf", FileMode.Create, FileAccess.Write);
using var fs2 = new FileStream("C:\\folder\\syncfusionconverted.pdf", FileMode.Create, FileAccess.Write);
//using var fs3 = new FileStream("C:\\folder\\syncfusionconverted.docx", FileMode.Create, FileAccess.Write);
//using var fs4 = new FileStream("C:\\folder\\test.txt", FileMode.Create, FileAccess.Write);
//ms.WriteTo(fs1);
var bodyreplaced = Regex.Replace(Regex.Replace(body.Replace("<br>", "<br/>").Replace("<![if !supportAnnotations]>", "").Replace("<![endif]>", ""), "=(?<tagname>(?!utf-8)[\\w|-]+)", "=\"${tagname}\""), "<meta .*?>", "");
const string rtfInlineObject = "[*[RTFINLINEOBJECT]*]";
const string imgString = "<img";
Expand All @@ -148,19 +130,16 @@ public MSGFileProcessor(Stream sourceStream)
if (!attachment.GetType().FullName.ToLower().Contains("message"))
{
var _attachment = (Storage.Attachment)attachment;
if (htmlInline && !String.IsNullOrEmpty(_attachment.ContentId) && bodyreplaced.Contains(_attachment.ContentId))
if (htmlInline)
{
inlineAttachments.Add(_attachment);
//attachmentStream = new();
//attachmentStream.Write(_attachment.Data, 0, _attachment.Data.Length);
//attachmentsObj.Remove(attachmentStream);
if (!String.IsNullOrEmpty(_attachment.ContentId) && bodyreplaced.Contains(_attachment.ContentId))
{
inlineAttachments.Add(_attachment);
}
}
else if (rtfInline)
{
inlineAttachments.Add(_attachment);
//attachmentStream = new();
//attachmentStream.Write(_attachment.Data, 0, _attachment.Data.Length);
//attachmentsObj.Remove(attachmentStream);
}
}
}
Expand All @@ -185,19 +164,36 @@ public MSGFileProcessor(Stream sourceStream)
width = scale * width;
}
bodyreplaced = Regex.Replace(bodyreplaced, "<img.*cid:" + inlineAttachment.ContentId + ".*?>", "<img width='" + width.ToString() + "' height ='" + height.ToString() + "' src=\"data:" + inlineAttachment.MimeType + ";base64," + Convert.ToBase64String(inlineAttachment.Data) + "\"/>");
foreach (KeyValuePair<MemoryStream, Dictionary<string, string>> attachment in attachmentsObj)
{
if (attachment.Value["cid"] == inlineAttachment.ContentId)
{
attachmentsObj.Remove(attachment.Key);
}
}
}
else if (rtfInline)
{
//using var fs5 = new FileStream("C:\\folder\\image.png", FileMode.Create, FileAccess.Write);
//fs5.Write(inlineAttachment.Data, 0, inlineAttachment.Data.Length);
bodyreplaced = ReplaceFirstOccurrence(bodyreplaced, rtfInlineObject, "<img src=\"data:image/" + Path.GetExtension(inlineAttachment.FileName) + ";base64," + Convert.ToBase64String(inlineAttachment.Data) + "\"/>");
//var string1 = Convert.ToBase64String(inlineAttachment.Data);
//var string2 = BitConverter.ToString(inlineAttachment.Data).Replace("-", string.Empty);
if (inlineAttachment.OleAttachment)
{
bodyreplaced = ReplaceFirstOccurrence(bodyreplaced, rtfInlineObject, "<img src=\"data:image/" + Path.GetExtension(inlineAttachment.FileName) + ";base64," + Convert.ToBase64String(inlineAttachment.Data) + "\"/>");
foreach (KeyValuePair<MemoryStream, Dictionary<string, string>> attachment in attachmentsObj)
{
if (attachment.Value["filename"] == inlineAttachment.FileName)
{
attachmentsObj.Remove(attachment.Key);
}
}
}
else
{
bodyreplaced = ReplaceFirstOccurrence(bodyreplaced, rtfInlineObject, " <b>[**Inline Attachment - " + inlineAttachment.FileName + "**]</b>");
}
}
}
}
}


byte[] byteArray = Encoding.ASCII.GetBytes(bodyreplaced);
using (MemoryStream messageStream = new MemoryStream(byteArray))
{
Expand All @@ -210,6 +206,26 @@ public MSGFileProcessor(Stream sourceStream)
var regex = new Regex(@"(\r)*(\n)*(\t)+", RegexOptions.Multiline);
var occurences = rtfDoc.Replace(regex, "\r\n");

List<Entity> pictures = rtfDoc.FindAllItemsByProperty(EntityType.Picture, "", "");

foreach (WPicture picture in pictures)
{
picture.LockAspectRatio = true;
const float maxSize = 500;
if (picture.Height > maxSize && picture.Height >= picture.Width)
{
var scale = (maxSize / picture.Height) * 100;
picture.HeightScale = scale;
picture.WidthScale = scale;
}
if (picture.Width > maxSize)
{
var scale = (maxSize / picture.Width) * 100;
picture.HeightScale = scale;
picture.WidthScale = scale;
}
}

//Gets all the hyperlink fields in the document

List<Entity> fields = rtfDoc.FindAllItemsByProperty(EntityType.Field, "FieldType", FieldType.FieldHyperlink.ToString());
Expand Down Expand Up @@ -311,6 +327,8 @@ public MSGFileProcessor(Stream sourceStream)
//bool isConverted;
//(output, isConverted) = ConvertHTMLtoPDF(htmlString, output);




break;
}
Expand Down

0 comments on commit eb5b7ff

Please sign in to comment.