From cccaa16098484a7d9df26c18c0b6aa5b9defbeb5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 6 Sep 2015 17:35:10 +0100 Subject: Extract uriToPath --- lib/docx/files.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/docx/files.js b/lib/docx/files.js index 6596a01..6e6f797 100644 --- a/lib/docx/files.js +++ b/lib/docx/files.js @@ -4,15 +4,12 @@ var promises = require("../promises"); exports.Files = Files; +exports.uriToPath = uriToPath; 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); - } + function read(uri, encoding) { + return readFile(uriToPath(uri), encoding); } return { @@ -22,6 +19,16 @@ function Files() { var readFile = promises.promisify(fs.readFile.bind(fs)); + +function uriToPath(uri) { + var fileUriPrefix = "file:///"; + if (startsWith(uri, fileUriPrefix)) { + return uri.slice(fileUriPrefix.length - 1); + } else { + throw new Error("Could not convert URI to path: " + uri); + } +} + function startsWith(string, search) { return string.indexOf(search) === 0; } -- cgit v1.2.3