feature: save uploaded files to album if specified

This commit is contained in:
Pitu 2019-10-12 17:52:49 +09:00
parent c385f8b30a
commit e6eb13e5cd
1 changed files with 21 additions and 7 deletions

View File

@ -31,9 +31,6 @@ class uploadPOST extends Route {
const user = await Util.isAuthorized(req);
if (!user && process.env.PUBLIC_MODE == 'false') return res.status(401).json({ message: 'Not authorized to use this resource' });
return upload(req, res, async err => {
if (err) console.error(err.message);
const albumId = req.body.albumid || req.headers.albumid;
if (albumId && !user) return res.status(401).json({ message: 'Only registered users can upload files to an album' });
if (albumId && user) {
@ -41,9 +38,13 @@ class uploadPOST extends Route {
if (!album) return res.status(401).json({ message: 'Album doesn\'t exist or it doesn\'t belong to the user' });
}
return upload(req, res, async err => {
if (err) console.error(err.message);
let uploadedFile = {};
let originalFile;
let insertedId;
const now = moment.utc().toDate();
const remappedKeys = this._remapKeys(req.body);
for (const file of req.files) {
@ -87,6 +88,19 @@ class uploadPOST extends Route {
uploadedFile.deleteUrl = `${process.env.DOMAIN}/api/file/${insertedId[0]}`;
}
/*
If the upload had an album specified we make sure to create the relation
and update the according timestamps..
*/
if (albumId) {
try {
await db.table('albumsFiles').insert({ albumId, fileId: insertedId[0] });
await db.table('albums').where('id', albumId).update('editedAt', now);
} catch (error) {
console.error(error);
}
}
return res.status(201).send({
message: 'Sucessfully uploaded the file.',
...uploadedFile