Add maximum filesize validator for UploadFormFields
authorJoshua Rüsweg <ruesweg@woltlab.com>
Fri, 27 Sep 2019 17:13:44 +0000 (19:13 +0200)
committerJoshua Rüsweg <ruesweg@woltlab.com>
Fri, 27 Sep 2019 17:13:50 +0000 (19:13 +0200)
wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index e1aa5d987a75902960410b1ef1fe4526411b394d..b2becacbdc30375ab657bffc664071fd0a444186 100644 (file)
@@ -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.
index 57eb66e78dc06fea6ed0259879433fa377538e03..b3e70066700c37278bfc9cc2e3e95e4a86aa1a50 100644 (file)
@@ -3927,6 +3927,7 @@ Dateianhänge:
                <item name="wcf.form.field.email"><![CDATA[E-Mail-Adresse]]></item>
                <item name="wcf.form.field.email.error.invalidEmail"><![CDATA[Die Eingabe ist keine gültige E-Mail-Adresse.]]></item>
                <item name="wcf.form.field.email.error.maximumLength"><![CDATA[Die angegebene E-Mail-Adresse{if $language} für die Sprache „{$language}“{/if} umfasst {#$length} Zeichen, sie darf aber nicht länger als {#$maximumLength} Zeichen sein.]]></item>
+               <item name="wcf.form.field.upload.error.maximumFilesize"><![CDATA[Die Datei „{$file->getFilename()}“ darf maximal {$maximumFilesize|filesize} groß sein.]]></item>
        </category>
        <category name="wcf.image">
                <item name="wcf.image.coverPhoto"><![CDATA[Titelbild]]></item>
index e67282bdd2e88771588037bda591550cf299f9b0..8081c1c6eae0994031aad68ef97a1394f67adb54 100644 (file)
@@ -3873,6 +3873,7 @@ Attachments:
                <item name="wcf.form.field.email"><![CDATA[Email Address]]></item>
                <item name="wcf.form.field.email.error.invalidEmail"><![CDATA[The entered text is no valid email address.]]></item>
                <item name="wcf.form.field.email.error.maximumLength"><![CDATA[The entered email address{if $language|isset} for language “{$language}”{/if} contains {#$length} character{if $length !== 1}s{/if} but it may not be longer than {#$maximumLength} character{if $maximumLength !== 1}s{/if}.]]></item>
+               <item name="wcf.form.field.upload.error.maximumFilesize"><![CDATA[The file “{$file->getFilename()}” may have a maximum size of {$maximumFilesize|filesize}.]]></item>
        </category>
        <category name="wcf.image">
                <item name="wcf.image.coverPhoto"><![CDATA[Cover Photo]]></item>