Added maxFilesize option to the FileUpload FormWidget (#4077)

Credit to @webmaxx
This commit is contained in:
webmaxx 2019-07-18 17:48:31 +03:00 committed by Luke Towers
parent ae5f1a4282
commit cc7d595fff
7 changed files with 39 additions and 0 deletions

4
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,4 @@
# These are supported funding model platforms
custom: ['https://octobercms.com/fundraising']
open_collective: octobercms
patreon: LukeTowers

View File

@ -60,6 +60,11 @@ class FileUpload extends FormWidgetBase
*/
public $mimeTypes = false;
/**
* @var mixed Max file size.
*/
public $maxFilesize;
/**
* @var array Options used for generating thumbnails.
*/
@ -97,11 +102,14 @@ class FileUpload extends FormWidgetBase
*/
public function init()
{
$this->maxFilesize = $this->getUploadMaxFilesize();
$this->fillFromConfig([
'prompt',
'imageWidth',
'imageHeight',
'fileTypes',
'maxFilesize',
'mimeTypes',
'thumbOptions',
'useCaption',
@ -137,6 +145,10 @@ class FileUpload extends FormWidgetBase
$this->useCaption = false;
}
if ($this->maxFilesize > $this->getUploadMaxFilesize()) {
throw new ApplicationException('Maximum allowed size for uploaded files: ' . $this->getUploadMaxFilesize());
}
$this->vars['fileList'] = $fileList = $this->getFileList();
$this->vars['singleFile'] = $fileList->first();
$this->vars['displayMode'] = $this->getDisplayMode();
@ -144,6 +156,7 @@ class FileUpload extends FormWidgetBase
$this->vars['imageHeight'] = $this->imageHeight;
$this->vars['imageWidth'] = $this->imageWidth;
$this->vars['acceptedFileTypes'] = $this->getAcceptedFileTypes(true);
$this->vars['maxFilesize'] = $this->maxFilesize;
$this->vars['cssDimensions'] = $this->getCssDimensions();
$this->vars['cssBlockDimensions'] = $this->getCssDimensions('block');
$this->vars['useCaption'] = $this->useCaption;
@ -513,4 +526,20 @@ class FileUpload extends FormWidgetBase
return $file;
}
/**
* Return max upload filesize in Mb
* @return integer
*/
protected function getUploadMaxFilesize()
{
$size = ini_get('upload_max_filesize');
if (preg_match('/^([\d\.]+)([KMG])$/i', $size, $match)) {
$pos = array_search($match[2], ['K', 'M', 'G']);
if ($pos !== false) {
$size = $match[1] * pow(1024, $pos + 1);
}
}
return floor($size / 1024 / 1024);
}
}

View File

@ -103,6 +103,7 @@
clickable: this.$uploadButton.get(0),
previewsContainer: this.$filesContainer.get(0),
maxFiles: !this.options.isMulti ? 1 : null,
maxFilesize: this.options.maxFilesize,
headers: {}
}
@ -432,6 +433,7 @@
extraData: {},
paramName: 'file_data',
fileTypes: null,
maxFilesize: 256,
template: null,
errorTemplate: null,
isMulti: null,

View File

@ -7,6 +7,7 @@
data-error-template="#<?= $this->getId('errorTemplate') ?>"
data-sort-handler="<?= $this->getEventHandler('onSortAttachments') ?>"
data-unique-id="<?= $this->getId() ?>"
data-max-filesize="<?= $maxFilesize ?>"
<?php if ($useCaption): ?>data-config-handler="<?= $this->getEventHandler('onLoadAttachmentConfig') ?>"<?php endif ?>
<?php if ($acceptedFileTypes): ?>data-file-types="<?= $acceptedFileTypes ?>"<?php endif ?>
>

View File

@ -6,6 +6,7 @@
data-template="#<?= $this->getId('template') ?>"
data-error-template="#<?= $this->getId('errorTemplate') ?>"
data-unique-id="<?= $this->getId() ?>"
data-max-filesize="<?= $maxFilesize ?>"
<?php if ($useCaption): ?>data-config-handler="<?= $this->getEventHandler('onLoadAttachmentConfig') ?>"<?php endif ?>
<?php if ($acceptedFileTypes): ?>data-file-types="<?= $acceptedFileTypes ?>"<?php endif ?>
>

View File

@ -7,6 +7,7 @@
data-error-template="#<?= $this->getId('errorTemplate') ?>"
data-sort-handler="<?= $this->getEventHandler('onSortAttachments') ?>"
data-unique-id="<?= $this->getId() ?>"
data-max-filesize="<?= $maxFilesize ?>"
<?php if ($useCaption): ?>data-config-handler="<?= $this->getEventHandler('onLoadAttachmentConfig') ?>"<?php endif ?>
<?php if ($acceptedFileTypes): ?>data-file-types="<?= $acceptedFileTypes ?>"<?php endif ?>
>

View File

@ -8,6 +8,7 @@
data-unique-id="<?= $this->getId() ?>"
data-thumbnail-width="<?= $imageWidth ?: '0' ?>"
data-thumbnail-height="<?= $imageHeight ?: '0' ?>"
data-max-filesize="<?= $maxFilesize ?>"
<?php if ($useCaption): ?>data-config-handler="<?= $this->getEventHandler('onLoadAttachmentConfig') ?>"<?php endif ?>
<?php if ($acceptedFileTypes): ?>data-file-types="<?= $acceptedFileTypes ?>"<?php endif ?>
>