7
7
8
8
use Magento \Framework \App \Filesystem \DirectoryList ;
9
9
use Magento \Framework \App \ObjectManager ;
10
+ use Magento \Framework \Exception \FileSystemException ;
11
+ use Magento \Framework \Exception \LocalizedException ;
10
12
use Magento \Framework \Exception \ValidatorException ;
11
13
use Magento \Framework \Filesystem ;
12
14
use Magento \Framework \Filesystem \Directory \TargetDirectory ;
@@ -56,18 +58,14 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
56
58
'png ' => 'image/png ' ,
57
59
];
58
60
59
- const DEFAULT_FILE_TYPE = 'application/octet-stream ' ;
61
+ public const DEFAULT_FILE_TYPE = 'application/octet-stream ' ;
60
62
61
63
/**
62
- * Image factory.
63
- *
64
64
* @var \Magento\Framework\Image\AdapterFactory
65
65
*/
66
66
protected $ _imageFactory ;
67
67
68
68
/**
69
- * Validator.
70
- *
71
69
* @var \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension
72
70
*/
73
71
protected $ _validator ;
@@ -114,6 +112,8 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
114
112
115
113
/**
116
114
* Directory and filename must be no more than 255 characters in length
115
+ *
116
+ * @var int
117
117
*/
118
118
private $ maxFilenameLength = 255 ;
119
119
@@ -132,8 +132,8 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
132
132
* @param string|null $filePath
133
133
* @param \Magento\Framework\Math\Random|null $random
134
134
* @param TargetDirectory|null $targetDirectory
135
- * @throws \Magento\Framework\Exception\ FileSystemException
136
- * @throws \Magento\Framework\Exception\ LocalizedException
135
+ * @throws FileSystemException
136
+ * @throws LocalizedException
137
137
*/
138
138
public function __construct (
139
139
\Magento \MediaStorage \Helper \File \Storage \Database $ coreFileStorageDb ,
@@ -182,7 +182,7 @@ public function init()
182
182
* @param string $fileName
183
183
* @param bool $renameFileOff
184
184
* @return array
185
- * @throws \Magento\Framework\Exception\ LocalizedException
185
+ * @throws LocalizedException
186
186
*/
187
187
public function move ($ fileName , $ renameFileOff = false )
188
188
{
@@ -197,17 +197,20 @@ public function move($fileName, $renameFileOff = false)
197
197
}
198
198
199
199
$ this ->_setUploadFile ($ tmpFilePath );
200
- $ rootDirectory = $ this ->getTargetDirectory () ->getDirectoryRead (DirectoryList::ROOT );
200
+ $ rootDirectory = $ this ->targetDirectory ->getDirectoryRead (DirectoryList::ROOT );
201
201
$ destDir = $ rootDirectory ->getAbsolutePath ($ this ->getDestDir ());
202
202
$ result = $ this ->save ($ destDir );
203
- unset($ result ['path ' ]);
204
- $ result ['name ' ] = self ::getCorrectFileName ($ result ['name ' ]);
205
203
206
- // Directory and filename must be no more than 255 characters in length
207
- if (strlen ($ result ['file ' ]) > $ this ->maxFilenameLength ) {
208
- throw new \LengthException (
209
- __ ('Filename is too long; must be %1 characters or less ' , $ this ->maxFilenameLength )
210
- );
204
+ if (\is_array ($ result )) {
205
+ unset($ result ['path ' ]);
206
+ $ result ['name ' ] = self ::getCorrectFileName ($ result ['name ' ]);
207
+
208
+ // Directory and filename must be no more than 255 characters in length
209
+ if (strlen ($ result ['file ' ]) > $ this ->maxFilenameLength ) {
210
+ throw new \LengthException (
211
+ __ ('Filename is too long; must be %1 characters or less ' , $ this ->maxFilenameLength )
212
+ );
213
+ }
211
214
}
212
215
213
216
return $ result ;
@@ -219,29 +222,30 @@ public function move($fileName, $renameFileOff = false)
219
222
* @param string $url
220
223
* @param string $driver
221
224
* @return string
222
- * @throws \Magento\Framework\Exception\ LocalizedException
225
+ * @throws LocalizedException
223
226
*/
224
227
private function downloadFileFromUrl ($ url , $ driver )
225
228
{
226
229
$ parsedUrlPath = parse_url ($ url , PHP_URL_PATH );
230
+
227
231
if (!$ parsedUrlPath ) {
228
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Could not parse resource url. ' ));
232
+ throw new LocalizedException (__ ('Could not parse resource url. ' ));
229
233
}
230
234
$ urlPathValues = explode ('/ ' , $ parsedUrlPath );
231
235
$ fileName = preg_replace ('/[^a-z0-9\._-]+/i ' , '' , end ($ urlPathValues ));
232
-
236
+ //phpcs:ignore Magento2.Functions.DiscouragedFunction
233
237
$ fileExtension = pathinfo ($ fileName , PATHINFO_EXTENSION );
238
+
234
239
if ($ fileExtension && !$ this ->checkAllowedExtension ($ fileExtension )) {
235
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Disallowed file type. ' ));
240
+ throw new LocalizedException (__ ('Disallowed file type. ' ));
236
241
}
237
-
238
242
$ tmpFileName = str_replace (". $ fileExtension " , '' , $ fileName );
239
243
$ tmpFileName .= '_ ' . $ this ->random ->getRandomString (16 );
240
244
$ tmpFileName .= $ fileExtension ? ". $ fileExtension " : '' ;
241
245
$ tmpFilePath = $ this ->_directory ->getRelativePath ($ this ->getTempFilePath ($ tmpFileName ));
242
246
243
247
if (!$ this ->_directory ->isWritable ($ this ->getTmpDir ())) {
244
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
248
+ throw new LocalizedException (
245
249
__ ('Import images directory must be writable in order to process remote images. ' )
246
250
);
247
251
}
@@ -253,26 +257,12 @@ private function downloadFileFromUrl($url, $driver)
253
257
return $ tmpFilePath ;
254
258
}
255
259
256
- /**
257
- * Retrieves target directory.
258
- *
259
- * @return TargetDirectory
260
- */
261
- private function getTargetDirectory (): TargetDirectory
262
- {
263
- if (!isset ($ this ->targetDirectory )) {
264
- $ this ->targetDirectory = ObjectManager::getInstance ()->get (TargetDirectory::class);
265
- }
266
-
267
- return $ this ->targetDirectory ;
268
- }
269
-
270
260
/**
271
261
* Prepare information about the file for moving
272
262
*
273
263
* @param string $filePath
274
264
* @return void
275
- * @throws \Magento\Framework\Exception\ LocalizedException
265
+ * @throws LocalizedException
276
266
*/
277
267
protected function _setUploadFile ($ filePath )
278
268
{
@@ -290,7 +280,7 @@ protected function _setUploadFile($filePath)
290
280
$ readable = false ;
291
281
}
292
282
if (!$ readable ) {
293
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
283
+ throw new LocalizedException (
294
284
__ ('File \'%1 \' was not found or has read restriction. ' , $ filePath )
295
285
);
296
286
}
@@ -322,7 +312,7 @@ protected function _readFileInfo($filePath)
322
312
* Validate uploaded file by type and etc.
323
313
*
324
314
* @return void
325
- * @throws \Magento\Framework\Exception\ LocalizedException
315
+ * @throws LocalizedException
326
316
*/
327
317
protected function _validateFile ()
328
318
{
@@ -335,7 +325,7 @@ protected function _validateFile()
335
325
336
326
$ fileExtension = pathinfo ($ filePath , PATHINFO_EXTENSION );
337
327
if (!$ this ->checkAllowedExtension ($ fileExtension )) {
338
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Disallowed file type. ' ));
328
+ throw new LocalizedException (__ ('Disallowed file type. ' ));
339
329
}
340
330
//run validate callbacks
341
331
foreach ($ this ->_validateCallbacks as $ params ) {
@@ -405,7 +395,7 @@ public function getDestDir()
405
395
*/
406
396
public function setDestDir ($ path )
407
397
{
408
- $ directoryRoot = $ this ->getTargetDirectory () ->getDirectoryWrite (DirectoryList::ROOT );
398
+ $ directoryRoot = $ this ->targetDirectory ->getDirectoryWrite (DirectoryList::ROOT );
409
399
if (is_string ($ path ) && $ directoryRoot ->isWritable ($ path )) {
410
400
$ this ->_destDir = $ path ;
411
401
return true ;
@@ -429,7 +419,7 @@ protected function _moveFile($tmpPath, $destPath)
429
419
$ destinationRealPath = $ this ->_directory ->getDriver ()->getRealPath ($ destPath );
430
420
$ relativeDestPath = $ this ->_directory ->getRelativePath ($ destPath );
431
421
$ isSameFile = $ tmpRealPath === $ destinationRealPath ;
432
- $ rootDirectory = $ this ->getTargetDirectory () ->getDirectoryWrite (DirectoryList::ROOT );
422
+ $ rootDirectory = $ this ->targetDirectory ->getDirectoryWrite (DirectoryList::ROOT );
433
423
return $ isSameFile ?: $ this ->_directory ->copyFile ($ tmpPath , $ relativeDestPath , $ rootDirectory );
434
424
} else {
435
425
return false ;
0 commit comments