Skip to content

[TASK] Streamline MimeTypeValidator option handling in FileUpload #1149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
TYPO3IncTeam opened this issue Nov 4, 2024 · 1 comment
Open
Labels
13.3 help wanted Extra attention is needed

Comments

@TYPO3IncTeam
Copy link
Collaborator

ℹ️ View this commit on Github
👥 Authored by Torben Hansen [email protected]
✔️ Merged by Garvin Hicking [email protected]

Commit message

[TASK] Streamline MimeTypeValidator option handling in FileUpload

With #103511, the FileUpload attribute has been introduced to allow
file upload in extbase. For the FileSize and ImageDimensions
validators, it is possible to provide all possible validator options
via configuration. For the MimeTypeValidator, this is however not
possible and the validator is always initialized with the
allowedMimeTypes configuration only.

With this change, the FileUpload attribute has been extended to
support the new mimeType configuration, which allows to define
all possible options of the MimeTypeValidator (allowedMimeTypes,
ignoreFileExtensionCheck, notAllowedMessage,
invalidExtensionMessage).

The allowedMimeTypes configuration in the FileUpload attribute
is kept to ensure backwards compatibility for users already using
this configuration.

Resolves: #105532
Related: #103511
Releases: main, 13.4
Change-Id: I146d091fb024097812aaa608b2b429d39f058b1f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/86873
Reviewed-by: Jörg Hofrichter [email protected]
Tested-by: Jörg Hofrichter [email protected]
Reviewed-by: Benni Mack [email protected]
Tested-by: Garvin Hicking [email protected]
Reviewed-by: Garvin Hicking [email protected]
Tested-by: core-ci [email protected]

➗ Modified files

13.3/Feature-103511-IntroduceExtbaseFileUploadHandling.rst
@@ -70,7 +70,7 @@ Example:
             'required' => true,
             'maxFiles' => 1,
             'fileSize' => ['minimum' => '0K', 'maximum' => '2M'],
-            'allowedMimeTypes' => ['image/jpeg', 'image/png'],
+            'mimeType' => ['allowedMimeTypes' => ['image/jpeg', 'image/png']],
         ],
         'uploadFolder' => '1:/user_upload/files/',
     ])]
@@ -87,7 +87,7 @@ can achieve with a manual configuration as shown below.
 The currently available configuration array keys are:
 
 *   `validation` (:php:`array` with keys `required`, `maxFiles`, `minFiles`,
-    `fileSize`, `allowedMimeTypes`, `imageDimensions`, see
+    `fileSize`, `allowedMimeTypes`, `mimeType`, `imageDimensions`, see
     :ref:`83749-validationkeys`)
 *   `uploadFolder` (:php:`string`, destination folder)
 *   `duplicationBehavior` (:php:`object`, behaviour when file exists)
@@ -294,9 +294,10 @@ section of the :php-short:`\TYPO3\CMS\Extbase\Annotation\FileUpload` attribute:
 *   :php:`required`
 *   :php:`minFiles`
 *   :php:`maxFiles`
-*   :php:`fileSize`
-*   :php:`allowedMimeTypes`
-*   :php:`imageDimensions`
+*   :php:`fileSize`  (for :php:`TYPO3\CMS\Extbase\Validation\Validator\FilesizeValidator`)
+*   :php:`imageDimensions` (for :php:`TYPO3\CMS\Extbase\Validation\Validator\ImageDimensionsValidator`)
+*   :php:`mimeType` (for :php:`TYPO3\CMS\Extbase\Validation\Validator\MimeTypeValidator`)
+*   :php:`allowedMimeTypes` (shorthand notation for configuration option :php:`allowedMimeTypes` of the :php:`MimeTypeValidator`)
 
 Example:
 
@@ -307,14 +308,14 @@ Example:
             'required' => true,
             'maxFiles' => 1,
             'fileSize' => ['minimum' => '0K', 'maximum' => '2M'],
-            'allowedMimeTypes' => ['image/jpeg'],
+            'mimeType' => ['allowedMimeTypes' => ['image/jpeg']],
             'imageDimensions' => ['maxWidth' => 4096, 'maxHeight' => 4096]
         ],
         'uploadFolder' => '1:/user_upload/extbase_single_file/',
     ])]
 
 Extbase will internally use the Extbase file upload validators for
-:php:`fileSize`, :php:`allowedMimeTypes` and :php:`imageDimensions` validation.
+:php:`fileSize`, :php:`mimeType` and :php:`imageDimensions` validation.
 
 Custom validators can be created according to project requirements and must
 extend the Extbase :php-short:`\TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator`.
@@ -324,6 +325,31 @@ Custom validators can however not
 be used in the :php-short:`\TYPO3\CMS\Extbase\Annotation\FileUpload` attribute
 and must be configured manually.
 
+Shorthand notation for `allowedMimeTypes`
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Using the :php:`mimeType` configuration array, all options of the `MimeTypeValidator`
+can be set as sub-keys (since TYPO3 13.4.1):
+
+..  code-block:: php
+
+    #[FileUpload([
+        'validation' => [
+            'required' => true,
+            'mimeType' => [
+                'allowedMimeTypes' => ['image/jpeg'],
+                'ignoreFileExtensionCheck' => false,
+                'notAllowedMessage' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:validation.mimetype.notAllowedMessage',
+                'invalidExtensionMessage' => 'LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:validation.mimetype.invalidExtensionMessage',
+            ],
+        ],
+        'uploadFolder' => '1:/user_upload/files/',
+    ])]
+
+The shorthand notation via :php:`'allowedMimeTypes'` continues to
+exist, in case only the mime type validation is needed. However, it is recommended
+to utilize the full :php:`'mimeType'` configuration array.
+
 
 Deletion of uploaded files and file references
 ----------------------------------------------
@linawolf
Copy link
Member

@garvinhicking do oyu want to document this as you have been working on this?

linawolf added a commit to TYPO3-Documentation/TYPO3CMS-Reference-CoreApi that referenced this issue Mar 29, 2025
@linawolf linawolf self-assigned this Mar 29, 2025
linawolf added a commit to TYPO3-Documentation/TYPO3CMS-Reference-CoreApi that referenced this issue Mar 29, 2025
@linawolf linawolf removed their assignment Apr 27, 2025
@linawolf linawolf added the help wanted Extra attention is needed label Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
13.3 help wanted Extra attention is needed
Projects
None yet
2 participants