From cc7d595fffdb719504b91c2c6b7da629aaad2788 Mon Sep 17 00:00:00 2001 From: webmaxx Date: Thu, 18 Jul 2019 17:48:31 +0300 Subject: [PATCH] Added `maxFilesize` option to the `FileUpload` FormWidget (#4077) Credit to @webmaxx --- .github/FUNDING.yml | 4 +++ modules/backend/formwidgets/FileUpload.php | 29 +++++++++++++++++++ .../fileupload/assets/js/fileupload.js | 2 ++ .../fileupload/partials/_file_multi.htm | 1 + .../fileupload/partials/_file_single.htm | 1 + .../fileupload/partials/_image_multi.htm | 1 + .../fileupload/partials/_image_single.htm | 1 + 7 files changed, 39 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000000..7feae6a605 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms +custom: ['https://octobercms.com/fundraising'] +open_collective: octobercms +patreon: LukeTowers diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index 7deae0d993..0a8f5d1ba9 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -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 @@ 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(); @@ -144,6 +156,7 @@ protected function prepareVars() $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 @@ 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); + } } diff --git a/modules/backend/formwidgets/fileupload/assets/js/fileupload.js b/modules/backend/formwidgets/fileupload/assets/js/fileupload.js index 20c5730353..bd4e50b43b 100644 --- a/modules/backend/formwidgets/fileupload/assets/js/fileupload.js +++ b/modules/backend/formwidgets/fileupload/assets/js/fileupload.js @@ -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, diff --git a/modules/backend/formwidgets/fileupload/partials/_file_multi.htm b/modules/backend/formwidgets/fileupload/partials/_file_multi.htm index 0dceb6e354..e478a7c682 100644 --- a/modules/backend/formwidgets/fileupload/partials/_file_multi.htm +++ b/modules/backend/formwidgets/fileupload/partials/_file_multi.htm @@ -7,6 +7,7 @@ data-error-template="#getId('errorTemplate') ?>" data-sort-handler="getEventHandler('onSortAttachments') ?>" data-unique-id="getId() ?>" + data-max-filesize="" data-config-handler="getEventHandler('onLoadAttachmentConfig') ?>" data-file-types="" > diff --git a/modules/backend/formwidgets/fileupload/partials/_file_single.htm b/modules/backend/formwidgets/fileupload/partials/_file_single.htm index f58ed010b8..fbf1530519 100644 --- a/modules/backend/formwidgets/fileupload/partials/_file_single.htm +++ b/modules/backend/formwidgets/fileupload/partials/_file_single.htm @@ -6,6 +6,7 @@ data-template="#getId('template') ?>" data-error-template="#getId('errorTemplate') ?>" data-unique-id="getId() ?>" + data-max-filesize="" data-config-handler="getEventHandler('onLoadAttachmentConfig') ?>" data-file-types="" > diff --git a/modules/backend/formwidgets/fileupload/partials/_image_multi.htm b/modules/backend/formwidgets/fileupload/partials/_image_multi.htm index a24b3fbfcc..1f43e6cf4f 100644 --- a/modules/backend/formwidgets/fileupload/partials/_image_multi.htm +++ b/modules/backend/formwidgets/fileupload/partials/_image_multi.htm @@ -7,6 +7,7 @@ data-error-template="#getId('errorTemplate') ?>" data-sort-handler="getEventHandler('onSortAttachments') ?>" data-unique-id="getId() ?>" + data-max-filesize="" data-config-handler="getEventHandler('onLoadAttachmentConfig') ?>" data-file-types="" > diff --git a/modules/backend/formwidgets/fileupload/partials/_image_single.htm b/modules/backend/formwidgets/fileupload/partials/_image_single.htm index 8d89e14d78..069ff1a32a 100644 --- a/modules/backend/formwidgets/fileupload/partials/_image_single.htm +++ b/modules/backend/formwidgets/fileupload/partials/_image_single.htm @@ -8,6 +8,7 @@ data-unique-id="getId() ?>" data-thumbnail-width="" data-thumbnail-height="" + data-max-filesize="" data-config-handler="getEventHandler('onLoadAttachmentConfig') ?>" data-file-types="" >