Merge pull request #5664 from mrgoldy/ticket/16076

[ticket/16076] addFileFilter to check max file size per mime type
This commit is contained in:
Marc Alexander 2019-09-22 13:02:19 +02:00
commit a4edb41e3f
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
3 changed files with 63 additions and 20 deletions

View file

@ -463,6 +463,44 @@ phpbb.plupload.fileError = function(file, error) {
phpbb.plupload.uploader = new plupload.Uploader(phpbb.plupload.config); phpbb.plupload.uploader = new plupload.Uploader(phpbb.plupload.config);
phpbb.plupload.initialize(); 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, callback) {
if (file.size !== 'undefined') {
$(types).each(function(i, type) {
let extensions = [],
extsArray = type.extensions.split(',');
$(extsArray).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
});
callback(false);
} else {
callback(true);
}
} else {
callback(true);
}
return false;
}
});
}
});
var $fileList = $('#file-list'); var $fileList = $('#file-list');
/** /**

View file

@ -216,38 +216,36 @@ class plupload
} }
/** /**
* Looks at the list of allowed extensions and generates a string * Looks at the list of allowed extensions and generates a string
* appropriate for use in configuring plupload with * appropriate for use in configuring plupload with
* *
* @param \phpbb\cache\service $cache * @param \phpbb\cache\service $cache Cache service object
* @param string $forum_id The ID of the forum * @param string $forum_id The forum identifier
* *
* @return string * @return string
*/ */
public function generate_filter_string(\phpbb\cache\service $cache, $forum_id) public function generate_filter_string(\phpbb\cache\service $cache, $forum_id)
{ {
$groups = [];
$filters = [];
$attach_extensions = $cache->obtain_attach_extensions($forum_id); $attach_extensions = $cache->obtain_attach_extensions($forum_id);
unset($attach_extensions['_allowed_']); unset($attach_extensions['_allowed_']);
$groups = array();
// Re-arrange the extension array to $groups[$group_name][] // Re-arrange the extension array to $groups[$group_name][]
foreach ($attach_extensions as $extension => $extension_info) foreach ($attach_extensions as $extension => $extension_info)
{ {
if (!isset($groups[$extension_info['group_name']])) $groups[$extension_info['group_name']]['extensions'][] = $extension;
{ $groups[$extension_info['group_name']]['max_file_size'] = (int) $extension_info['max_filesize'];
$groups[$extension_info['group_name']] = array();
}
$groups[$extension_info['group_name']][] = $extension;
} }
$filters = array(); foreach ($groups as $group => $group_info)
foreach ($groups as $group => $extensions)
{ {
$filters[] = sprintf( $filters[] = sprintf(
"{title: '%s', extensions: '%s'}", "{title: '%s', extensions: '%s', max_file_size: %s}",
addslashes(ucfirst(strtolower($group))), addslashes(ucfirst(strtolower($group))),
addslashes(implode(',', $extensions)) addslashes(implode(',', $group_info['extensions'])),
$group_info['max_file_size']
); );
} }

View file

@ -45,7 +45,14 @@ phpbb.plupload = {
max_file_size: '{FILESIZE}b', max_file_size: '{FILESIZE}b',
chunk_size: '{CHUNK_SIZE}b', chunk_size: '{CHUNK_SIZE}b',
unique_names: true, unique_names: true,
filters: [{FILTERS}], filters: {
mime_types: [
{FILTERS}
],
mime_types_max_file_size: [
{FILTERS}
],
},
{S_RESIZE} {S_RESIZE}
headers: {'X-PHPBB-USING-PLUPLOAD': '1', 'X-Requested-With': 'XMLHttpRequest'}, headers: {'X-PHPBB-USING-PLUPLOAD': '1', 'X-Requested-With': 'XMLHttpRequest'},
file_data_name: 'fileupload', file_data_name: 'fileupload',