Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Fix persona images not being added as Base64 #84

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Epsilon/Export/Exporters/WordModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ public async Task<Stream> Export(ExportData data, string format)

var altChunkId = "HomePage";

var personaHTML = new HtmlDocument();
personaHTML.LoadHtml(data.PersonaHtml);
var personaHtml = new HtmlDocument();
personaHtml.LoadHtml(data.PersonaHtml);

var updatedPersonaHtml = await ReplaceImageSrcWithBase64String(personaHtml);
kn4a-com marked this conversation as resolved.
Show resolved Hide resolved

using var ms = new MemoryStream(new UTF8Encoding(true).GetPreamble()
.Concat(Encoding.UTF8.GetBytes($"<html>{personaHTML.Text}</html>")).ToArray());
.Concat(Encoding.UTF8.GetBytes($"<html>{updatedPersonaHtml.Text}</html>")).ToArray());

var formatImportPart =
document.MainDocumentPart.AddAlternativeFormatImportPart(
Expand Down Expand Up @@ -122,8 +123,10 @@ public async Task<Stream> Export(ExportData data, string format)
new TableCellProperties(new TableCellWidth {Type = TableWidthUnitValues.Auto})
);

private async Task ReplaceImageSrcWithBase64String(HtmlDocument htmlDoc)
private async Task<HtmlDocument> ReplaceImageSrcWithBase64String(HtmlDocument htmlDoc)
{
if (htmlDoc.DocumentNode.SelectNodes("//img") == null)
return null;
foreach (var node in htmlDoc.DocumentNode.SelectNodes("//img"))
{
var imageSrc = node
Expand All @@ -139,5 +142,7 @@ private async Task ReplaceImageSrcWithBase64String(HtmlDocument htmlDoc)

node.SetAttributeValue("src", $"data:image/jpeg;base64,{imageBase64}");
}

return htmlDoc;
}
}