From fb919fbb53e28940ecd9313428c9fcd6755953ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 30 Jun 2020 11:27:58 +0200 Subject: [PATCH] Add support for acceptable types to UploadFormField of form builder Resolves #3414 --- .../builder/field/UploadFormField.class.php | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) 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 d095b32468..ec8239f4b9 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,13 @@ class UploadFormField extends AbstractFormField { */ protected $allowSvgImage = false; + /** + * Acceptable file types. + * @var null|string[] + * @since 5.3 + */ + protected $acceptableFiles = null; + /** * maximum filesize for each uploaded file * @var null|number @@ -110,6 +117,7 @@ class UploadFormField extends AbstractFormField { if ($this->isImageOnly()) { $uploadField->setAllowSvgImage($this->svgImageAllowed()); } + $uploadField->setAcceptableFiles($this->getAcceptableFiles()); return $uploadField; } @@ -597,7 +605,10 @@ class UploadFormField extends AbstractFormField { /** * Sets the flag for `imageOnly`. This flag indicates whether only images * can uploaded via this field. Other file types will be rejected during upload. - * + * + * If set to `true` will also set the acceptable types to `image/*`. If set to + * false it will clear the acceptable types if they are `image/*`. + * * @param boolean $imageOnly * @return static this field * @@ -623,6 +634,16 @@ class UploadFormField extends AbstractFormField { } $this->imageOnly = $imageOnly; + if ($imageOnly) { + $this->setAcceptableFiles(['image/*']); + } + else { + // Using == here is safe, because we match a single element array containing + // a scalar value. + if ($this->getAcceptableFiles() == ['image/*']) { + $this->setAcceptableFiles(null); + } + } return $this; } @@ -668,4 +689,38 @@ class UploadFormField extends AbstractFormField { public function svgImageAllowed() { return $this->allowSvgImage; } + + /** + * Specifies acceptable file types. Use null to not specify any restrictions. + * + * Heads up: This feature is used to improve user experience, by removing + * unacceptable files from the file picker. It does not validate the type of the uploaded + * file. You are responsible to perform (proper) validation on the server side. + * + * Valid values are specified as "Unique file type specifiers": + * - A case insensitive file extension starting with a dot. + * - A mime type. + * - `audio/*` + * - `image/*` + * - `video/*` + * + * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers + * @param string[]|null $acceptableFiles + * @since 5.3 + */ + public function setAcceptableFiles($acceptableFiles = null) { + $this->acceptableFiles = $acceptableFiles; + + return $this; + } + + /** + * Returns the acceptable file types. + * + * @return string[]|null + * @since 5.3 + */ + public function getAcceptableFiles() { + return $this->acceptableFiles; + } } -- 2.20.1