From 8bc056ebe6d5876c6de2a2ca84bf234678c3e702 Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Sun, 25 Aug 2019 21:24:22 +0200 Subject: [ticket/16076] addFileFilter to check max file size per mime type PHPBB3-16076 --- phpBB/assets/javascript/plupload.js | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'phpBB/assets') diff --git a/phpBB/assets/javascript/plupload.js b/phpBB/assets/javascript/plupload.js index fab1ca6d7c..ab6d0d7e88 100644 --- a/phpBB/assets/javascript/plupload.js +++ b/phpBB/assets/javascript/plupload.js @@ -446,6 +446,44 @@ phpbb.plupload.fileError = function(file, error) { phpbb.plupload.uploader = new plupload.Uploader(phpbb.plupload.config); phpbb.plupload.initialize(); +/** + * Add a file filter to check for max file sizes per mime type. + */ +plupload.addFileFilter('mime_types_max_file_size', function(types, file, cb) { + if (file.size !== 'undefined') { + $(types).each(function(i, type) { + let extensions = [], + exts_array = type.extensions.split(','); + + $(exts_array).each(function(i, extension) { + /^\s*\*\s*$/.test(extension) ? extensions.push("\\.*") : extensions.push("\\." + extension.replace(new RegExp("[" + "/^$.*+?|()[]{}\\".replace(/./g, "\\$&") + "]", "g"), "\\$&")); + }); + + let regex = new RegExp("(" + extensions.join("|") + ")$", "i"); + + if (regex.test(file.name)) { + if (type.max_file_size !== 'undefined' && type.max_file_size) { + if (file.size > type.max_file_size) { + phpbb.plupload.uploader.trigger('Error', { + code: plupload.FILE_SIZE_ERROR, + message: plupload.translate('File size error.'), + file: file + }); + + cb(false); + } else { + cb(true); + } + } else { + cb(true); + } + + return false; + } + }); + } +}); + var $fileList = $('#file-list'); /** -- cgit v1.2.1