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 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'lib/content-types-reader.js') 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; + } +}; -- cgit v1.2.3