From 29d3c2477d438f6c69d16188e75664fdc27655d5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 14 Feb 2016 12:49:54 +0000 Subject: Encode images as base64 --- .../java/org/zwobble/mammoth/DocumentConverter.java | 17 ++++++++++++++++- .../zwobble/mammoth/tests/DocumentConverterTests.java | 14 +++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index dce76ad..da10324 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -1,9 +1,12 @@ package org.zwobble.mammoth; +import com.google.common.io.ByteStreams; import org.zwobble.mammoth.documents.*; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.html.HtmlNode; +import java.io.IOException; +import java.util.Base64; import java.util.List; import java.util.Map; @@ -127,7 +130,19 @@ public class DocumentConverter { @Override public List visit(Image image) { - throw new UnsupportedOperationException(); + // TODO: handle empty content type + return image.getContentType() + .map(contentType -> { + try { + String base64 = Base64.getEncoder().encodeToString(ByteStreams.toByteArray(image.open())); + String src = "data:" + contentType + ";base64," + base64; + return list(Html.selfClosingElement("img", map("src", src))); + } catch (IOException exception) { + // TODO: return a result with a warning + throw new RuntimeException(exception); + } + }) + .orElse(list()); } }); } diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java index 1890334..9287f74 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java @@ -6,7 +6,9 @@ import org.zwobble.mammoth.documents.*; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.html.HtmlNode; +import java.io.ByteArrayInputStream; import java.util.List; +import java.util.Optional; import static com.natpryce.makeiteasy.MakeItEasy.*; import static org.hamcrest.MatcherAssert.assertThat; @@ -105,7 +107,7 @@ public class DocumentConverterTests { new TableRow(list( new TableCell(list(paragraphWithText("Bottom left"))), new TableCell(list(paragraphWithText("Bottom right")))))))), - + deepEquals(list(Html.element("table", list( Html.element("tr", list( Html.element("td", list(Html.element("p", list(Html.text("Top left"))))), @@ -136,6 +138,16 @@ public class DocumentConverterTests { deepEquals(list(Html.element("a", map("id", "doc-42-start"))))); } + @Test + public void imagesAreConvertedToImageTagsWithDataUriByDefault() { + Image image = new Image( + Optional.empty(), + Optional.of("image/png"), + () -> new ByteArrayInputStream(new byte[]{97, 98, 99})); + assertThat( + convertToHtml(image), + deepEquals(list(Html.selfClosingElement("img", map("src", "data:image/png;base64,YWJj"))))); + } private List convertToHtml(DocumentElement element) { return documentConverter().convertToHtml(element); -- cgit v1.2.3