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