From ff09789812fabaf8c306be2e345951dc6356123a Mon Sep 17 00:00:00 2001 From: Raphael Beer Date: Tue, 29 Mar 2016 09:59:47 +0200 Subject: [PATCH] Check file size locally. --- public/src/modules/uploader.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/public/src/modules/uploader.js b/public/src/modules/uploader.js index 575508b2b9..65e1a481e3 100644 --- a/public/src/modules/uploader.js +++ b/public/src/modules/uploader.js @@ -16,9 +16,10 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) { }; module.show = function(data, callback) { + var fileSize = data.hasOwnProperty('fileSize') && data.fileSize !== undefined ? parseInt(data.fileSize, 10) : false; parseModal({ showHelp: data.hasOwnProperty('showHelp') && data.showHelp !== undefined ? data.showHelp : true, - fileSize: data.hasOwnProperty('fileSize') && data.fileSize !== undefined ? parseInt(data.fileSize, 10) : false, + fileSize: fileSize, title: data.title || '[[global:upload_file]]', description: data.description || '', button: data.button || '[[global:upload]]', @@ -40,13 +41,17 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) { }); uploadForm.submit(function() { - onSubmit(uploadModal, callback); + onSubmit(uploadModal, fileSize, callback); return false; }); }); }; - function onSubmit(uploadModal, callback) { + module.hideAlerts = function(modal) { + $(modal).find('#alert-status, #alert-success, #alert-error, #upload-progress-box').addClass('hide'); + }; + + function onSubmit(uploadModal, fileSize, callback) { function showAlert(type, message) { module.hideAlerts(uploadModal); uploadModal.find('#alert-' + type).translateText(message).removeClass('hide'); @@ -57,9 +62,13 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) { uploadModal.find('#upload-progress-bar').css('width', '0%'); uploadModal.find('#upload-progress-box').show().removeClass('hide'); - if (!uploadModal.find('#fileInput').val()) { + var fileInput = uploadModal.find('#fileInput'); + if (!fileInput.val()) { return showAlert('error', '[[uploads:select-file-to-upload]]'); } + if (hasValidFileSize(fileInput[0], fileSize) === false) { + return showAlert('error', '[[error:file-too-big, ' + fileSize + ']]'); + } uploadModal.find('#uploadForm').ajaxSubmit({ headers: { @@ -107,9 +116,11 @@ define('uploader', ['csrf', 'translator'], function(csrf, translator) { return response; } - module.hideAlerts = function(modal) { - $(modal).find('#alert-status, #alert-success, #alert-error, #upload-progress-box').addClass('hide'); - }; + function hasValidFileSize(fileElement, maxSize) { + if (window.FileReader && maxSize) { + return fileElement.files[0].size <= maxSize * 1000; + } + } return module; });