aboutsummaryrefslogtreecommitdiff
path: root/lib/docx/files.js
blob: e17bd456955c2370dfed3736b2bfdbe68556edfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var fs = require("fs");
var url = require("url");
var os = require("os");

var promises = require("../promises");


exports.Files = Files;
exports.uriToPath = uriToPath;


function Files() {
    function read(uri, encoding) {
        return readFile(uriToPath(uri), encoding);
    }
    
    return {
        read: read
    };
}

var readFile = promises.promisify(fs.readFile.bind(fs));


function uriToPath(uriString, platform) {
    if (!platform) {
        platform = os.platform();
    }
    
    var uri = url.parse(uriString);
    if (uri.protocol === "file:" && (!uri.host || uri.host === "localhost")) {
        var path = decodeURIComponent(uri.path);
        if (platform === "win32" && /^\/[a-z]:/i.test(path)) {
            return path.slice(1);
        } else {
            return path;
        }
    } else {
        throw new Error("Could not convert URI to path: " + uriString);
    }
}