Skip to content

Commit

Permalink
Update to MhtParser
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWhiteDev committed Jan 8, 2018
1 parent 0765fb3 commit 0bb987d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions OpenXmlPowerTools/PtUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class MhtParserPart
public string ContentLocation;
public string ContentTransferEncoding;
public string ContentType;
public string CharSet;
public string Text;
public byte[] Binary;
}
Expand Down Expand Up @@ -153,6 +154,7 @@ public static MhtParser Parse(string src)
string contentLocation = null;
string contentTransferEncoding = null;
string partContentType = null;
string partCharSet = null;
byte[] partBinary = null;

foreach (var item in partPriamble)
Expand All @@ -176,6 +178,29 @@ public static MhtParser Parse(string src)
.Select(l => l + Environment.NewLine)
.StringConcatenate();

if (partContentType != null && partContentType.Contains(";"))
{
string thisPartContentType = null;
var spl = partContentType.Split(';').Select(s => s.Trim()).ToArray();
foreach (var s in spl)
{
if (s.StartsWith("charset"))
{
var begText = "charset=\"";
var begLen = begText.Length;
partCharSet = s.Substring(begLen, s.Length - begLen - 1);
continue;
}
if (thisPartContentType == null)
{
thisPartContentType = s;
continue;
}
throw new OpenXmlPowerToolsException("Unexpected content in MHTML");
}
partContentType = thisPartContentType;
}

if (contentTransferEncoding != null && contentTransferEncoding.ToUpper() == "BASE64")
{
partBinary = Convert.FromBase64String(partText);
Expand All @@ -186,6 +211,7 @@ public static MhtParser Parse(string src)
ContentLocation = contentLocation,
ContentTransferEncoding = contentTransferEncoding,
ContentType = partContentType,
CharSet = partCharSet,
Text = partText,
Binary = partBinary,
};
Expand Down

0 comments on commit 0bb987d

Please sign in to comment.