diff --git a/public/language/en_GB/error.json b/public/language/en_GB/error.json index 4efb4cd52f..251db61998 100644 --- a/public/language/en_GB/error.json +++ b/public/language/en_GB/error.json @@ -59,7 +59,8 @@ "cant-ban-other-admins": "You can't ban other admins!", - "invalid-image-type": "Invalid image type", + "invalid-image-type": "Invalid image type. Allowed types are: %1", + "invalid-image-extension": "Invalid image extension", "group-name-too-short": "Group name too short", "group-already-exists": "Group already exists", @@ -88,5 +89,6 @@ "reload-failed": "NodeBB encountered a problem while reloading: \"%1\". NodeBB will continue to serve the existing client-side assets, although you should undo what you did just prior to reloading.", - "registration-error": "Registration Error" + "registration-error": "Registration Error", + "parse-error": "Something went wrong while parsing server response" } \ No newline at end of file diff --git a/public/language/en_GB/uploads.json b/public/language/en_GB/uploads.json new file mode 100644 index 0000000000..5dc53ede1f --- /dev/null +++ b/public/language/en_GB/uploads.json @@ -0,0 +1,5 @@ +{ + "uploading-file" : "Uploading the file...", + "select-file-to-upload": "Select a file to upload!", + "upload-success": "File uploaded successfully!" +} \ No newline at end of file diff --git a/public/src/modules/uploader.js b/public/src/modules/uploader.js index 91eb439a25..d2f7612c0e 100644 --- a/public/src/modules/uploader.js +++ b/public/src/modules/uploader.js @@ -30,16 +30,16 @@ define('uploader', ['csrf'], function(csrf) { function showAlert(type, message) { module.hideAlerts(); - uploadModal.find('#alert-' + type).text(message).removeClass('hide'); + uploadModal.find('#alert-' + type).translateText(message).removeClass('hide'); } - showAlert('status', 'uploading the file ...'); + showAlert('status', '[[uploads:uploading-file]]'); uploadModal.find('#upload-progress-bar').css('width', '0%'); uploadModal.find('#upload-progress-box').show().removeClass('hide'); if (!$('#userPhotoInput').val()) { - showAlert('error', 'select an image to upload!'); + showAlert('error', '[[uploads:select-file-to-upload]]'); return false; } @@ -49,7 +49,7 @@ define('uploader', ['csrf'], function(csrf) { }, error: function(xhr) { xhr = maybeParse(xhr); - showAlert('error', 'Error: ' + xhr.status); + showAlert('error', xhr.responseJSON.error); }, uploadProgress: function(event, position, total, percent) { @@ -66,7 +66,7 @@ define('uploader', ['csrf'], function(csrf) { callback(response[0].url); - showAlert('success', 'File uploaded successfully!'); + showAlert('success', '[[uploads:upload-success]]'); setTimeout(function() { module.hideAlerts(); uploadModal.modal('hide'); @@ -83,7 +83,7 @@ define('uploader', ['csrf'], function(csrf) { try { return $.parseJSON(response); } catch (e) { - return {error: 'Something went wrong while parsing server response'}; + return {error: '[[error:parse-error]]'}; } } return response; diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js index 69ea848ec0..fd972ccd3b 100644 --- a/src/controllers/accounts.js +++ b/src/controllers/accounts.js @@ -395,25 +395,19 @@ accountsController.uploadPicture = function (req, res, next) { if (userPhoto.size > uploadSize * 1024) { fs.unlink(userPhoto.path); - return res.json({ - error: 'Images must be smaller than ' + uploadSize + ' kb!' - }); + return next(new Error('[[error:file-too-big, ' + uploadSize + ']]')); } var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']; if (allowedTypes.indexOf(userPhoto.type) === -1) { fs.unlink(userPhoto.path); - return res.json({ - error: 'Allowed image types are png, jpg and gif!' - }); + return next(new Error('[[error:invalid-image-type, ' + allowedTypes.join(', ') + ']]')); } var extension = path.extname(userPhoto.name); if (!extension) { fs.unlink(userPhoto.path); - return res.json({ - error: 'Error uploading file! Error : Invalid extension!' - }); + return next(new Error('[[error:invalid-image-extension]]')); } var updateUid = req.user ? req.user.uid : 0; @@ -465,7 +459,7 @@ accountsController.uploadPicture = function (req, res, next) { if (err) { fs.unlink(userPhoto.path); - return res.json({error:err.message}); + return next(err); } if (plugins.hasListeners('filter:uploadImage')) { @@ -476,6 +470,9 @@ accountsController.uploadPicture = function (req, res, next) { var filename = updateUid + '-profileimg' + (convertToPNG ? '.png' : extension); user.getUserField(updateUid, 'uploadedpicture', function (err, oldpicture) { + if (err) { + return next(err); + } if (!oldpicture) { file.saveFileToLocal(filename, 'profile', userPhoto.path, done); return; diff --git a/src/controllers/admin/uploads.js b/src/controllers/admin/uploads.js index 0b7ece2775..9ba3ad049c 100644 --- a/src/controllers/admin/uploads.js +++ b/src/controllers/admin/uploads.js @@ -16,16 +16,13 @@ uploadsController.uploadCategoryPicture = function(req, res, next) { try { params = JSON.parse(req.body.params); } catch (e) { - var err = { - error: 'Error uploading file! Error :' + e.message - }; fs.unlink(uploadedFile.path); - return res.send(req.xhr ? err : JSON.stringify(err)); + return next(e); } - if (validateUpload(req, res, uploadedFile, allowedTypes)) { + if (validateUpload(req, res, next, uploadedFile, allowedTypes)) { var filename = 'category-' + params.cid + path.extname(uploadedFile.name); - uploadImage(filename, 'category', uploadedFile, req, res); + uploadImage(filename, 'category', uploadedFile, req, res, next); } }; @@ -33,13 +30,14 @@ uploadsController.uploadFavicon = function(req, res, next) { var uploadedFile = req.files.files[0]; var allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon']; - if (validateUpload(res, req, uploadedFile, allowedTypes)) { + if (validateUpload(res, req, next, uploadedFile, allowedTypes)) { file.saveFileToLocal('favicon.ico', 'files', uploadedFile.path, function(err, image) { fs.unlink(uploadedFile.path); + if (err) { + return next(err); + } - var response = err ? {error: err.message} : [{name: uploadedFile.name, url: image.url}]; - - res.send(req.xhr ? response : JSON.stringify(response)); + res.json([{name: uploadedFile.name, url: image.url}]); }); } }; @@ -55,33 +53,31 @@ uploadsController.uploadGravatarDefault = function(req, res, next) { function upload(name, req, res, next) { var uploadedFile = req.files.files[0]; var allowedTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif']; - if (validateUpload(req, res, uploadedFile, allowedTypes)) { + if (validateUpload(req, res, next, uploadedFile, allowedTypes)) { var filename = name + path.extname(uploadedFile.name); - uploadImage(filename, 'files', uploadedFile, req, res); + uploadImage(filename, 'files', uploadedFile, req, res, next); } } -function validateUpload(req, res, uploadedFile, allowedTypes) { +function validateUpload(req, res, next, uploadedFile, allowedTypes) { if (allowedTypes.indexOf(uploadedFile.type) === -1) { - var err = { - error: 'Invalid image type. Allowed types are: ' + allowedTypes.join(', ') - }; - fs.unlink(uploadedFile.path); - res.send(req.xhr ? err : JSON.stringify(err)); + + next(new Error('[[error:invalid-image-type, ' + allowedTypes.join(', ') + ']]')); return false; } return true; } -function uploadImage(filename, folder, uploadedFile, req, res) { +function uploadImage(filename, folder, uploadedFile, req, res, next) { function done(err, image) { fs.unlink(uploadedFile.path); + if (err) { + return next(err); + } - var response = err ? {error: err.message} : [{name: uploadedFile.name, url: image.url}]; - - res.send(req.xhr ? response : JSON.stringify(response)); + res.json([{name: uploadedFile.name, url: image.url}]); } if (plugins.hasListeners('filter:uploadImage')) { diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 3620800e7a..023a823f88 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -166,7 +166,7 @@ SocketUser.changePicture = function(socket, data, callback) { } else if (type === 'uploaded') { type = 'uploadedpicture'; } else { - return callback(new Error('[[error:invalid-image-type]]')); + return callback(new Error('[[error:invalid-image-type, ' + ['gravatar', 'uploadedpicture'].join(', ') + ']]')); } if (socket.uid === parseInt(data.uid, 10)) {