11
11
use Magento \Framework \Api \Data \ImageContentInterface ;
12
12
use Magento \Framework \App \Filesystem \DirectoryList ;
13
13
use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \Exception \LocalizedException ;
14
15
use Magento \Framework \File \UploaderFactory ;
15
16
use Magento \Framework \Filesystem ;
16
17
@@ -111,6 +112,7 @@ public function extractValue(\Magento\Framework\App\RequestInterface $request)
111
112
$ extend = $ this ->_getRequestValue ($ request );
112
113
113
114
$ attrCode = $ this ->getAttribute ()->getAttributeCode ();
115
+ // phpcs:ignore Magento2.Security.Superglobal
114
116
if ($ this ->_requestScope || !isset ($ _FILES [$ attrCode ])) {
115
117
$ value = [];
116
118
if (strpos ($ this ->_requestScope , '/ ' ) !== false ) {
@@ -120,9 +122,10 @@ public function extractValue(\Magento\Framework\App\RequestInterface $request)
120
122
$ mainScope = $ this ->_requestScope ;
121
123
$ scopes = [];
122
124
}
123
-
125
+ // phpcs:disable Magento2.Security.Superglobal
124
126
if (!empty ($ _FILES [$ mainScope ])) {
125
127
foreach ($ _FILES [$ mainScope ] as $ fileKey => $ scopeData ) {
128
+ // phpcs:enable Magento2.Security.Superglobal
126
129
foreach ($ scopes as $ scopeName ) {
127
130
if (isset ($ scopeData [$ scopeName ])) {
128
131
$ scopeData = $ scopeData [$ scopeName ];
@@ -147,8 +150,10 @@ public function extractValue(\Magento\Framework\App\RequestInterface $request)
147
150
$ value = [];
148
151
}
149
152
} else {
153
+ // phpcs:disable Magento2.Security.Superglobal
150
154
if (isset ($ _FILES [$ attrCode ])) {
151
155
$ value = $ _FILES [$ attrCode ];
156
+ // phpcs:enable Magento2.Security.Superglobal
152
157
} else {
153
158
$ value = [];
154
159
}
@@ -171,7 +176,7 @@ protected function _validateByRules($value)
171
176
{
172
177
$ label = $ value ['name ' ];
173
178
$ rules = $ this ->getAttribute ()->getValidationRules ();
174
- $ extension = pathinfo ($ value ['name ' ], PATHINFO_EXTENSION ) ;
179
+ $ extension = $ this -> fileProcessor -> getStat ($ value ['name ' ])[ ' extension ' ] ;
175
180
$ fileExtensions = ArrayObjectSearch::getArrayElementByName (
176
181
$ rules ,
177
182
'file_extensions '
@@ -219,12 +224,13 @@ protected function _validateByRules($value)
219
224
*/
220
225
protected function _isUploadedFile ($ filename )
221
226
{
227
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
222
228
if (is_uploaded_file ($ filename )) {
223
229
return true ;
224
230
}
225
231
226
232
// This case is required for file uploader UI component
227
- $ temporaryFile = FileProcessor::TMP_DIR . '/ ' . pathinfo ($ filename )['basename ' ];
233
+ $ temporaryFile = FileProcessor::TMP_DIR . '/ ' . $ this -> fileProcessor -> getStat ($ filename )['basename ' ];
228
234
if ($ this ->fileProcessor ->isExist ($ temporaryFile )) {
229
235
return true ;
230
236
}
@@ -343,16 +349,20 @@ protected function processInputFieldValue($value)
343
349
}
344
350
345
351
if (!empty ($ value ['tmp_name ' ])) {
352
+ $ uploader = $ this ->uploaderFactory ->create (['fileId ' => $ value ]);
353
+ $ fileExtension = $ uploader ->getFileExtension ();
354
+ if (!$ this ->_fileValidator ->isValid ($ fileExtension )) {
355
+ throw new LocalizedException ($ this ->_fileValidator ->getMessages ()[$ fileExtension ]);
356
+ }
357
+ $ uploader ->setFilesDispersion (true );
358
+ $ uploader ->setFilenamesCaseSensitivity (false );
359
+ $ uploader ->setAllowRenameFiles (true );
346
360
try {
347
- $ uploader = $ this ->uploaderFactory ->create (['fileId ' => $ value ]);
348
- $ uploader ->setFilesDispersion (true );
349
- $ uploader ->setFilenamesCaseSensitivity (false );
350
- $ uploader ->setAllowRenameFiles (true );
351
361
$ uploader ->save ($ mediaDir ->getAbsolutePath ($ this ->_entityTypeCode ), $ value ['name ' ]);
352
- $ result = $ uploader ->getUploadedFileName ();
353
362
} catch (\Exception $ e ) {
354
363
$ this ->_logger ->critical ($ e );
355
364
}
365
+ $ result = $ uploader ->getUploadedFileName ();
356
366
}
357
367
358
368
return $ result ;
0 commit comments