diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-09-06 17:57:18 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-09-06 17:57:18 +0100 |
| commit | 97144ee9f557ef4886456972d36583b6b62685d2 (patch) | |
| tree | ce4265dc24f418db6d1738bd5043067d56ba7856 /lib | |
| parent | ced42637087b386df99cfd017262afad03b809ce (diff) | |
Support relative URIs
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/files.js | 15 |
1 files changed, 12 insertions, 3 deletions
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; +} |
