3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
7
+ declare (strict_types=1 );
8
+
6
9
namespace Magento \Customer \Model \Metadata \Form ;
7
10
8
11
use Magento \Customer \Api \AddressMetadataInterface ;
9
12
use Magento \Customer \Api \CustomerMetadataInterface ;
13
+ use Magento \Customer \Api \Data \AttributeMetadataInterface ;
10
14
use Magento \Customer \Model \FileProcessor ;
15
+ use Magento \Customer \Model \FileProcessorFactory ;
11
16
use Magento \Framework \Api \ArrayObjectSearch ;
12
17
use Magento \Framework \Api \Data \ImageContentInterface ;
13
18
use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
14
19
use Magento \Framework \App \ObjectManager ;
20
+ use Magento \Framework \Exception \FileSystemException ;
21
+ use Magento \Framework \Exception \LocalizedException ;
15
22
use Magento \Framework \File \UploaderFactory ;
16
23
use Magento \Framework \Filesystem ;
24
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
25
+ use Magento \Framework \Filesystem \Io \File as IoFileSystem ;
26
+ use Magento \Framework \App \Filesystem \DirectoryList ;
27
+ use Magento \Framework \Filesystem \Directory \WriteFactory ;
28
+ use Magento \Framework \Locale \ResolverInterface ;
29
+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
30
+ use Magento \Framework \Url \EncoderInterface ;
31
+ use Magento \MediaStorage \Model \File \Validator \NotProtectedExtension ;
32
+ use Psr \Log \LoggerInterface ;
17
33
18
34
/**
19
35
* Metadata for form image field
@@ -27,38 +43,55 @@ class Image extends File
27
43
*/
28
44
private $ imageContentFactory ;
29
45
46
+ /**
47
+ * @var IoFileSystem
48
+ */
49
+ private $ ioFileSystem ;
50
+
51
+ /**
52
+ * @var WriteInterface
53
+ */
54
+ private $ mediaEntityTmpDirectory ;
55
+
30
56
/**
31
57
* Constructor
32
58
*
33
- * @param \Magento\Framework\Stdlib\DateTime\ TimezoneInterface $localeDate
34
- * @param \Psr\Log\ LoggerInterface $logger
35
- * @param \Magento\Customer\Api\Data\ AttributeMetadataInterface $attribute
36
- * @param \Magento\Framework\Locale\ ResolverInterface $localeResolver
59
+ * @param TimezoneInterface $localeDate
60
+ * @param LoggerInterface $logger
61
+ * @param AttributeMetadataInterface $attribute
62
+ * @param ResolverInterface $localeResolver
37
63
* @param null|string $value
38
64
* @param string $entityTypeCode
39
65
* @param bool $isAjax
40
- * @param \Magento\Framework\Url\ EncoderInterface $urlEncoder
41
- * @param \Magento\MediaStorage\Model\File\Validator\ NotProtectedExtension $fileValidator
66
+ * @param EncoderInterface $urlEncoder
67
+ * @param NotProtectedExtension $fileValidator
42
68
* @param Filesystem $fileSystem
43
69
* @param UploaderFactory $uploaderFactory
44
- * @param \Magento\Customer\Model\FileProcessorFactory|null $fileProcessorFactory
45
- * @param \Magento\Framework\Api\Data\ImageContentInterfaceFactory|null $imageContentInterfaceFactory
70
+ * @param FileProcessorFactory|null $fileProcessorFactory
71
+ * @param ImageContentInterfaceFactory|null $imageContentInterfaceFactory
72
+ * @param IoFileSystem|null $ioFileSystem
73
+ * @param DirectoryList|null $directoryList
74
+ * @param WriteFactory|null $writeFactory
46
75
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
76
+ * @throws FileSystemException
47
77
*/
48
78
public function __construct (
49
- \ Magento \ Framework \ Stdlib \ DateTime \ TimezoneInterface $ localeDate ,
50
- \ Psr \ Log \ LoggerInterface $ logger ,
51
- \ Magento \ Customer \ Api \ Data \ AttributeMetadataInterface $ attribute ,
52
- \ Magento \ Framework \ Locale \ ResolverInterface $ localeResolver ,
79
+ TimezoneInterface $ localeDate ,
80
+ LoggerInterface $ logger ,
81
+ AttributeMetadataInterface $ attribute ,
82
+ ResolverInterface $ localeResolver ,
53
83
$ value ,
54
84
$ entityTypeCode ,
55
85
$ isAjax ,
56
- \ Magento \ Framework \ Url \ EncoderInterface $ urlEncoder ,
57
- \ Magento \ MediaStorage \ Model \ File \ Validator \ NotProtectedExtension $ fileValidator ,
86
+ EncoderInterface $ urlEncoder ,
87
+ NotProtectedExtension $ fileValidator ,
58
88
Filesystem $ fileSystem ,
59
89
UploaderFactory $ uploaderFactory ,
60
- \Magento \Customer \Model \FileProcessorFactory $ fileProcessorFactory = null ,
61
- \Magento \Framework \Api \Data \ImageContentInterfaceFactory $ imageContentInterfaceFactory = null
90
+ FileProcessorFactory $ fileProcessorFactory = null ,
91
+ ImageContentInterfaceFactory $ imageContentInterfaceFactory = null ,
92
+ IoFileSystem $ ioFileSystem = null ,
93
+ ?DirectoryList $ directoryList = null ,
94
+ ?WriteFactory $ writeFactory = null
62
95
) {
63
96
parent ::__construct (
64
97
$ localeDate ,
@@ -75,7 +108,16 @@ public function __construct(
75
108
$ fileProcessorFactory
76
109
);
77
110
$ this ->imageContentFactory = $ imageContentInterfaceFactory ?: ObjectManager::getInstance ()
78
- ->get (\Magento \Framework \Api \Data \ImageContentInterfaceFactory::class);
111
+ ->get (ImageContentInterfaceFactory::class);
112
+ $ this ->ioFileSystem = $ ioFileSystem ?: ObjectManager::getInstance ()
113
+ ->get (IoFileSystem::class);
114
+ $ writeFactory = $ writeFactory ?? ObjectManager::getInstance ()->get (WriteFactory::class);
115
+ $ directoryList = $ directoryList ?? ObjectManager::getInstance ()->get (DirectoryList::class);
116
+ $ this ->mediaEntityTmpDirectory = $ writeFactory ->create (
117
+ $ directoryList ->getPath ($ directoryList ::MEDIA )
118
+ . '/ ' . $ this ->_entityTypeCode
119
+ . '/ ' . FileProcessor::TMP_DIR
120
+ );
79
121
}
80
122
81
123
/**
@@ -85,6 +127,7 @@ public function __construct(
85
127
*
86
128
* @param array $value
87
129
* @return string[]
130
+ * @throws LocalizedException
88
131
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
89
132
* @SuppressWarnings(PHPMD.NPathComplexity)
90
133
*/
@@ -93,7 +136,11 @@ protected function _validateByRules($value)
93
136
$ label = $ value ['name ' ];
94
137
$ rules = $ this ->getAttribute ()->getValidationRules ();
95
138
96
- $ imageProp = @getimagesize ($ value ['tmp_name ' ]);
139
+ try {
140
+ $ imageProp = getimagesize ($ value ['tmp_name ' ]);
141
+ } catch (\Throwable $ e ) {
142
+ $ imageProp = false ;
143
+ }
97
144
98
145
if (!$ this ->_isUploadedFile ($ value ['tmp_name ' ]) || !$ imageProp ) {
99
146
return [__ ('"%1" is not a valid file. ' , $ label )];
@@ -106,9 +153,11 @@ protected function _validateByRules($value)
106
153
}
107
154
108
155
// modify image name
109
- $ extension = pathinfo ($ value ['name ' ], PATHINFO_EXTENSION ) ;
156
+ $ extension = $ this -> ioFileSystem -> getPathInfo ($ value ['name ' ])[ ' extension ' ] ;
110
157
if ($ extension != $ allowImageTypes [$ imageProp [2 ]]) {
111
- $ value ['name ' ] = pathinfo ($ value ['name ' ], PATHINFO_FILENAME ) . '. ' . $ allowImageTypes [$ imageProp [2 ]];
158
+ $ value ['name ' ] = $ this ->ioFileSystem ->getPathInfo ($ value ['name ' ])['filename ' ]
159
+ . '. '
160
+ . $ allowImageTypes [$ imageProp [2 ]];
112
161
}
113
162
114
163
$ maxFileSize = ArrayObjectSearch::getArrayElementByName (
@@ -153,6 +202,7 @@ protected function _validateByRules($value)
153
202
*
154
203
* @param array $value
155
204
* @return bool|int|ImageContentInterface|string
205
+ * @throws LocalizedException
156
206
*/
157
207
protected function processUiComponentValue (array $ value )
158
208
{
@@ -174,32 +224,43 @@ protected function processUiComponentValue(array $value)
174
224
*
175
225
* @param array $value
176
226
* @return string
227
+ * @throws LocalizedException
177
228
*/
178
229
protected function processCustomerAddressValue (array $ value )
179
230
{
180
- $ result = $ this ->getFileProcessor ()->moveTemporaryFile ($ value ['file ' ]);
181
- return $ result ;
231
+ $ fileName = $ this ->mediaEntityTmpDirectory
232
+ ->getDriver ()
233
+ ->getRealPathSafety (
234
+ $ this ->mediaEntityTmpDirectory ->getAbsolutePath (
235
+ ltrim (
236
+ $ value ['file ' ],
237
+ '/ '
238
+ )
239
+ )
240
+ );
241
+ return $ this ->getFileProcessor ()->moveTemporaryFile (
242
+ $ this ->mediaEntityTmpDirectory ->getRelativePath ($ fileName )
243
+ );
182
244
}
183
245
184
246
/**
185
247
* Process file uploader UI component data for customer entity
186
248
*
187
249
* @param array $value
188
250
* @return bool|int|ImageContentInterface|string
251
+ * @throws LocalizedException
189
252
*/
190
253
protected function processCustomerValue (array $ value )
191
254
{
192
- $ temporaryFile = FileProcessor:: TMP_DIR . ' / ' . ltrim ($ value ['file ' ], '/ ' );
193
-
194
- if ( $ this -> getFileProcessor ()-> isExist ( $ temporaryFile)) {
255
+ $ file = ltrim ($ value ['file ' ], '/ ' );
256
+ if ( $ this -> mediaEntityTmpDirectory -> isExist ( $ file )) {
257
+ $ temporaryFile = FileProcessor:: TMP_DIR . ' / ' . $ file ;
195
258
$ base64EncodedData = $ this ->getFileProcessor ()->getBase64EncodedData ($ temporaryFile );
196
-
197
259
/** @var ImageContentInterface $imageContentDataObject */
198
260
$ imageContentDataObject = $ this ->imageContentFactory ->create ()
199
261
->setName ($ value ['name ' ])
200
262
->setBase64EncodedData ($ base64EncodedData )
201
263
->setType ($ value ['type ' ]);
202
-
203
264
// Remove temporary file
204
265
$ this ->getFileProcessor ()->removeUploadedFile ($ temporaryFile );
205
266
0 commit comments