diff options
Diffstat (limited to 'controllers')
| -rw-r--r-- | controllers/albumsController.js | 43 | ||||
| -rw-r--r-- | controllers/uploadController.js | 10 |
2 files changed, 51 insertions, 2 deletions
diff --git a/controllers/albumsController.js b/controllers/albumsController.js index 57597ca..c1a36f1 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -3,7 +3,8 @@ const db = require('knex')(config.database); const randomstring = require('randomstring'); const utils = require('./utilsController.js'); const path = require('path'); - +const fs = require('fs'); +const Zip = require('jszip'); const albumsController = {}; albumsController.list = async (req, res, next) => { @@ -129,4 +130,44 @@ albumsController.get = async (req, res, next) => { }); }; + +albumsController.generateZip = async (req, res, next) => { + const identifier = req.params.identifier; + if (identifier === undefined) return res.status(401).json({ success: false, description: 'No identifier provided' }); + if (!config.uploads.generateZips) return res.status(401).json({ success: false, description: 'Zip generation disabled' }); + + const album = await db.table('albums').where({ identifier, enabled: 1 }).first(); + if (!album) return res.json({ success: false, description: 'Album not found' }); + + if (album.zipGeneratedAt > album.editedAt) { + const filePath = path.join(config.uploads.folder, 'zips', `${identifier}.zip`); + const fileName = `${album.name}.zip`; + return res.download(filePath, fileName); + } else { + console.log(`Generating zip for album identifier: ${identifier}`); + const files = await db.table('files').select('name').where('albumid', album.id); + if (files.length === 0) return res.json({ success: false, description: 'There are no files in the album' }); + + const zipPath = path.join(__dirname, '..', config.uploads.folder, 'zips', `${album.identifier}.zip`); + let archive = new Zip(); + + for (let file of files) { + archive.file(file.name, fs.readFileSync(path.join(__dirname, '..', config.uploads.folder, file.name))); + } + + archive + .generateNodeStream({ type: 'nodebuffer', streamFiles: true }) + .pipe(fs.createWriteStream(zipPath)) + .on('finish', async () => { + await db.table('albums') + .where('id', album.id) + .update({ zipGeneratedAt: Math.floor(Date.now() / 1000) }); + + const filePath = path.join(config.uploads.folder, 'zips', `${identifier}.zip`); + const fileName = `${album.name}.zip`; + return res.download(filePath, fileName); + }); + } +}; + module.exports = albumsController; diff --git a/controllers/uploadController.js b/controllers/uploadController.js index f0f75f6..42fccf5 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -42,7 +42,7 @@ uploadsController.upload = async (req, res, next) => { const albumid = req.headers.albumid || req.params.albumid; if (albumid && user) { - const album = await db.table('albums').where({ id: album, userid: user.id }).first(); + const album = await db.table('albums').where({ id: albumid, userid: user.id }).first(); if (!album) { return res.json({ success: false, @@ -150,6 +150,11 @@ uploadsController.processFilesForDisplay = async (req, res, files, existingFiles file.thumb = `${basedomain}/thumbs/${file.name.slice(0, -ext.length)}.png`; utils.generateThumbs(file); } + + if (file.albumid) { + db.table('albums').where('id', file.albumid).update('editedAt', file.timestamp).then(() => {}) + .catch(error => { console.log(error); res.json({ success: false, description: 'Error updating album' }); }); + } } }; @@ -172,6 +177,9 @@ uploadsController.delete = async (req, res) => { try { await uploadsController.deleteFile(file.name); await db.table('files').where('id', id).del(); + if (file.albumid) { + await db.table('albums').where('id', file.albumid).update('editedAt', Math.floor(Date.now() / 1000)); + } } catch (err) { console.log(err); } |