55 */
66namespace Magento \Catalog \Model ;
77
8+ use Magento \Framework \App \Filesystem \DirectoryList ;
9+ use Magento \Framework \Exception \LocalizedException ;
810use Magento \Framework \File \Uploader ;
11+ use Magento \Framework \App \ObjectManager ;
12+ use Magento \Framework \File \Name ;
13+ use Magento \Framework \Filesystem ;
14+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
15+ use Magento \MediaStorage \Helper \File \Storage \Database ;
16+ use Magento \MediaStorage \Model \File \UploaderFactory ;
17+ use Magento \Store \Model \StoreManagerInterface ;
18+ use Psr \Log \LoggerInterface ;
919
1020/**
1121 * Catalog image uploader
1222 */
1323class ImageUploader
1424{
1525 /**
16- * Core file storage database
17- *
18- * @var \Magento\MediaStorage\Helper\File\Storage\Database
26+ * @var Database
1927 */
2028 protected $ coreFileStorageDatabase ;
2129
2230 /**
23- * Media directory object (writable).
24- *
25- * @var \Magento\Framework\Filesystem\Directory\WriteInterface
31+ * @var WriteInterface
2632 */
2733 protected $ mediaDirectory ;
2834
2935 /**
30- * Uploader factory
31- *
32- * @var \Magento\MediaStorage\Model\File\UploaderFactory
36+ * @var UploaderFactory
3337 */
3438 private $ uploaderFactory ;
3539
3640 /**
37- * Store manager
38- *
39- * @var \Magento\Store\Model\StoreManagerInterface
41+ * @var StoreManagerInterface
4042 */
4143 protected $ storeManager ;
4244
4345 /**
44- * @var \Psr\Log\ LoggerInterface
46+ * @var LoggerInterface
4547 */
4648 protected $ logger ;
4749
4850 /**
49- * Base tmp path
50- *
5151 * @var string
5252 */
5353 protected $ baseTmpPath ;
5454
5555 /**
56- * Base path
57- *
5856 * @var string
5957 */
6058 protected $ basePath ;
6159
6260 /**
63- * Allowed extensions
64- *
6561 * @var string
6662 */
6763 protected $ allowedExtensions ;
6864
6965 /**
70- * List of allowed image mime types
71- *
7266 * @var string[]
7367 */
7468 private $ allowedMimeTypes ;
7569
7670 /**
77- * ImageUploader constructor
71+ * @var Name
72+ */
73+ private $ fileNameLookup ;
74+
75+ /**
76+ * ImageUploader constructor.
7877 *
79- * @param \Magento\MediaStorage\Helper\File\Storage\ Database $coreFileStorageDatabase
80- * @param \Magento\Framework\ Filesystem $filesystem
81- * @param \Magento\MediaStorage\Model\File\ UploaderFactory $uploaderFactory
82- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
83- * @param \Psr\Log\ LoggerInterface $logger
78+ * @param Database $coreFileStorageDatabase
79+ * @param Filesystem $filesystem
80+ * @param UploaderFactory $uploaderFactory
81+ * @param StoreManagerInterface $storeManager
82+ * @param LoggerInterface $logger
8483 * @param string $baseTmpPath
8584 * @param string $basePath
8685 * @param string[] $allowedExtensions
8786 * @param string[] $allowedMimeTypes
87+ * @param Name|null $fileNameLookup
88+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
8889 */
8990 public function __construct (
90- \ Magento \ MediaStorage \ Helper \ File \ Storage \ Database $ coreFileStorageDatabase ,
91- \ Magento \ Framework \ Filesystem $ filesystem ,
92- \ Magento \ MediaStorage \ Model \ File \ UploaderFactory $ uploaderFactory ,
93- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
94- \ Psr \ Log \ LoggerInterface $ logger ,
91+ Database $ coreFileStorageDatabase ,
92+ Filesystem $ filesystem ,
93+ UploaderFactory $ uploaderFactory ,
94+ StoreManagerInterface $ storeManager ,
95+ LoggerInterface $ logger ,
9596 $ baseTmpPath ,
9697 $ basePath ,
9798 $ allowedExtensions ,
98- $ allowedMimeTypes = []
99+ $ allowedMimeTypes = [],
100+ Name $ fileNameLookup = null
99101 ) {
100102 $ this ->coreFileStorageDatabase = $ coreFileStorageDatabase ;
101- $ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (\ Magento \ Framework \ App \ Filesystem \ DirectoryList::MEDIA );
103+ $ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
102104 $ this ->uploaderFactory = $ uploaderFactory ;
103105 $ this ->storeManager = $ storeManager ;
104106 $ this ->logger = $ logger ;
105107 $ this ->baseTmpPath = $ baseTmpPath ;
106108 $ this ->basePath = $ basePath ;
107109 $ this ->allowedExtensions = $ allowedExtensions ;
108110 $ this ->allowedMimeTypes = $ allowedMimeTypes ;
111+ $ this ->fileNameLookup = $ fileNameLookup ?? ObjectManager::getInstance ()->get (Name::class);
109112 }
110113
111114 /**
112115 * Set base tmp path
113116 *
114117 * @param string $baseTmpPath
115- *
116118 * @return void
117119 */
118120 public function setBaseTmpPath ($ baseTmpPath )
@@ -124,7 +126,6 @@ public function setBaseTmpPath($baseTmpPath)
124126 * Set base path
125127 *
126128 * @param string $basePath
127- *
128129 * @return void
129130 */
130131 public function setBasePath ($ basePath )
@@ -136,7 +137,6 @@ public function setBasePath($basePath)
136137 * Set allowed extensions
137138 *
138139 * @param string[] $allowedExtensions
139- *
140140 * @return void
141141 */
142142 public function setAllowedExtensions ($ allowedExtensions )
@@ -179,7 +179,6 @@ public function getAllowedExtensions()
179179 *
180180 * @param string $path
181181 * @param string $imageName
182- *
183182 * @return string
184183 */
185184 public function getFilePath ($ path , $ imageName )
@@ -194,7 +193,7 @@ public function getFilePath($path, $imageName)
194193 * @param bool $returnRelativePath
195194 * @return string
196195 *
197- * @throws \Magento\Framework\Exception\ LocalizedException
196+ * @throws LocalizedException
198197 */
199198 public function moveFileFromTmp ($ imageName , $ returnRelativePath = false )
200199 {
@@ -203,7 +202,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
203202
204203 $ baseImagePath = $ this ->getFilePath (
205204 $ basePath ,
206- Uploader:: getNewFileName (
205+ $ this -> fileNameLookup -> getNewFileName (
207206 $ this ->mediaDirectory ->getAbsolutePath (
208207 $ this ->getFilePath ($ basePath , $ imageName )
209208 )
@@ -222,10 +221,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
222221 );
223222 } catch (\Exception $ e ) {
224223 $ this ->logger ->critical ($ e );
225- throw new \Magento \Framework \Exception \LocalizedException (
226- __ ('Something went wrong while saving the file(s). ' ),
227- $ e
228- );
224+ throw new LocalizedException (__ ('Something went wrong while saving the file(s). ' ), $ e );
229225 }
230226
231227 return $ returnRelativePath ? $ baseImagePath : $ imageName ;
@@ -235,10 +231,9 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
235231 * Checking file for save and save it to tmp dir
236232 *
237233 * @param string $fileId
238- *
239234 * @return string[]
240235 *
241- * @throws \Magento\Framework\Exception\ LocalizedException
236+ * @throws LocalizedException
242237 */
243238 public function saveFileToTmpDir ($ fileId )
244239 {
@@ -249,15 +244,13 @@ public function saveFileToTmpDir($fileId)
249244 $ uploader ->setAllowedExtensions ($ this ->getAllowedExtensions ());
250245 $ uploader ->setAllowRenameFiles (true );
251246 if (!$ uploader ->checkMimeType ($ this ->allowedMimeTypes )) {
252- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('File validation failed. ' ));
247+ throw new LocalizedException (__ ('File validation failed. ' ));
253248 }
254249 $ result = $ uploader ->save ($ this ->mediaDirectory ->getAbsolutePath ($ baseTmpPath ));
255250 unset($ result ['path ' ]);
256251
257252 if (!$ result ) {
258- throw new \Magento \Framework \Exception \LocalizedException (
259- __ ('File can not be saved to the destination folder. ' )
260- );
253+ throw new LocalizedException (__ ('File can not be saved to the destination folder. ' ));
261254 }
262255
263256 /**
@@ -277,7 +270,7 @@ public function saveFileToTmpDir($fileId)
277270 $ this ->coreFileStorageDatabase ->saveFile ($ relativePath );
278271 } catch (\Exception $ e ) {
279272 $ this ->logger ->critical ($ e );
280- throw new \ Magento \ Framework \ Exception \ LocalizedException (
273+ throw new LocalizedException (
281274 __ ('Something went wrong while saving the file(s). ' ),
282275 $ e
283276 );
0 commit comments