diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-04-11 23:00:06 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-04-11 23:00:06 +0100 |
| commit | 90cf7be66b7ecbe11066fafcdcacf8f0c6bcf3e9 (patch) | |
| tree | 9f8d0f3e98f70982661589fcce2163eec39edbc3 /lib/docx-reader.js | |
| parent | 1d9da5d8ed67e3f31ebda8f12082e6e607c673f0 (diff) | |
Move unzip code into separate module
Diffstat (limited to 'lib/docx-reader.js')
| -rw-r--r-- | lib/docx-reader.js | 70 |
1 files changed, 3 insertions, 67 deletions
diff --git a/lib/docx-reader.js b/lib/docx-reader.js index 09bc8c1..455bb02 100644 --- a/lib/docx-reader.js +++ b/lib/docx-reader.js @@ -1,14 +1,9 @@ exports.read = read; var q = require("q"); -var _ = require("underscore"); -var unzip = { - Parse: require("unzip/lib/parse") -}; +var unzip = require("./unzip"); var path = require("path"); var fs = require("fs"); -var Buffers = require("buffers"); -var streamBuffers = require("stream-buffers"); var xmlreader = require("./xmlreader"); var documents = require("./documents"); @@ -19,15 +14,11 @@ var Result = require("./results").Result; function read(options) { var docxFile; if (options.path) { - var zipPath = docxFile; - docxFile = openZipStream(fs.createReadStream(options.path)); + docxFile = unzip.unzipStream(fs.createReadStream(options.path)); } else if (options.file) { docxFile = options.file; } else if (options.uint8Array) { - var buffer = new Buffer(options.uint8Array); - var stream = new streamBuffers.ReadableStreamBuffer(); - stream.put(buffer); - docxFile = openZipStream(stream); + docxFile = unzip.unzipUint8Array(options.uint8Array); } else { return q.reject(new Error("Could not find file in options")); } @@ -44,61 +35,6 @@ function read(options) { }); } -function openZipStream(stream) { - var deferred = q.defer(); - var names = []; - var files = {}; - - stream - .pipe(unzip.Parse()) - .on("entry", function(entry) { - names.push(entry.path); - files[entry.path] = readEntry(entry); - }) - .on("close", function() { - deferred.resolve(zipFile); - }) - .on("error", function(err) { - deferred.reject(err); - }); - - var exists = function(path) { - return names.indexOf(path) !== -1; - } - - function readEntry(entry) { - var readDeferred = q.defer(); - var buffers = new Buffers(); - - entry - .on("data", function(data) { - buffers.push(data); - }) - .on("end", function() { - readDeferred.resolve(buffers); - }) - .on("error", readDeferred.reject.bind(readDeferred)) - - return readDeferred.promise; - } - var zipFile = { - read: function(path, encoding) { - if (!exists(path)) { - return q.reject(new Error("No file in zip: " + path)); - } - return files[path].then(function(buffer) { - if (encoding) { - return buffer.toString(encoding); - } else { - return buffer; - } - }); - }, - exists: exists - }; - return deferred.promise; -} - function readXmlFile(docxFile, path) { var xmlNamespaceMap = { "http://schemas.openxmlformats.org/wordprocessingml/2006/main": "w", |
