From d46578f9c125e4700f0c8e1f4db3d896edb9cd92 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 10 May 2017 20:11:39 +0100 Subject: Add support for image converters --- Mammoth.Tests/DocumentConverterTests.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'Mammoth.Tests') diff --git a/Mammoth.Tests/DocumentConverterTests.cs b/Mammoth.Tests/DocumentConverterTests.cs index 416893f..5469e08 100644 --- a/Mammoth.Tests/DocumentConverterTests.cs +++ b/Mammoth.Tests/DocumentConverterTests.cs @@ -2,6 +2,7 @@ using System.IO; using Xunit.Sdk; using System; +using System.Collections.Generic; using System.Linq; namespace Mammoth.Tests { @@ -97,6 +98,28 @@ namespace Mammoth.Tests { Directory.Delete(tempDirectory, recursive: true); } } + + [Fact] + public void ImageConversionCanBeCustomised() { + AssertSuccessfulConversion( + ConvertToHtml("tiny-picture.docx", mammoth => mammoth.ImageConverter(ConvertImage)), + "

" + ); + } + + private IDictionary ConvertImage(IImage image) { + using (var stream = image.GetStream()) { + var base64 = StreamToBase64(stream); + var src = base64.Substring(0, 2) + "," + image.ContentType; + return new Dictionary { { "src", src } }; + } + } + + private static string StreamToBase64(System.IO.Stream stream) { + var memoryStream = new System.IO.MemoryStream(); + stream.CopyTo(memoryStream); + return System.Convert.ToBase64String(memoryStream.ToArray()); + } [Fact] public void ContentTypesAreRead() { -- cgit v1.2.3