From 97144ee9f557ef4886456972d36583b6b62685d2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 6 Sep 2015 17:57:18 +0100 Subject: Support relative URIs --- lib/docx/files.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/docx/files.js b/lib/docx/files.js index e17bd45..2d0963b 100644 --- a/lib/docx/files.js +++ b/lib/docx/files.js @@ -1,6 +1,7 @@ var fs = require("fs"); var url = require("url"); var os = require("os"); +var resolvePath = require("path").resolve; var promises = require("../promises"); @@ -9,9 +10,9 @@ exports.Files = Files; exports.uriToPath = uriToPath; -function Files() { +function Files(base) { function read(uri, encoding) { - return readFile(uriToPath(uri), encoding); + return readFile(resolvePath(base, uriToPath(uri)), encoding); } return { @@ -28,7 +29,7 @@ function uriToPath(uriString, platform) { } var uri = url.parse(uriString); - if (uri.protocol === "file:" && (!uri.host || uri.host === "localhost")) { + if (isLocalFileUri(uri) || isRelativeUri(uri)) { var path = decodeURIComponent(uri.path); if (platform === "win32" && /^\/[a-z]:/i.test(path)) { return path.slice(1); @@ -39,3 +40,11 @@ function uriToPath(uriString, platform) { throw new Error("Could not convert URI to path: " + uriString); } } + +function isLocalFileUri(uri) { + return uri.protocol === "file:" && (!uri.host || uri.host === "localhost"); +} + +function isRelativeUri(uri) { + return !uri.protocol && !uri.host; +} -- cgit v1.2.3