Skip to content

Commit

Permalink
Added maxFilesize option to the FileUpload FormWidget (octobercms…
Browse files Browse the repository at this point in the history
…#4077)

Credit to @webmaxx
  • Loading branch information
webmaxx authored and LukeTowers committed Jul 18, 2019
1 parent ae5f1a4 commit cc7d595
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms
custom: ['https://octobercms.com/fundraising']
open_collective: octobercms
patreon: LukeTowers
29 changes: 29 additions & 0 deletions modules/backend/formwidgets/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -137,13 +145,18 @@ protected function prepareVars()
$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();
$this->vars['emptyIcon'] = $this->getConfig('emptyIcon', 'icon-upload');
$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;
Expand Down Expand Up @@ -513,4 +526,20 @@ protected function decorateFileAttributes($file)

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
}

Expand Down Expand Up @@ -432,6 +433,7 @@
extraData: {},
paramName: 'file_data',
fileTypes: null,
maxFilesize: 256,
template: null,
errorTemplate: null,
isMulti: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?>
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?>
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?>
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?>
>
Expand Down

0 comments on commit cc7d595

Please sign in to comment.