From 43db35ec5e5125055fa8ccfdef17d077a63f31f9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 27 Apr 2013 23:01:49 +0100 Subject: Read [Content_Types].xml to find content types of files --- lib/content-types-reader.js | 29 ++++++++++++++++++++++++++++- lib/office-xml-reader.js | 3 ++- 2 files changed, 30 insertions(+), 2 deletions(-) (limited to 'lib') 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" }; -- cgit v1.2.3