v3.0.0/src/api/routes/albums/albumZipGET.js

90 lines
2.7 KiB
JavaScript
Raw Normal View History

const path = require('path');
const jetpack = require('fs-jetpack');
2018-09-18 08:34:00 +02:00
const Route = require('../../structures/Route');
const Util = require('../../utils/Util');
const log = require('../../utils/Log');
class albumGET extends Route {
constructor() {
super('/album/:identifier/zip', 'get', { bypassAuth: true });
}
2019-02-19 15:52:24 +01:00
async run(req, res, db) {
2018-09-18 08:34:00 +02:00
const { identifier } = req.params;
if (!identifier) return res.status(400).json({ message: 'Invalid identifier supplied' });
2020-05-10 17:19:10 +02:00
// TODO: Do we really want to let anyone create a zip of an album?
2018-09-18 08:34:00 +02:00
/*
Make sure it exists and it's enabled
*/
2019-03-12 07:03:15 +01:00
const link = await db.table('links')
2019-09-30 09:06:22 +02:00
.where({
identifier,
enabled: true,
2020-12-24 09:40:50 +01:00
enableDownload: true
2019-09-30 09:06:22 +02:00
})
2019-03-12 07:03:15 +01:00
.first();
2019-09-30 09:06:22 +02:00
if (!link) return res.status(400).json({ message: 'The supplied identifier could not be found' });
2018-09-18 08:34:00 +02:00
/*
Same with the album, just to make sure is not a deleted album and a leftover link
*/
2019-03-12 07:03:15 +01:00
const album = await db.table('albums')
.where('id', link.albumId)
.first();
2018-09-18 08:34:00 +02:00
if (!album) return res.status(400).json({ message: 'Album not found' });
/*
If the date when the album was zipped is greater than the album's last edit, we just send the zip to the user
*/
if (album.zippedAt > album.editedAt) {
2020-12-24 09:40:50 +01:00
const filePath = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER, 'zips', `${album.userId}-${album.id}.zip`);
2018-09-18 08:34:00 +02:00
const exists = await jetpack.existsAsync(filePath);
/*
Make sure the file exists just in case, and if not, continue to it's generation.
*/
if (exists) {
2020-12-25 12:45:22 +01:00
const fileName = `chibisafe-${identifier}.zip`;
2018-09-18 08:34:00 +02:00
return res.download(filePath, fileName);
}
}
/*
Grab the files in a very unoptimized way. (This should be a join between both tables)
*/
2019-03-12 07:03:15 +01:00
const fileList = await db.table('albumsFiles')
.where('albumId', link.albumId)
.select('fileId');
2018-09-18 08:34:00 +02:00
/*
If there are no files, stop here
*/
2019-03-12 07:03:15 +01:00
if (!fileList || !fileList.length) return res.status(400).json({ message: 'Can\'t download an empty album' });
2018-09-18 08:34:00 +02:00
/*
Get the actual files
*/
2020-12-24 15:45:16 +01:00
const fileIds = fileList.map(el => el.fileId);
2018-09-18 08:34:00 +02:00
const files = await db.table('files')
.whereIn('id', fileIds)
.select('name');
2020-12-24 15:45:16 +01:00
const filesToZip = files.map(el => el.name);
2018-09-18 08:34:00 +02:00
try {
Util.createZip(filesToZip, album);
2019-03-12 07:03:15 +01:00
await db.table('albums')
.where('id', link.albumId)
.update('zippedAt', db.fn.now());
2018-09-18 08:34:00 +02:00
2020-12-24 09:40:50 +01:00
const filePath = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER, 'zips', `${album.userId}-${album.id}.zip`);
2020-12-25 12:45:22 +01:00
const fileName = `chibisafe-${identifier}.zip`;
2018-09-18 08:34:00 +02:00
return res.download(filePath, fileName);
} catch (error) {
log.error(error);
return res.status(500).json({ message: 'There was a problem downloading the album' });
}
}
}
module.exports = albumGET;