diff options
Diffstat (limited to 'lib/docx/content-types-reader.js')
| -rw-r--r-- | lib/docx/content-types-reader.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/docx/content-types-reader.js b/lib/docx/content-types-reader.js new file mode 100644 index 0000000..0a74eb9 --- /dev/null +++ b/lib/docx/content-types-reader.js @@ -0,0 +1,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; + } +}; |
