[TASK] EXT:form - Optimize file upload/ handling of files
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Property / TypeConverter / ArrayToValidationElementConverter.php
1 <?php
2 namespace TYPO3\CMS\Form\Domain\Property\TypeConverter;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter;
19 use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface;
20 use TYPO3\CMS\Form\Domain\Model\ValidationElement;
21
22 /**
23 * The form wizard controller
24 */
25 class ArrayToValidationElementConverter extends AbstractTypeConverter {
26
27 /**
28 * @var array<string>
29 */
30 protected $sourceTypes = array('array');
31
32 /**
33 * @var string
34 */
35 protected $targetType = 'TYPO3\\CMS\\Form\\Domain\\Model\\ValidationElement';
36
37 /**
38 * @var int
39 */
40 protected $priority = 1;
41
42 /**
43 * We can only convert empty strings to array or array to array.
44 *
45 * @param mixed $source
46 * @param string $targetType
47 * @return bool
48 */
49 public function canConvertFrom($source, $targetType) {
50 return is_array($source);
51 }
52
53 /**
54 * Convert the incoming array to a ValidationElement
55 *
56 * @param array $source
57 * @param string $targetType
58 * @param array $convertedChildProperties
59 * @param PropertyMappingConfigurationInterface $configuration
60 * @return ValidationElement
61 * @api
62 */
63 public function convertFrom($source, $targetType, array $convertedChildProperties = array(), PropertyMappingConfigurationInterface $configuration = NULL) {
64 /** @var ValidationElement $validationElement */
65 $validationElement = GeneralUtility::makeInstance(ValidationElement::class);
66 if (is_array($source)) {
67 /**
68 * Find uploaded files.
69 *
70 * Extbase has already mapped the $_FILES data into the request
71 * @see TYPO3\CMS\Extbase\Mvc\Web\Request::build()
72 * If a $_FILES array is found in the request data ($source),
73 * set the file mime type with
74 * \TYPO3\CMS\Core\Type\File\FileInfo
75 * and write the data back into $source.
76 */
77 foreach ($source as $propertyName => $value) {
78 if (is_array($value)) {
79 $uploadedFiles = array();
80 if (
81 isset($value['name'])
82 && isset($value['type'])
83 && isset($value['tmp_name'])
84 && isset($value['size'])
85 ) {
86 // if single file upload - cast to array
87 $uploadedFiles[] = $value;
88 } elseif (
89 isset($value[0]['name'])
90 && isset($value[0]['type'])
91 && isset($value[0]['tmp_name'])
92 && isset($value[0]['size'])
93 ) {
94 // multi file upload
95 $uploadedFiles = $value;
96 }
97
98 if (!empty($uploadedFiles)) {
99 foreach ($uploadedFiles as $key => &$file) {
100 if (
101 $file['name'] === ''
102 && $file['type'] === ''
103 && $file['tmp_name'] === ''
104 && $file['size'] === 0
105 ) {
106 unset($uploadedFiles[$key]);
107 continue;
108 }
109 $fileInfo = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Type\File\FileInfo::class, $file['tmp_name']);
110 $file['type'] = $fileInfo->getMimeType();
111 $file['name'] = htmlspecialchars($file['name']);
112 }
113 $source[$propertyName] = $uploadedFiles;
114 }
115 }
116 }
117 $validationElement->setIncomingFields($source);
118 }
119
120 return $validationElement;
121 }
122 }