blob: 0a74eb94c4a68e853620c5caa6329117cf6e5247 (
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
42
43
44
45
46
47
48
49
50
|
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 = {};
var overrides = {};
xml.root.children.forEach(function(child) {
if (child.name === "content-types:Default") {
extensionDefaults[child.attributes.Extension] = child.attributes.ContentType;
}
if (child.name === "content-types:Override") {
var name = child.attributes.PartName;
if (name.charAt(0) === "/") {
name = name.substring(1);
}
overrides[name] = child.attributes.ContentType;
}
});
return {
findContentType: function(path) {
var overrideContentType = overrides[path];
if (overrideContentType) {
return overrideContentType;
} else {
var pathParts = path.split(".");
var extension = pathParts[pathParts.length - 1];
return extensionDefaults[extension];
}
}
}
}
var defaultContentTypes = {
findContentType: function(path) {
return null;
}
};
|