aboutsummaryrefslogtreecommitdiff
path: root/lib/content-types-reader.js
blob: 0346b8c4e761d831e71ac23a0fb211e2bb4c65c1 (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
var readXmlFromZipFile = require("./office-xml-reader").readXmlFromZipFile;

exports.readContentTypesFromZipFile = readContentTypesFromZipFile;
exports.readContentTypesFromXml = readContentTypesFromXml;


function readContentTypesFromZipFile(file) {
    return readXmlFromZipFile(file, "[Content_Types].xml")
        .then(function(xml) {
            return xml ? readContentTypesFromXml(xml) : defaultContentTypes;
        });
}

function readContentTypesFromXml(xml) {
    var extensionDefaults = {};
    
    xml.root.children.forEach(function(child) {
        if (child.name === "content-types:Default") {
            extensionDefaults[child.attributes.Extension] = child.attributes.ContentType;
        }
    });
    
    return {
        findContentType: function(path) {
            var pathParts = path.split(".");
            var extension = pathParts[pathParts.length - 1];
            return extensionDefaults[extension];
        }
    }
}


var defaultContentTypes = {
    findContentType: function(path) {
        return null;
    }
};