From 89d074071d250a23264e9ab82bb896af3e5ce674 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 1 Apr 2013 22:39:47 +0100 Subject: Fix image generation --- lib/docx-reader.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'lib/docx-reader.js') diff --git a/lib/docx-reader.js b/lib/docx-reader.js index eece7bb..2902943 100644 --- a/lib/docx-reader.js +++ b/lib/docx-reader.js @@ -4,6 +4,7 @@ exports.DocumentReader = DocumentReader; var q = require("q"); var _ = require("underscore"); var ZipFile = require("zipfile").ZipFile; +var path = require("path"); var xmlreader = require("./xmlreader"); var documents = require("./documents"); @@ -12,20 +13,25 @@ var documents = require("./documents"); function read(docxFile) { if (_.isString(docxFile)) { var zipFile = new ZipFile(docxFile); + var exists = function(path) { + return zipFile.names.indexOf(path) !== -1; + } docxFile = { read: function(path, encoding) { - return q.ninvoke(zipFile, "readFile", path) - .then(function(value) { - if (encoding) { - return value; - } else { - return value.toString(encoding); - } - }); + if (exists(path)) { + return q.ninvoke(zipFile, "readFile", path) + .then(function(value) { + if (encoding) { + return value.toString(encoding); + } else { + return value; + } + }); + } else { + return q.reject(new Error("No file in zip: " + path)); + } }, - exists: function(path) { - return zipFile.names.indexOf(path) !== -1; - } + exists: exists }; } @@ -41,7 +47,7 @@ function read(docxFile) { function readXmlFile(docxFile, path) { if (docxFile.exists(path)) { - return docxFile.read(path) + return docxFile.read(path, "utf-8") .then(xmlreader.read); } else { return null; @@ -168,7 +174,7 @@ function DocumentReader(relationships, docxFile) { .attributes["r:embed"]; var imagePath = relationships[relationshipId].target; var image = documents.Image(function() { - return docxFile.read(imagePath); + return docxFile.read(path.join("word", imagePath)); }) return new Result(image); } -- cgit v1.2.3