From cf20bdbd1a099bc2aaa96c916ffb052e419a9736 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Thu, 18 Jul 2019 06:22:47 +0700 Subject: [PATCH] Updated albumsController.js Allow JSZip to be configured from config.js file. --- config.sample.js | 15 ++++++++++++++- controllers/albumsController.js | 17 +++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/config.sample.js b/config.sample.js index a50d26a..2701a38 100644 --- a/config.sample.js +++ b/config.sample.js @@ -293,7 +293,20 @@ module.exports = { The file is generated when the user clicks the download button in the view and is re-used if the album has not changed between download requests. */ - generateZips: true + generateZips: true, + + /* + JSZip's options to use when generating album ZIPs. + https://stuk.github.io/jszip/documentation/api_jszip/generate_async.html + NOTE: Changing this option will not re-generate existing ZIPs. + */ + jsZipOptions: { + streamFiles: true, + compression: 'DEFLATE', + compressionOptions: { + level: 1 + } + } }, /* diff --git a/controllers/albumsController.js b/controllers/albumsController.js index dc9b660..ac3fdb1 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -15,6 +15,16 @@ const uploadsDir = path.join(__dirname, '..', config.uploads.folder) const zipsDir = path.join(uploadsDir, 'zips') const zipMaxTotalSize = config.cloudflare.zipMaxTotalSize const zipMaxTotalSizeBytes = parseInt(config.cloudflare.zipMaxTotalSize) * 1000000 +const zipOptions = config.uploads.jsZipOptions + +// Force 'type' option to 'nodebuffer' +zipOptions.type = 'nodebuffer' + +// Apply fallbacks for missing config values +if (zipOptions.streamFiles === undefined) zipOptions.streamFiles = true +if (zipOptions.compression === undefined) zipOptions.compression = 'DEFLATE' +if (zipOptions.compressionOptions === undefined || zipOptions.compressionOptions.level === undefined) + zipOptions.compressionOptions = { level: 1 } albumsController.zipEmitters = new Map() @@ -402,12 +412,7 @@ albumsController.generateZip = async (req, res, next) => { iteration++ if (iteration === files.length) archive - .generateNodeStream({ - type: 'nodebuffer', - streamFiles: true, - compression: 'DEFLATE', - compressionOptions: { level: 1 } - }) + .generateNodeStream(zipOptions) .pipe(fs.createWriteStream(zipPath)) .on('finish', async () => { console.log(`Finished zip task for album: ${identifier} (success).`)