diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-09-06 11:32:11 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-09-06 11:34:31 +0100 |
| commit | ca74aa92bdc45f7777a66304745b3cf153a6c98e (patch) | |
| tree | 7f7ebddc90d79363958dfd6c4d7edb9fd0a3e6e3 | |
| parent | e1344467f2301c122f8ce00b2ddf29db92bc90d2 (diff) | |
Add support for reading file URIs
| -rw-r--r-- | lib/docx/files.js | 27 | ||||
| -rw-r--r-- | tests/docx/files.tests.js | 24 | ||||
| -rw-r--r-- | tests/test-data/tiny-picture.png | bin | 0 -> 110 bytes |
3 files changed, 51 insertions, 0 deletions
diff --git a/lib/docx/files.js b/lib/docx/files.js new file mode 100644 index 0000000..6596a01 --- /dev/null +++ b/lib/docx/files.js @@ -0,0 +1,27 @@ +var fs = require("fs"); + +var promises = require("../promises"); + + +exports.Files = Files; + + +function Files() { + function read(path, encoding) { + // TODO: check whether this works for Windows file URIs + var fileUriPrefix = "file:///"; + if (startsWith(path, fileUriPrefix)) { + return readFile(path.slice(fileUriPrefix.length - 1), encoding); + } + } + + return { + read: read + }; +} + +var readFile = promises.promisify(fs.readFile.bind(fs)); + +function startsWith(string, search) { + return string.indexOf(search) === 0; +} diff --git a/tests/docx/files.tests.js b/tests/docx/files.tests.js new file mode 100644 index 0000000..d3f3391 --- /dev/null +++ b/tests/docx/files.tests.js @@ -0,0 +1,24 @@ +var path = require("path"); +var fs = require("fs"); +var assert = require("assert"); + +var promises = require("../../lib/promises"); +var Files = require("../../lib/docx/files").Files; + +var testing = require("../testing"); +var test = testing.test; + +var readFile = promises.promisify(fs.readFile.bind(fs)); + + +describe("Files", function() { + test("can open files with file URI", function() { + var filePath = path.resolve(testing.testPath("tiny-picture.png")); + var files = new Files(null); + return files.read("file:///" + filePath.replace(/^\//, ""), "base64").then(function(contents) { + return readFile(filePath, "base64").then(function(expectedContents) { + assert.deepEqual(contents, expectedContents); + }); + }); + }); +}); diff --git a/tests/test-data/tiny-picture.png b/tests/test-data/tiny-picture.png Binary files differnew file mode 100644 index 0000000..74cb4ba --- /dev/null +++ b/tests/test-data/tiny-picture.png |
