6
6
namespace Magento \CatalogImportExport \Model \Import ;
7
7
8
8
use Magento \Framework \App \Filesystem \DirectoryList ;
9
+ use Magento \Framework \App \ObjectManager ;
9
10
use Magento \Framework \Filesystem \DriverPool ;
10
11
11
12
/**
12
13
* Import entity product model
13
14
*
14
15
* @api
15
16
* @since 100.0.2
17
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
+ * phpcs:disable Magento2.Functions.DiscouragedFunction
16
19
*/
17
20
class Uploader extends \Magento \MediaStorage \Model \File \Uploader
18
21
{
@@ -31,6 +34,13 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
31
34
*/
32
35
protected $ _tmpDir = '' ;
33
36
37
+ /**
38
+ * Download directory for url-based resources.
39
+ *
40
+ * @var string
41
+ */
42
+ private $ downloadDir ;
43
+
34
44
/**
35
45
* Destination directory.
36
46
*
@@ -94,6 +104,13 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
94
104
*/
95
105
protected $ _coreFileStorage ;
96
106
107
+ /**
108
+ * Instance of random data generator.
109
+ *
110
+ * @var \Magento\Framework\Math\Random
111
+ */
112
+ private $ random ;
113
+
97
114
/**
98
115
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
99
116
* @param \Magento\MediaStorage\Helper\File\Storage $coreFileStorage
@@ -102,6 +119,8 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
102
119
* @param \Magento\Framework\Filesystem $filesystem
103
120
* @param \Magento\Framework\Filesystem\File\ReadFactory $readFactory
104
121
* @param string|null $filePath
122
+ * @param \Magento\Framework\Math\Random|null $random
123
+ * @throws \Magento\Framework\Exception\FileSystemException
105
124
* @throws \Magento\Framework\Exception\LocalizedException
106
125
*/
107
126
public function __construct (
@@ -111,7 +130,8 @@ public function __construct(
111
130
\Magento \MediaStorage \Model \File \Validator \NotProtectedExtension $ validator ,
112
131
\Magento \Framework \Filesystem $ filesystem ,
113
132
\Magento \Framework \Filesystem \File \ReadFactory $ readFactory ,
114
- $ filePath = null
133
+ $ filePath = null ,
134
+ \Magento \Framework \Math \Random $ random = null
115
135
) {
116
136
$ this ->_imageFactory = $ imageFactory ;
117
137
$ this ->_coreFileStorageDb = $ coreFileStorageDb ;
@@ -122,6 +142,8 @@ public function __construct(
122
142
if ($ filePath !== null ) {
123
143
$ this ->_setUploadFile ($ filePath );
124
144
}
145
+ $ this ->random = $ random ?: ObjectManager::getInstance ()->get (\Magento \Framework \Math \Random::class);
146
+ $ this ->downloadDir = DirectoryList::getDefaultConfig ()[DirectoryList::TMP ][DirectoryList::PATH ];
125
147
}
126
148
127
149
/**
@@ -150,52 +172,61 @@ public function init()
150
172
*/
151
173
public function move ($ fileName , $ renameFileOff = false )
152
174
{
153
- if ($ renameFileOff ) {
154
- $ this ->setAllowRenameFiles (false );
155
- }
156
-
157
- if ($ this ->getTmpDir ()) {
158
- $ filePath = $ this ->getTmpDir () . '/ ' ;
159
- } else {
160
- $ filePath = '' ;
161
- }
175
+ $ this ->setAllowRenameFiles (!$ renameFileOff );
162
176
163
177
if (preg_match ('/\bhttps?:\/\//i ' , $ fileName , $ matches )) {
164
178
$ url = str_replace ($ matches [0 ], '' , $ fileName );
165
- $ driver = $ matches [0 ] === $ this ->httpScheme ? DriverPool::HTTP : DriverPool::HTTPS ;
166
- $ read = $ this ->_readFactory ->create ($ url , $ driver );
167
-
168
- //only use filename (for URI with query parameters)
169
- $ parsedUrlPath = parse_url ($ url , PHP_URL_PATH );
170
- if ($ parsedUrlPath ) {
171
- $ urlPathValues = explode ('/ ' , $ parsedUrlPath );
172
- if (!empty ($ urlPathValues )) {
173
- $ fileName = end ($ urlPathValues );
174
- }
175
- }
176
-
177
- $ fileExtension = pathinfo ($ fileName , PATHINFO_EXTENSION );
178
- if ($ fileExtension && !$ this ->checkAllowedExtension ($ fileExtension )) {
179
- throw new \Magento \Framework \Exception \LocalizedException (__ ('Disallowed file type. ' ));
180
- }
181
-
182
- $ fileName = preg_replace ('/[^a-z0-9\._-]+/i ' , '' , $ fileName );
183
- $ relativePath = $ this ->_directory ->getRelativePath ($ filePath . $ fileName );
184
- $ this ->_directory ->writeFile (
185
- $ relativePath ,
186
- $ read ->readAll ()
187
- );
179
+ $ driver = ($ matches [0 ] === $ this ->httpScheme ) ? DriverPool::HTTP : DriverPool::HTTPS ;
180
+ $ tmpFilePath = $ this ->downloadFileFromUrl ($ url , $ driver );
181
+ } else {
182
+ $ tmpDir = $ this ->getTmpDir () ? ($ this ->getTmpDir () . '/ ' ) : '' ;
183
+ $ tmpFilePath = $ this ->_directory ->getRelativePath ($ tmpDir . $ fileName );
188
184
}
189
185
190
- $ filePath = $ this ->_directory ->getRelativePath ($ filePath . $ fileName );
191
- $ this ->_setUploadFile ($ filePath );
186
+ $ this ->_setUploadFile ($ tmpFilePath );
192
187
$ destDir = $ this ->_directory ->getAbsolutePath ($ this ->getDestDir ());
193
188
$ result = $ this ->save ($ destDir );
194
189
unset($ result ['path ' ]);
195
190
$ result ['name ' ] = self ::getCorrectFileName ($ result ['name ' ]);
191
+
196
192
return $ result ;
197
193
}
198
194
195
+ /**
196
+ * Writes a url-based file to the temp directory.
197
+ *
198
+ * @param string $url
199
+ * @param string $driver
200
+ * @return string
201
+ * @throws \Magento\Framework\Exception\LocalizedException
202
+ */
203
+ private function downloadFileFromUrl ($ url , $ driver )
204
+ {
205
+ $ parsedUrlPath = parse_url ($ url , PHP_URL_PATH );
206
+ if (!$ parsedUrlPath ) {
207
+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Could not parse resource url. ' ));
208
+ }
209
+ $ urlPathValues = explode ('/ ' , $ parsedUrlPath );
210
+ $ fileName = preg_replace ('/[^a-z0-9\._-]+/i ' , '' , end ($ urlPathValues ));
211
+
212
+ $ fileExtension = pathinfo ($ fileName , PATHINFO_EXTENSION );
213
+ if ($ fileExtension && !$ this ->checkAllowedExtension ($ fileExtension )) {
214
+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Disallowed file type. ' ));
215
+ }
216
+
217
+ $ tmpFileName = str_replace (". $ fileExtension " , '' , $ fileName );
218
+ $ tmpFileName .= '_ ' . $ this ->random ->getRandomString (16 );
219
+ $ tmpFileName .= $ fileExtension ? ". $ fileExtension " : '' ;
220
+ $ tmpFilePath = $ this ->_directory ->getRelativePath ($ this ->downloadDir . '/ ' . $ tmpFileName );
221
+
222
+ $ this ->_directory ->writeFile (
223
+ $ tmpFilePath ,
224
+ $ this ->_readFactory ->create ($ url , $ driver )->readAll ()
225
+ );
226
+
227
+ return $ tmpFilePath ;
228
+ }
229
+
199
230
/**
200
231
* Prepare information about the file for moving
201
232
*
@@ -238,7 +269,7 @@ protected function _readFileInfo($filePath)
238
269
* Validate uploaded file by type and etc.
239
270
*
240
271
* @return void
241
- * @throws \Exception
272
+ * @throws \Magento\Framework\ Exception\LocalizedException
242
273
*/
243
274
protected function _validateFile ()
244
275
{
@@ -251,8 +282,7 @@ protected function _validateFile()
251
282
252
283
$ fileExtension = pathinfo ($ filePath , PATHINFO_EXTENSION );
253
284
if (!$ this ->checkAllowedExtension ($ fileExtension )) {
254
- $ this ->_directory ->delete ($ filePath );
255
- throw new \Exception ('Disallowed file type. ' );
285
+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Disallowed file type. ' ));
256
286
}
257
287
//run validate callbacks
258
288
foreach ($ this ->_validateCallbacks as $ params ) {
@@ -356,6 +386,7 @@ protected function _moveFile($tmpPath, $destPath)
356
386
*/
357
387
protected function chmod ($ file )
358
388
{
389
+ //phpcs:ignore Squiz.PHP.NonExecutableCode.ReturnNotRequired
359
390
return ;
360
391
}
361
392
}
0 commit comments