7
7
8
8
namespace Magento \Customer \Model ;
9
9
10
+ use Magento \Framework \App \Filesystem \DirectoryList ;
11
+ use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \Filesystem \Directory \WriteFactory ;
13
+
10
14
/**
11
- * Processor class for work with uploaded files
15
+ * Processor class for work with uploaded files.
16
+ *
17
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12
18
*/
13
19
class FileProcessor
14
20
{
@@ -20,7 +26,7 @@ class FileProcessor
20
26
/**
21
27
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
22
28
*/
23
- private $ mediaDirectory ;
29
+ private $ mediaEntityDirectory ;
24
30
25
31
/**
26
32
* @var \Magento\MediaStorage\Model\File\UploaderFactory
@@ -60,6 +66,9 @@ class FileProcessor
60
66
* @param string $entityTypeCode
61
67
* @param \Magento\Framework\File\Mime $mime
62
68
* @param array $allowedExtensions
69
+ * @param WriteFactory|null $writeFactory
70
+ * @param DirectoryList|null $directoryList
71
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
63
72
*/
64
73
public function __construct (
65
74
\Magento \Framework \Filesystem $ filesystem ,
@@ -68,15 +77,22 @@ public function __construct(
68
77
\Magento \Framework \Url \EncoderInterface $ urlEncoder ,
69
78
$ entityTypeCode ,
70
79
\Magento \Framework \File \Mime $ mime ,
71
- array $ allowedExtensions = []
80
+ array $ allowedExtensions = [],
81
+ WriteFactory $ writeFactory = null ,
82
+ DirectoryList $ directoryList = null
72
83
) {
73
- $ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA );
74
84
$ this ->uploaderFactory = $ uploaderFactory ;
75
85
$ this ->urlBuilder = $ urlBuilder ;
76
86
$ this ->urlEncoder = $ urlEncoder ;
77
87
$ this ->entityTypeCode = $ entityTypeCode ;
78
88
$ this ->mime = $ mime ;
79
89
$ this ->allowedExtensions = $ allowedExtensions ;
90
+ $ writeFactory = $ writeFactory ?? ObjectManager::getInstance ()->get (WriteFactory::class);
91
+ $ directoryList = $ directoryList ?? ObjectManager::getInstance ()->get (DirectoryList::class);
92
+ $ this ->mediaEntityDirectory = $ writeFactory ->create (
93
+ $ directoryList ->getPath (DirectoryList::MEDIA )
94
+ . DIRECTORY_SEPARATOR . $ this ->entityTypeCode
95
+ );
80
96
}
81
97
82
98
/**
@@ -87,9 +103,7 @@ public function __construct(
87
103
*/
88
104
public function getBase64EncodedData ($ fileName )
89
105
{
90
- $ filePath = $ this ->entityTypeCode . '/ ' . ltrim ($ fileName , '/ ' );
91
-
92
- $ fileContent = $ this ->mediaDirectory ->readFile ($ filePath );
106
+ $ fileContent = $ this ->mediaEntityDirectory ->readFile ($ fileName );
93
107
94
108
$ encodedContent = base64_encode ($ fileContent );
95
109
return $ encodedContent ;
@@ -103,9 +117,7 @@ public function getBase64EncodedData($fileName)
103
117
*/
104
118
public function getStat ($ fileName )
105
119
{
106
- $ filePath = $ this ->entityTypeCode . '/ ' . ltrim ($ fileName , '/ ' );
107
-
108
- $ result = $ this ->mediaDirectory ->stat ($ filePath );
120
+ $ result = $ this ->mediaEntityDirectory ->stat ($ fileName );
109
121
return $ result ;
110
122
}
111
123
@@ -117,8 +129,7 @@ public function getStat($fileName)
117
129
*/
118
130
public function getMimeType ($ fileName )
119
131
{
120
- $ filePath = $ this ->entityTypeCode . '/ ' . ltrim ($ fileName , '/ ' );
121
- $ absoluteFilePath = $ this ->mediaDirectory ->getAbsolutePath ($ filePath );
132
+ $ absoluteFilePath = $ this ->mediaEntityDirectory ->getAbsolutePath ($ fileName );
122
133
123
134
$ result = $ this ->mime ->getMimeType ($ absoluteFilePath );
124
135
return $ result ;
@@ -132,9 +143,7 @@ public function getMimeType($fileName)
132
143
*/
133
144
public function isExist ($ fileName )
134
145
{
135
- $ filePath = $ this ->entityTypeCode . '/ ' . ltrim ($ fileName , '/ ' );
136
-
137
- $ result = $ this ->mediaDirectory ->isExist ($ filePath );
146
+ $ result = $ this ->mediaEntityDirectory ->isExist ($ fileName );
138
147
return $ result ;
139
148
}
140
149
@@ -150,9 +159,9 @@ public function getViewUrl($filePath, $type)
150
159
$ viewUrl = '' ;
151
160
152
161
if ($ this ->entityTypeCode == \Magento \Customer \Api \AddressMetadataInterface::ENTITY_TYPE_ADDRESS ) {
153
- $ filePath = $ this ->entityTypeCode . ' / ' . ltrim ($ filePath , '/ ' );
162
+ $ filePath = $ this ->entityTypeCode . DIRECTORY_SEPARATOR . ltrim ($ filePath , '/ ' );
154
163
$ viewUrl = $ this ->urlBuilder ->getBaseUrl (['_type ' => \Magento \Framework \UrlInterface::URL_TYPE_MEDIA ])
155
- . $ this ->mediaDirectory ->getRelativePath ($ filePath );
164
+ . $ this ->mediaEntityDirectory ->getRelativePath ($ filePath );
156
165
}
157
166
158
167
if ($ this ->entityTypeCode == \Magento \Customer \Api \CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER ) {
@@ -181,8 +190,8 @@ public function saveTemporaryFile($fileId)
181
190
$ uploader ->setAllowRenameFiles (true );
182
191
$ uploader ->setAllowedExtensions ($ this ->allowedExtensions );
183
192
184
- $ path = $ this ->mediaDirectory ->getAbsolutePath (
185
- $ this -> entityTypeCode . ' / ' . self ::TMP_DIR
193
+ $ path = $ this ->mediaEntityDirectory ->getAbsolutePath (
194
+ DIRECTORY_SEPARATOR . self ::TMP_DIR
186
195
);
187
196
188
197
$ result = $ uploader ->save ($ path );
@@ -207,38 +216,36 @@ public function moveTemporaryFile($fileName)
207
216
{
208
217
$ fileName = ltrim ($ fileName , '/ ' );
209
218
210
- $ dispersionPath = \Magento \MediaStorage \Model \File \Uploader::getDispersionPath ($ fileName );
211
- $ destinationPath = $ this ->entityTypeCode . $ dispersionPath ;
219
+ $ destinationPath = \Magento \MediaStorage \Model \File \Uploader::getDispersionPath ($ fileName );
212
220
213
- if (!$ this ->mediaDirectory ->create ($ destinationPath )) {
221
+ if (!$ this ->mediaEntityDirectory ->create ($ destinationPath )) {
214
222
throw new \Magento \Framework \Exception \LocalizedException (
215
223
__ ('Unable to create directory %1. ' , $ destinationPath )
216
224
);
217
225
}
218
226
219
- if (!$ this ->mediaDirectory ->isWritable ($ destinationPath )) {
227
+ if (!$ this ->mediaEntityDirectory ->isWritable ($ destinationPath )) {
220
228
throw new \Magento \Framework \Exception \LocalizedException (
221
229
__ ('Destination folder is not writable or does not exists. ' )
222
230
);
223
231
}
224
232
225
233
$ destinationFileName = \Magento \MediaStorage \Model \File \Uploader::getNewFileName (
226
- $ this ->mediaDirectory ->getAbsolutePath ($ destinationPath ) . '/ ' . $ fileName
234
+ $ this ->mediaEntityDirectory ->getAbsolutePath ($ destinationPath ) . '/ ' . $ fileName
227
235
);
228
236
229
237
try {
230
- $ this ->mediaDirectory ->renameFile (
231
- $ this -> entityTypeCode . ' / ' . self ::TMP_DIR . ' / ' . $ fileName ,
232
- $ destinationPath . ' / ' . $ destinationFileName
238
+ $ this ->mediaEntityDirectory ->renameFile (
239
+ DIRECTORY_SEPARATOR . self ::TMP_DIR . DIRECTORY_SEPARATOR . $ fileName ,
240
+ $ destinationPath . DIRECTORY_SEPARATOR . $ destinationFileName
233
241
);
234
242
} catch (\Exception $ e ) {
235
243
throw new \Magento \Framework \Exception \LocalizedException (
236
244
__ ('Something went wrong while saving the file. ' )
237
245
);
238
246
}
239
247
240
- $ fileName = $ dispersionPath . '/ ' . $ destinationFileName ;
241
- return $ fileName ;
248
+ return $ destinationPath . DIRECTORY_SEPARATOR . $ destinationFileName ;
242
249
}
243
250
244
251
/**
@@ -249,9 +256,9 @@ public function moveTemporaryFile($fileName)
249
256
*/
250
257
public function removeUploadedFile ($ fileName )
251
258
{
252
- $ filePath = $ this -> entityTypeCode . ' / ' . ltrim ($ fileName , '/ ' );
259
+ $ filePath = DIRECTORY_SEPARATOR . ltrim ($ fileName , '/ ' );
253
260
254
- $ result = $ this ->mediaDirectory ->delete ($ filePath );
261
+ $ result = $ this ->mediaEntityDirectory ->delete ($ filePath );
255
262
return $ result ;
256
263
}
257
264
}
0 commit comments