From 03c9f1b930697b07e0a15e8d09339ab9f909d226 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 8 Mar 2018 10:43:24 -0800 Subject: [PATCH] Improved robustness of extractFramesFromZip. - changed method of processing files in zip from fixed naming, to iterating the list of files that come back from JSZip. This approach is sensitive to the order the files are returned and assume the zip files are ordered by frame number. --- vatic.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/vatic.js b/vatic.js index d222fe0..93702ed 100644 --- a/vatic.js +++ b/vatic.js @@ -132,19 +132,23 @@ function extractFramesFromZip(config, file) { .loadAsync(file) .then((zip) => { let totalFrames = 0; - for (let i = 0; ; i++) { - let file = zip.file(i + config.imageExtension); - if (file == null) { - totalFrames = i; - break; + for (const file in zip.files) { + if (zip.files.hasOwnProperty(file)) { + totalFrames++; } } - + resolve({ totalFrames: () => { return totalFrames; }, getFrame: (frameNumber) => { return new Promise((resolve, _) => { - let file = zip.file(frameNumber + config.imageExtension); + if (frameNumber < 0 || frameNumber > totalFrames) { + throw new Error(`Invalid frameNumber in getFrame(). frameNumber is ${frameNumber} but there are ${totalFrames}.`); + } + + const key = Object.keys(zip.files)[frameNumber]; + const file = zip.files[key]; + file .async('arraybuffer') .then((content) => {