77
88use Magento \Framework \App \Filesystem \DirectoryList ;
99use Magento \Framework \App \ObjectManager ;
10+ use Magento \Framework \Exception \FileSystemException ;
11+ use Magento \Framework \Exception \LocalizedException ;
1012use Magento \Framework \Exception \ValidatorException ;
1113use Magento \Framework \Filesystem ;
1214use Magento \Framework \Filesystem \Directory \TargetDirectory ;
@@ -56,18 +58,14 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
5658 'png ' => 'image/png ' ,
5759 ];
5860
59- const DEFAULT_FILE_TYPE = 'application/octet-stream ' ;
61+ public const DEFAULT_FILE_TYPE = 'application/octet-stream ' ;
6062
6163 /**
62- * Image factory.
63- *
6464 * @var \Magento\Framework\Image\AdapterFactory
6565 */
6666 protected $ _imageFactory ;
6767
6868 /**
69- * Validator.
70- *
7169 * @var \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension
7270 */
7371 protected $ _validator ;
@@ -114,6 +112,8 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
114112
115113 /**
116114 * Directory and filename must be no more than 255 characters in length
115+ *
116+ * @var int
117117 */
118118 private $ maxFilenameLength = 255 ;
119119
@@ -132,8 +132,8 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
132132 * @param string|null $filePath
133133 * @param \Magento\Framework\Math\Random|null $random
134134 * @param TargetDirectory|null $targetDirectory
135- * @throws \Magento\Framework\Exception\ FileSystemException
136- * @throws \Magento\Framework\Exception\ LocalizedException
135+ * @throws FileSystemException
136+ * @throws LocalizedException
137137 */
138138 public function __construct (
139139 \Magento \MediaStorage \Helper \File \Storage \Database $ coreFileStorageDb ,
@@ -182,7 +182,7 @@ public function init()
182182 * @param string $fileName
183183 * @param bool $renameFileOff
184184 * @return array
185- * @throws \Magento\Framework\Exception\ LocalizedException
185+ * @throws LocalizedException
186186 */
187187 public function move ($ fileName , $ renameFileOff = false )
188188 {
@@ -197,17 +197,20 @@ public function move($fileName, $renameFileOff = false)
197197 }
198198
199199 $ this ->_setUploadFile ($ tmpFilePath );
200- $ rootDirectory = $ this ->getTargetDirectory () ->getDirectoryRead (DirectoryList::ROOT );
200+ $ rootDirectory = $ this ->targetDirectory ->getDirectoryRead (DirectoryList::ROOT );
201201 $ destDir = $ rootDirectory ->getAbsolutePath ($ this ->getDestDir ());
202202 $ result = $ this ->save ($ destDir );
203- unset($ result ['path ' ]);
204- $ result ['name ' ] = self ::getCorrectFileName ($ result ['name ' ]);
205203
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+ }
211214 }
212215
213216 return $ result ;
@@ -219,29 +222,30 @@ public function move($fileName, $renameFileOff = false)
219222 * @param string $url
220223 * @param string $driver
221224 * @return string
222- * @throws \Magento\Framework\Exception\ LocalizedException
225+ * @throws LocalizedException
223226 */
224227 private function downloadFileFromUrl ($ url , $ driver )
225228 {
226229 $ parsedUrlPath = parse_url ($ url , PHP_URL_PATH );
230+
227231 if (!$ parsedUrlPath ) {
228- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Could not parse resource url. ' ));
232+ throw new LocalizedException (__ ('Could not parse resource url. ' ));
229233 }
230234 $ urlPathValues = explode ('/ ' , $ parsedUrlPath );
231235 $ fileName = preg_replace ('/[^a-z0-9\._-]+/i ' , '' , end ($ urlPathValues ));
232-
236+ //phpcs:ignore Magento2.Functions.DiscouragedFunction
233237 $ fileExtension = pathinfo ($ fileName , PATHINFO_EXTENSION );
238+
234239 if ($ fileExtension && !$ this ->checkAllowedExtension ($ fileExtension )) {
235- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Disallowed file type. ' ));
240+ throw new LocalizedException (__ ('Disallowed file type. ' ));
236241 }
237-
238242 $ tmpFileName = str_replace (". $ fileExtension " , '' , $ fileName );
239243 $ tmpFileName .= '_ ' . $ this ->random ->getRandomString (16 );
240244 $ tmpFileName .= $ fileExtension ? ". $ fileExtension " : '' ;
241245 $ tmpFilePath = $ this ->_directory ->getRelativePath ($ this ->getTempFilePath ($ tmpFileName ));
242246
243247 if (!$ this ->_directory ->isWritable ($ this ->getTmpDir ())) {
244- throw new \ Magento \ Framework \ Exception \ LocalizedException (
248+ throw new LocalizedException (
245249 __ ('Import images directory must be writable in order to process remote images. ' )
246250 );
247251 }
@@ -253,26 +257,12 @@ private function downloadFileFromUrl($url, $driver)
253257 return $ tmpFilePath ;
254258 }
255259
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-
270260 /**
271261 * Prepare information about the file for moving
272262 *
273263 * @param string $filePath
274264 * @return void
275- * @throws \Magento\Framework\Exception\ LocalizedException
265+ * @throws LocalizedException
276266 */
277267 protected function _setUploadFile ($ filePath )
278268 {
@@ -290,7 +280,7 @@ protected function _setUploadFile($filePath)
290280 $ readable = false ;
291281 }
292282 if (!$ readable ) {
293- throw new \ Magento \ Framework \ Exception \ LocalizedException (
283+ throw new LocalizedException (
294284 __ ('File \'%1 \' was not found or has read restriction. ' , $ filePath )
295285 );
296286 }
@@ -322,7 +312,7 @@ protected function _readFileInfo($filePath)
322312 * Validate uploaded file by type and etc.
323313 *
324314 * @return void
325- * @throws \Magento\Framework\Exception\ LocalizedException
315+ * @throws LocalizedException
326316 */
327317 protected function _validateFile ()
328318 {
@@ -335,7 +325,7 @@ protected function _validateFile()
335325
336326 $ fileExtension = pathinfo ($ filePath , PATHINFO_EXTENSION );
337327 if (!$ this ->checkAllowedExtension ($ fileExtension )) {
338- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('Disallowed file type. ' ));
328+ throw new LocalizedException (__ ('Disallowed file type. ' ));
339329 }
340330 //run validate callbacks
341331 foreach ($ this ->_validateCallbacks as $ params ) {
@@ -405,7 +395,7 @@ public function getDestDir()
405395 */
406396 public function setDestDir ($ path )
407397 {
408- $ directoryRoot = $ this ->getTargetDirectory () ->getDirectoryWrite (DirectoryList::ROOT );
398+ $ directoryRoot = $ this ->targetDirectory ->getDirectoryWrite (DirectoryList::ROOT );
409399 if (is_string ($ path ) && $ directoryRoot ->isWritable ($ path )) {
410400 $ this ->_destDir = $ path ;
411401 return true ;
@@ -429,7 +419,7 @@ protected function _moveFile($tmpPath, $destPath)
429419 $ destinationRealPath = $ this ->_directory ->getDriver ()->getRealPath ($ destPath );
430420 $ relativeDestPath = $ this ->_directory ->getRelativePath ($ destPath );
431421 $ isSameFile = $ tmpRealPath === $ destinationRealPath ;
432- $ rootDirectory = $ this ->getTargetDirectory () ->getDirectoryWrite (DirectoryList::ROOT );
422+ $ rootDirectory = $ this ->targetDirectory ->getDirectoryWrite (DirectoryList::ROOT );
433423 return $ isSameFile ?: $ this ->_directory ->copyFile ($ tmpPath , $ relativeDestPath , $ rootDirectory );
434424 } else {
435425 return false ;
0 commit comments