fixed "add to album" error on uploads (#92)

* Updates

Fixed the way error when adding album is being handled.

* I guess that sounds about right.
This commit is contained in:
Bobby Wibowo 2018-04-26 13:51:17 +07:00 committed by Crawl
parent 19e965a77a
commit 08637b9ea9
2 changed files with 41 additions and 31 deletions

View File

@ -71,7 +71,7 @@ uploadsController.upload = async (req, res, next) => {
return uploadsController.actuallyUpload(req, res, user, albumid); 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 => { upload(req, res, async err => {
if (err) { if (err) {
console.error(err); console.error(err);
@ -114,7 +114,7 @@ uploadsController.actuallyUpload = async (req, res, userid, album) => {
size: file.size, size: file.size,
hash: fileHash, hash: fileHash,
ip: req.ip, ip: req.ip,
albumid: album, albumid: albumid,
userid: userid !== undefined ? userid.id : null, userid: userid !== undefined ? userid.id : null,
timestamp: Math.floor(Date.now() / 1000) timestamp: Math.floor(Date.now() / 1000)
}); });
@ -124,7 +124,7 @@ uploadsController.actuallyUpload = async (req, res, userid, album) => {
} }
if (iteration === req.files.length) { if (iteration === req.files.length) {
return uploadsController.processFilesForDisplay(req, res, files, existingFiles); return uploadsController.processFilesForDisplay(req, res, files, existingFiles, albumid);
} }
iteration++; 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; let basedomain = config.domain;
if (files.length === 0) { if (files.length === 0) {
return res.json({ return res.json({
@ -150,8 +150,30 @@ uploadsController.processFilesForDisplay = async (req, res, files, existingFiles
await db.table('files').insert(files); await db.table('files').insert(files);
for (let efile of existingFiles) files.push(efile); for (let efile of existingFiles) files.push(efile);
res.json({ for (let file of files) {
success: true, 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);
}
}
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 => { files: files.map(file => {
return { return {
name: file.name, name: file.name,
@ -160,19 +182,6 @@ uploadsController.processFilesForDisplay = async (req, res, files, existingFiles
}; };
}) })
}); });
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' }); });
}
}
}; };
uploadsController.delete = async (req, res) => { uploadsController.delete = async (req, res) => {

View File

@ -156,12 +156,12 @@ upload.prepareDropzone = function(){
// Handle the responseText here. For example, add the text to the preview element: // Handle the responseText here. For example, add the text to the preview element:
if (response.success === false) { if (response.success === false) {
var span = document.createElement('span'); var p = document.createElement('p');
span.innerHTML = response.description; p.innerHTML = response.description;
file.previewTemplate.querySelector('.link').appendChild(span); file.previewTemplate.querySelector('.link').appendChild(p);
return;
} }
if (response.files[0].url) {
a = document.createElement('a'); a = document.createElement('a');
a.href = response.files[0].url; a.href = response.files[0].url;
a.target = '_blank'; a.target = '_blank';
@ -169,6 +169,7 @@ upload.prepareDropzone = function(){
file.previewTemplate.querySelector('.link').appendChild(a); file.previewTemplate.querySelector('.link').appendChild(a);
file.previewTemplate.querySelector('.progress').style.display = 'none'; file.previewTemplate.querySelector('.progress').style.display = 'none';
}
}); });