[TASK] EXT:form - Optimize file upload/ handling of files
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Property / TypeConverter / ArrayToValidationElementConverter.php
index 09d8974..f4b55ce 100644 (file)
@@ -63,8 +63,57 @@ class ArrayToValidationElementConverter extends AbstractTypeConverter {
        public function convertFrom($source, $targetType, array $convertedChildProperties = array(), PropertyMappingConfigurationInterface $configuration = NULL) {
                /** @var ValidationElement $validationElement */
                $validationElement = GeneralUtility::makeInstance(ValidationElement::class);
-
                if (is_array($source)) {
+                       /**
+                        * Find uploaded files.
+                        *
+                        * Extbase has already mapped the $_FILES data into the request
+                        * @see TYPO3\CMS\Extbase\Mvc\Web\Request::build()
+                        * If a $_FILES array is found in the request data ($source),
+                        * set the file mime type with
+                        * \TYPO3\CMS\Core\Type\File\FileInfo
+                        * and write the data back into $source.
+                        */
+                       foreach ($source as $propertyName => $value) {
+                               if (is_array($value)) {
+                                       $uploadedFiles = array();
+                                       if (
+                                               isset($value['name'])
+                                               && isset($value['type'])
+                                               && isset($value['tmp_name'])
+                                               && isset($value['size'])
+                                       ) {
+                                               // if single file upload - cast to array
+                                               $uploadedFiles[] = $value;
+                                       } elseif (
+                                               isset($value[0]['name'])
+                                               && isset($value[0]['type'])
+                                               && isset($value[0]['tmp_name'])
+                                               && isset($value[0]['size'])
+                                       ) {
+                                               // multi file upload
+                                               $uploadedFiles = $value;
+                                       }
+
+                                       if (!empty($uploadedFiles)) {
+                                               foreach ($uploadedFiles as $key => &$file) {
+                                                       if (
+                                                               $file['name'] === ''
+                                                               && $file['type'] === ''
+                                                               && $file['tmp_name'] === ''
+                                                               && $file['size'] === 0
+                                                       ) {
+                                                               unset($uploadedFiles[$key]);
+                                                               continue;
+                                                       }
+                                                       $fileInfo = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Type\File\FileInfo::class, $file['tmp_name']);
+                                                       $file['type'] = $fileInfo->getMimeType();
+                                                       $file['name'] = htmlspecialchars($file['name']);
+                                               }
+                                               $source[$propertyName] = $uploadedFiles;
+                                       }
+                               }
+                       }
                        $validationElement->setIncomingFields($source);
                }