mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Added maxFilesize
option to the FileUpload
FormWidget (#4077)
Credit to @webmaxx
This commit is contained in:
parent
ae5f1a4282
commit
cc7d595fff
4
.github/FUNDING.yml
vendored
Normal file
4
.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# These are supported funding model platforms
|
||||
custom: ['https://octobercms.com/fundraising']
|
||||
open_collective: octobercms
|
||||
patreon: LukeTowers
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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 ?>
|
||||
>
|
||||
|
@ -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 ?>
|
||||
>
|
||||
|
@ -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 ?>
|
||||
>
|
||||
|
@ -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 ?>
|
||||
>
|
||||
|
Loading…
x
Reference in New Issue
Block a user