5
5
*/
6
6
namespace Magento \Catalog \Model ;
7
7
8
+ use Magento \Framework \App \Filesystem \DirectoryList ;
9
+ use Magento \Framework \Exception \LocalizedException ;
8
10
use 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 ;
9
19
10
20
/**
11
21
* Catalog image uploader
12
22
*/
13
23
class ImageUploader
14
24
{
15
25
/**
16
- * Core file storage database
17
- *
18
- * @var \Magento\MediaStorage\Helper\File\Storage\Database
26
+ * @var Database
19
27
*/
20
28
protected $ coreFileStorageDatabase ;
21
29
22
30
/**
23
- * Media directory object (writable).
24
- *
25
- * @var \Magento\Framework\Filesystem\Directory\WriteInterface
31
+ * @var WriteInterface
26
32
*/
27
33
protected $ mediaDirectory ;
28
34
29
35
/**
30
- * Uploader factory
31
- *
32
- * @var \Magento\MediaStorage\Model\File\UploaderFactory
36
+ * @var UploaderFactory
33
37
*/
34
38
private $ uploaderFactory ;
35
39
36
40
/**
37
- * Store manager
38
- *
39
- * @var \Magento\Store\Model\StoreManagerInterface
41
+ * @var StoreManagerInterface
40
42
*/
41
43
protected $ storeManager ;
42
44
43
45
/**
44
- * @var \Psr\Log\ LoggerInterface
46
+ * @var LoggerInterface
45
47
*/
46
48
protected $ logger ;
47
49
48
50
/**
49
- * Base tmp path
50
- *
51
51
* @var string
52
52
*/
53
53
protected $ baseTmpPath ;
54
54
55
55
/**
56
- * Base path
57
- *
58
56
* @var string
59
57
*/
60
58
protected $ basePath ;
61
59
62
60
/**
63
- * Allowed extensions
64
- *
65
61
* @var string
66
62
*/
67
63
protected $ allowedExtensions ;
68
64
69
65
/**
70
- * List of allowed image mime types
71
- *
72
66
* @var string[]
73
67
*/
74
68
private $ allowedMimeTypes ;
75
69
76
70
/**
77
- * ImageUploader constructor
71
+ * @var Name
72
+ */
73
+ private $ fileNameLookup ;
74
+
75
+ /**
76
+ * ImageUploader constructor.
78
77
*
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
84
83
* @param string $baseTmpPath
85
84
* @param string $basePath
86
85
* @param string[] $allowedExtensions
87
86
* @param string[] $allowedMimeTypes
87
+ * @param Name|null $fileNameLookup
88
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
88
89
*/
89
90
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 ,
95
96
$ baseTmpPath ,
96
97
$ basePath ,
97
98
$ allowedExtensions ,
98
- $ allowedMimeTypes = []
99
+ $ allowedMimeTypes = [],
100
+ Name $ fileNameLookup = null
99
101
) {
100
102
$ this ->coreFileStorageDatabase = $ coreFileStorageDatabase ;
101
- $ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (\ Magento \ Framework \ App \ Filesystem \ DirectoryList::MEDIA );
103
+ $ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
102
104
$ this ->uploaderFactory = $ uploaderFactory ;
103
105
$ this ->storeManager = $ storeManager ;
104
106
$ this ->logger = $ logger ;
105
107
$ this ->baseTmpPath = $ baseTmpPath ;
106
108
$ this ->basePath = $ basePath ;
107
109
$ this ->allowedExtensions = $ allowedExtensions ;
108
110
$ this ->allowedMimeTypes = $ allowedMimeTypes ;
111
+ $ this ->fileNameLookup = $ fileNameLookup ?? ObjectManager::getInstance ()->get (Name::class);
109
112
}
110
113
111
114
/**
112
115
* Set base tmp path
113
116
*
114
117
* @param string $baseTmpPath
115
- *
116
118
* @return void
117
119
*/
118
120
public function setBaseTmpPath ($ baseTmpPath )
@@ -124,7 +126,6 @@ public function setBaseTmpPath($baseTmpPath)
124
126
* Set base path
125
127
*
126
128
* @param string $basePath
127
- *
128
129
* @return void
129
130
*/
130
131
public function setBasePath ($ basePath )
@@ -136,7 +137,6 @@ public function setBasePath($basePath)
136
137
* Set allowed extensions
137
138
*
138
139
* @param string[] $allowedExtensions
139
- *
140
140
* @return void
141
141
*/
142
142
public function setAllowedExtensions ($ allowedExtensions )
@@ -179,7 +179,6 @@ public function getAllowedExtensions()
179
179
*
180
180
* @param string $path
181
181
* @param string $imageName
182
- *
183
182
* @return string
184
183
*/
185
184
public function getFilePath ($ path , $ imageName )
@@ -194,7 +193,7 @@ public function getFilePath($path, $imageName)
194
193
* @param bool $returnRelativePath
195
194
* @return string
196
195
*
197
- * @throws \Magento\Framework\Exception\ LocalizedException
196
+ * @throws LocalizedException
198
197
*/
199
198
public function moveFileFromTmp ($ imageName , $ returnRelativePath = false )
200
199
{
@@ -203,7 +202,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
203
202
204
203
$ baseImagePath = $ this ->getFilePath (
205
204
$ basePath ,
206
- Uploader:: getNewFileName (
205
+ $ this -> fileNameLookup -> getNewFileName (
207
206
$ this ->mediaDirectory ->getAbsolutePath (
208
207
$ this ->getFilePath ($ basePath , $ imageName )
209
208
)
@@ -222,10 +221,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
222
221
);
223
222
} catch (\Exception $ e ) {
224
223
$ 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 );
229
225
}
230
226
231
227
return $ returnRelativePath ? $ baseImagePath : $ imageName ;
@@ -235,10 +231,9 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
235
231
* Checking file for save and save it to tmp dir
236
232
*
237
233
* @param string $fileId
238
- *
239
234
* @return string[]
240
235
*
241
- * @throws \Magento\Framework\Exception\ LocalizedException
236
+ * @throws LocalizedException
242
237
*/
243
238
public function saveFileToTmpDir ($ fileId )
244
239
{
@@ -249,15 +244,13 @@ public function saveFileToTmpDir($fileId)
249
244
$ uploader ->setAllowedExtensions ($ this ->getAllowedExtensions ());
250
245
$ uploader ->setAllowRenameFiles (true );
251
246
if (!$ uploader ->checkMimeType ($ this ->allowedMimeTypes )) {
252
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ('File validation failed. ' ));
247
+ throw new LocalizedException (__ ('File validation failed. ' ));
253
248
}
254
249
$ result = $ uploader ->save ($ this ->mediaDirectory ->getAbsolutePath ($ baseTmpPath ));
255
250
unset($ result ['path ' ]);
256
251
257
252
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. ' ));
261
254
}
262
255
263
256
/**
@@ -277,7 +270,7 @@ public function saveFileToTmpDir($fileId)
277
270
$ this ->coreFileStorageDatabase ->saveFile ($ relativePath );
278
271
} catch (\Exception $ e ) {
279
272
$ this ->logger ->critical ($ e );
280
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
273
+ throw new LocalizedException (
281
274
__ ('Something went wrong while saving the file(s). ' ),
282
275
$ e
283
276
);
0 commit comments