diff options
Diffstat (limited to 'controllers/uploadController.js')
| -rw-r--r-- | controllers/uploadController.js | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/controllers/uploadController.js b/controllers/uploadController.js index 342dc30..d5615d0 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -71,7 +71,7 @@ uploadsController.upload = async (req, res, next) => { return uploadsController.actuallyUpload(req, res, user, albumid); }; -uploadsController.actuallyUpload = async (req, res, userid, album) => { +uploadsController.actuallyUpload = async (req, res, userid, albumid) => { upload(req, res, async err => { if (err) { console.error(err); @@ -114,7 +114,7 @@ uploadsController.actuallyUpload = async (req, res, userid, album) => { size: file.size, hash: fileHash, ip: req.ip, - albumid: album, + albumid: albumid, userid: userid !== undefined ? userid.id : null, timestamp: Math.floor(Date.now() / 1000) }); @@ -124,7 +124,7 @@ uploadsController.actuallyUpload = async (req, res, userid, album) => { } if (iteration === req.files.length) { - return uploadsController.processFilesForDisplay(req, res, files, existingFiles); + return uploadsController.processFilesForDisplay(req, res, files, existingFiles, albumid); } iteration++; }); @@ -132,7 +132,7 @@ uploadsController.actuallyUpload = async (req, res, userid, album) => { }); }; -uploadsController.processFilesForDisplay = async (req, res, files, existingFiles) => { +uploadsController.processFilesForDisplay = async (req, res, files, existingFiles, albumid) => { let basedomain = config.domain; if (files.length === 0) { return res.json({ @@ -150,29 +150,38 @@ uploadsController.processFilesForDisplay = async (req, res, files, existingFiles await db.table('files').insert(files); for (let efile of existingFiles) files.push(efile); - res.json({ - success: true, - files: files.map(file => { - return { - name: file.name, - size: file.size, - url: `${basedomain}/${file.name}` - }; - }) - }); - for (let file of files) { let ext = path.extname(file.name).toLowerCase(); if (utils.imageExtensions.includes(ext) || utils.videoExtensions.includes(ext)) { 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' }); }); - } + let albumSuccess = true; + if (albumid) { + const editedAt = Math.floor(Date.now() / 1000) + albumSuccess = await db.table('albums') + .where('id', albumid) + .update('editedAt', editedAt) + .then(() => true) + .catch(error => { + console.log(error); + return false; + }); } + + return res.json({ + success: albumSuccess, + description: albumSuccess ? null : 'Warning: Error updating album.', + files: files.map(file => { + return { + name: file.name, + size: file.size, + url: `${basedomain}/${file.name}` + }; + }) + }); }; uploadsController.delete = async (req, res) => { |