From 27e66f75f392092915f06be3c48085b15680a085 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Sun, 20 Oct 2013 20:25:00 +0000 Subject: [PATCH] Show proper error message when image upload fails fixes #994 --- core/client/assets/lib/uploader.js | 9 ++++++++- core/server/controllers/admin.js | 2 +- core/test/unit/admin_spec.js | 12 ++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/core/client/assets/lib/uploader.js b/core/client/assets/lib/uploader.js index aecd95134b..d27086d498 100644 --- a/core/client/assets/lib/uploader.js +++ b/core/client/assets/lib/uploader.js @@ -94,6 +94,13 @@ fail: function (e, data) { $dropzone.trigger("uploadfailure", [data.result]); $dropzone.find('.js-upload-progress-bar').addClass('fail'); + if (data.jqXHR.status === 413) { + $dropzone.find('div.js-fail').text("The image you uploaded was too big."); + } else if (data.jqXHR.status === 415) { + $dropzone.find('div.js-fail').text("The image type you uploaded is not supported. Please use .PNG, .JPG, .GIF."); + } else { + $dropzone.find('div.js-fail').text("Something went wrong :("); + } $dropzone.find('div.js-fail, button.js-fail').fadeIn(1500); $dropzone.find('button.js-fail').on('click', function () { $dropzone.css({minHeight: 0}); @@ -233,4 +240,4 @@ ui.init(); }); }; -}(jQuery)); \ No newline at end of file +}(jQuery)); diff --git a/core/server/controllers/admin.js b/core/server/controllers/admin.js index db2e686d37..665e19e9f8 100644 --- a/core/server/controllers/admin.js +++ b/core/server/controllers/admin.js @@ -114,7 +114,7 @@ adminControllers = { renameFile(filename); }); } else { - res.send(403, 'Invalid file type'); + res.send(415, 'Unsupported Media Type'); } }, 'login': function (req, res) { diff --git a/core/test/unit/admin_spec.js b/core/test/unit/admin_spec.js index 6095ca8daf..22b40d6684 100644 --- a/core/test/unit/admin_spec.js +++ b/core/test/unit/admin_spec.js @@ -31,26 +31,26 @@ describe('Admin Controller', function() { }); describe('can not upload invalid file', function() { - it('should return 403 for invalid file type', function() { + it('should return 415 for invalid file type', function() { res.send = sinon.stub(); req.files.uploadimage.name = 'INVALID.FILE'; req.files.uploadimage.type = 'application/octet-stream' admin.uploader(req, res); res.send.calledOnce.should.be.true; - res.send.args[0][0].should.equal(403); - res.send.args[0][1].should.equal('Invalid file type'); + res.send.args[0][0].should.equal(415); + res.send.args[0][1].should.equal('Unsupported Media Type'); }); }); describe('can not upload file with valid extension but invalid type', function() { - it('should return 403 for invalid file type', function() { + it('should return 415 for invalid file type', function() { res.send = sinon.stub(); req.files.uploadimage.name = 'INVALID.jpg'; req.files.uploadimage.type = 'application/octet-stream' admin.uploader(req, res); res.send.calledOnce.should.be.true; - res.send.args[0][0].should.equal(403); - res.send.args[0][1].should.equal('Invalid file type'); + res.send.args[0][0].should.equal(415); + res.send.args[0][1].should.equal('Unsupported Media Type'); }); });