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

Commit

Permalink
Fix persona images not being added as Base64 (#84)
Browse files Browse the repository at this point in the history
* Made some adjustments that the pictures in the persona get converted to Base64 strings versus loading it remotely from Canvas (as the access key is only valid for a limited time period)

* Add coding guidelines enforcement (#85)

* Add initial coding guidelines and fix where necessary

* Enforce code styling and update code where necessary

* Remove duplicate .editorconfig entries

* Add code analysis setup to CI

* Add missing checkout step

* Update incorrect action version tag

* Remove incompatible analysis step

* Push code style offense to validate CI pipeline

* Remove code style violation

* Add global.json

* Made a couple of changes based on recommendations made in the merge request.

---------

Co-authored-by: Jelle Maas <typiqally@gmail.com>
  • Loading branch information
kn4a-com and Typiqally committed May 26, 2023
1 parent f2132f1 commit 63d4477
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 76 deletions.
4 changes: 4 additions & 0 deletions Epsilon.Tests/Epsilon.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
<ProjectReference Include="..\Epsilon\Epsilon.csproj"/>
</ItemGroup>

<ItemGroup>
<Folder Include="Exporters\"/>
</ItemGroup>

</Project>
62 changes: 0 additions & 62 deletions Epsilon.Tests/Exporters/WordModuleExporterTests.cs

This file was deleted.

57 changes: 43 additions & 14 deletions Epsilon/Export/Exporters/WordModuleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,38 @@
using DocumentFormat.OpenXml.Wordprocessing;
using Epsilon.Abstractions.Export;
using Epsilon.Abstractions.Model;
using Epsilon.Canvas.Abstractions.Service;
using HtmlAgilityPack;

namespace Epsilon.Export.Exporters;

public class WordModuleExporter : ICanvasModuleExporter
{
private readonly IFileHttpService _fileService;

public WordModuleExporter(IFileHttpService fileService)
{
_fileService = fileService;
}

private static readonly TableBorders s_defaultBorders = new TableBorders(new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 3,
Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3,
}, new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 3,
Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3,
}, new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 3,
Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3,
}, new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 3,
Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 3,
}, new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 6,
Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6,
}, new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 6,
Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 6,
});

private static readonly TableProperties s_defaultTableProperties = new TableProperties(s_defaultBorders);
Expand Down Expand Up @@ -63,8 +65,7 @@ public async Task<Stream> Export(ExportData data, string format)

const string altChunkId = "HomePage";

var personaHtml = new HtmlDocument();
personaHtml.LoadHtml(data.PersonaHtml);
var personaHtml = await GetPersonaHtmlDocument(_fileService, data.PersonaHtml);

using var ms = new MemoryStream(new UTF8Encoding(true).GetPreamble()
.Concat(Encoding.UTF8.GetBytes($"<html>{personaHtml.Text}</html>")).ToArray());
Expand Down Expand Up @@ -138,4 +139,32 @@ private static TableCell CreateTextCell(string text)
Type = TableWidthUnitValues.Auto,
}));
}

private static async Task<HtmlDocument> GetPersonaHtmlDocument(IFileHttpService fileService, string htmlString)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlString);
if (htmlDoc.DocumentNode.SelectNodes("//img") == null)
{
return null;
}

foreach (var node in htmlDoc.DocumentNode.SelectNodes("//img"))
{
var imageSrc = node
.SelectNodes("//img")
.First()
.Attributes["src"].Value;

if (imageSrc != null)
{
var imageBytes = await fileService.GetFileByteArray(new Uri(imageSrc));
var imageBase64 = Convert.ToBase64String(imageBytes.ToArray());

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

return htmlDoc;
}
}

0 comments on commit 63d4477

Please sign in to comment.