summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-27 23:01:49 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-27 23:01:49 +0100
commit43db35ec5e5125055fa8ccfdef17d077a63f31f9 (patch)
tree8a82c18817995606f2aca482e1240333b0b1cdb9
parente1f18ccd40a40c8f9001517c9f6aa56a797a9110 (diff)
Read [Content_Types].xml to find content types of files
-rw-r--r--lib/content-types-reader.js29
-rw-r--r--lib/office-xml-reader.js3
-rw-r--r--tests/content-types-reader.tests.js17
3 files changed, 47 insertions, 2 deletions
diff --git a/lib/content-types-reader.js b/lib/content-types-reader.js
index 5240d21..0346b8c 100644
--- a/lib/content-types-reader.js
+++ b/lib/content-types-reader.js
@@ -1,10 +1,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) {
- return "image/png";
+ var pathParts = path.split(".");
+ var extension = pathParts[pathParts.length - 1];
+ return extensionDefaults[extension];
}
}
}
+
+
+var defaultContentTypes = {
+ findContentType: function(path) {
+ return null;
+ }
+};
diff --git a/lib/office-xml-reader.js b/lib/office-xml-reader.js
index 7aa8bab..04b73fb 100644
--- a/lib/office-xml-reader.js
+++ b/lib/office-xml-reader.js
@@ -10,7 +10,8 @@ var xmlNamespaceMap = {
"http://schemas.openxmlformats.org/officeDocument/2006/relationships": "r",
"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing": "wp",
"http://schemas.openxmlformats.org/drawingml/2006/main": "a",
- "http://schemas.openxmlformats.org/drawingml/2006/picture": "pic"
+ "http://schemas.openxmlformats.org/drawingml/2006/picture": "pic",
+ "http://schemas.openxmlformats.org/package/2006/content-types": "content-types"
};
diff --git a/tests/content-types-reader.tests.js b/tests/content-types-reader.tests.js
new file mode 100644
index 0000000..7cd7295
--- /dev/null
+++ b/tests/content-types-reader.tests.js
@@ -0,0 +1,17 @@
+var assert = require("assert");
+
+var readContentTypesFromXml = require("../lib/content-types-reader").readContentTypesFromXml;
+var XmlElement = require("../lib/xmlreader").Element;
+var test = require("./testing").test;
+
+
+describe('readContentTypesFromXml', function() {
+ test('reads default-per-extension from XML', function() {
+ var contentTypes = readContentTypesFromXml({
+ root: new XmlElement("content-types:Types", {}, [
+ new XmlElement("content-types:Default", {Extension: "png", ContentType: "image/png"})
+ ])
+ });
+ assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/png");
+ });
+});