diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-09-07 20:39:54 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-09-07 20:39:54 +0100 |
| commit | 2596c1c7122096874d00d7c5392d4ffb482e31b6 (patch) | |
| tree | 9eea4c367538cc5b7c0554e7f7d3299dc82d166d | |
| parent | 20406f3b17d2bb188b78c22602b79684086798d1 (diff) | |
Continue with warning when failing to read image
| -rw-r--r-- | lib/document-to-html.js | 13 | ||||
| -rw-r--r-- | lib/docx/files.js | 16 | ||||
| -rw-r--r-- | package.json | 3 | ||||
| -rw-r--r-- | tests/docx/files.tests.js | 13 | ||||
| -rw-r--r-- | tests/mammoth.tests.js | 11 |
5 files changed, 52 insertions, 4 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 907d62c..8a7cec0 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -174,6 +174,17 @@ function DocumentConversion(options) { }; }); }); + + function recoveringConvertImage(convertImage) { + return function(image, html, messages, callback) { + convertImage(image, html, messages, function(error) { + if (error) { + messages.push(results.warning(error.message)); + } + callback(); + }); + }; + } function noteHtmlId(note) { return htmlId(note.noteType + "-" + note.noteId); @@ -315,7 +326,7 @@ function DocumentConversion(options) { } }); }, - "image": options.convertImage || defaultConvertImage, + "image": recoveringConvertImage(options.convertImage || defaultConvertImage), "table": convertTable, "tableRow": convertTableRow, "tableCell": convertTableCell, diff --git a/lib/docx/files.js b/lib/docx/files.js index 2d0963b..0e95197 100644 --- a/lib/docx/files.js +++ b/lib/docx/files.js @@ -2,6 +2,7 @@ var fs = require("fs"); var url = require("url"); var os = require("os"); var resolvePath = require("path").resolve; +var isAbsolutePath = require('path-is-absolute'); var promises = require("../promises"); @@ -12,7 +13,20 @@ exports.uriToPath = uriToPath; function Files(base) { function read(uri, encoding) { - return readFile(resolvePath(base, uriToPath(uri)), encoding); + return resolveUri(uri).then(function(path) { + return readFile(path, encoding); + }); + } + + function resolveUri(uri) { + var path = uriToPath(uri); + if (isAbsolutePath(path)) { + return promises.resolve(path); + } else if (base) { + return promises.resolve(resolvePath(base, path)); + } else { + return promises.reject(new Error("could not find external image '" + uri + "', path of input document is unknown")); + } } return { diff --git a/package.json b/package.json index f38cff3..d3b6c4c 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "async": "~0.2.6", "nomnom": "~1.6.2", "jszip": "~2.5.0", - "xmlbuilder": "~2.6.4" + "xmlbuilder": "~2.6.4", + "path-is-absolute": "^1.0.0" }, "devDependencies": { "mocha": "~2.2.5", diff --git a/tests/docx/files.tests.js b/tests/docx/files.tests.js index e37df6b..a00b60b 100644 --- a/tests/docx/files.tests.js +++ b/tests/docx/files.tests.js @@ -32,8 +32,21 @@ describe("Files", function() { }); }); }); + + test("given base is not set when opening relative uri then error is raised", function() { + var files = new Files(null); + return assertError(files.read("not-a-real-file.png", "base64"), function(err) { + assert.equal(err.message, "could not find external image 'not-a-real-file.png', path of input document is unknown"); + }); + }); }); +function assertError(promise, func) { + return promise.then(function() { + assert(false, "Expected error"); + }, func); +} + describe("uriToPath", function() { test("leading slash is retained on non-Windows file URIs", function() { diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js index bb3dca2..10b95ef 100644 --- a/tests/mammoth.tests.js +++ b/tests/mammoth.tests.js @@ -188,7 +188,16 @@ describe('mammoth', function() { var docxPath = path.join(__dirname, "test-data/external-picture.docx"); return mammoth.convertToHtml({path: docxPath}).then(function(result) { assert.equal(result.value, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>'); - assert.deepEqual([], result.messages); + assert.deepEqual(result.messages, []); + }); + }); + + test('warn if images stored outside of document are specified when passing file without path', function() { + var docxPath = path.join(__dirname, "test-data/external-picture.docx"); + var buffer = fs.readFileSync(docxPath); + return mammoth.convertToHtml({buffer: buffer}).then(function(result) { + assert.equal(result.value, ''); + assert.deepEqual(result.messages, [results.warning("could not find external image 'tiny-picture.png', path of input document is unknown")]); }); }); |
