From: Joshua Rüsweg Date: Fri, 27 Sep 2019 17:13:44 +0000 (+0200) Subject: Add maximum filesize validator for UploadFormFields X-Git-Tag: 5.2.0_Beta_2~34 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=214c3f227902513bef06632e84e1018505d0041e;p=GitHub%2FWoltLab%2FWCF.git Add maximum filesize validator for UploadFormFields --- diff --git a/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php index e1aa5d987a..b2becacbdc 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php @@ -42,6 +42,12 @@ class UploadFormField extends AbstractFormField { */ protected $allowSvgImage = false; + /** + * maximum filesize for each uploaded file + * @var null|number + */ + protected $maximumFilesize; + /** * @inheritDoc */ @@ -153,6 +159,22 @@ class UploadFormField extends AbstractFormField { ['maximum' => $this->getMaximum()] )); } + + if ($this->getMaximumFilesize() !== null) { + foreach ($this->getValue() as $file) { + $filesize = filesize($file->getLocation()); + if ($filesize > $this->getMaximumFilesize()) { + $this->addValidationError(new FormFieldValidationError( + 'maximumFilesize', + 'wcf.form.field.upload.error.maximumFilesize', + [ + 'maximumFilesize' => $this->getMaximumFilesize(), + 'file' => $file + ] + )); + } + } + } } /** @@ -282,6 +304,37 @@ class UploadFormField extends AbstractFormField { return $this->traitMaximum($maximum); } + /** + * Sets the maximum filesize for each upload. If `null` is passed, the + * maximum filesize is removed. + * + * @param null|number $maximumFilesize maximum filesize + * @return static this field + * + * @throws \InvalidArgumentException if the given maximum filesize is no number or otherwise invalid + */ + public function maximumFilesize($maximumFilesize = null) { + if ($maximumFilesize !== null) { + if (!is_numeric($maximumFilesize)) { + throw new \InvalidArgumentException("Given maximum filesize is no int, '" . gettype($maximumFilesize) . "' given."); + } + } + + $this->maximumFilesize = $maximumFilesize; + + return $this; + } + + /** + * Returns the maximum filesize of each file or `null` if no maximum filesize + * has been set. + * + * @return null|number + */ + public function getMaximumFilesize() { + return $this->maximumFilesize; + } + /** * Sets the flag for `imageOnly`. This flag indicates whether only images * can uploaded via this field. Other file types will be rejected during upload. diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 57eb66e78d..b3e7006670 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3927,6 +3927,7 @@ Dateianhänge: + getFilename()}“ darf maximal {$maximumFilesize|filesize} groß sein.]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index e67282bdd2..8081c1c6ea 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3873,6 +3873,7 @@ Attachments: + getFilename()}” may have a maximum size of {$maximumFilesize|filesize}.]]>