Skip to content

Commit 080abd3

Browse files
author
Joan He
committed
Merge remote-tracking branch 'arcticfoxes/MC-13954' into 2.3-qwerty-pr
2 parents d147d80 + b4cef0f commit 080abd3

File tree

2 files changed

+175
-78
lines changed

2 files changed

+175
-78
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Uploader.php

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
namespace Magento\CatalogImportExport\Model\Import;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Filesystem\DriverPool;
1011

1112
/**
1213
* Import entity product model
1314
*
1415
* @api
1516
* @since 100.0.2
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18+
* phpcs:disable Magento2.Functions.DiscouragedFunction
1619
*/
1720
class Uploader extends \Magento\MediaStorage\Model\File\Uploader
1821
{
@@ -31,6 +34,13 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
3134
*/
3235
protected $_tmpDir = '';
3336

37+
/**
38+
* Download directory for url-based resources.
39+
*
40+
* @var string
41+
*/
42+
private $downloadDir;
43+
3444
/**
3545
* Destination directory.
3646
*
@@ -94,6 +104,13 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
94104
*/
95105
protected $_coreFileStorage;
96106

107+
/**
108+
* Instance of random data generator.
109+
*
110+
* @var \Magento\Framework\Math\Random
111+
*/
112+
private $random;
113+
97114
/**
98115
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
99116
* @param \Magento\MediaStorage\Helper\File\Storage $coreFileStorage
@@ -102,6 +119,8 @@ class Uploader extends \Magento\MediaStorage\Model\File\Uploader
102119
* @param \Magento\Framework\Filesystem $filesystem
103120
* @param \Magento\Framework\Filesystem\File\ReadFactory $readFactory
104121
* @param string|null $filePath
122+
* @param \Magento\Framework\Math\Random|null $random
123+
* @throws \Magento\Framework\Exception\FileSystemException
105124
* @throws \Magento\Framework\Exception\LocalizedException
106125
*/
107126
public function __construct(
@@ -111,7 +130,8 @@ public function __construct(
111130
\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator,
112131
\Magento\Framework\Filesystem $filesystem,
113132
\Magento\Framework\Filesystem\File\ReadFactory $readFactory,
114-
$filePath = null
133+
$filePath = null,
134+
\Magento\Framework\Math\Random $random = null
115135
) {
116136
$this->_imageFactory = $imageFactory;
117137
$this->_coreFileStorageDb = $coreFileStorageDb;
@@ -122,6 +142,8 @@ public function __construct(
122142
if ($filePath !== null) {
123143
$this->_setUploadFile($filePath);
124144
}
145+
$this->random = $random ?: ObjectManager::getInstance()->get(\Magento\Framework\Math\Random::class);
146+
$this->downloadDir = DirectoryList::getDefaultConfig()[DirectoryList::TMP][DirectoryList::PATH];
125147
}
126148

127149
/**
@@ -150,52 +172,61 @@ public function init()
150172
*/
151173
public function move($fileName, $renameFileOff = false)
152174
{
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);
162176

163177
if (preg_match('/\bhttps?:\/\//i', $fileName, $matches)) {
164178
$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);
188184
}
189185

190-
$filePath = $this->_directory->getRelativePath($filePath . $fileName);
191-
$this->_setUploadFile($filePath);
186+
$this->_setUploadFile($tmpFilePath);
192187
$destDir = $this->_directory->getAbsolutePath($this->getDestDir());
193188
$result = $this->save($destDir);
194189
unset($result['path']);
195190
$result['name'] = self::getCorrectFileName($result['name']);
191+
196192
return $result;
197193
}
198194

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+
199230
/**
200231
* Prepare information about the file for moving
201232
*
@@ -238,7 +269,7 @@ protected function _readFileInfo($filePath)
238269
* Validate uploaded file by type and etc.
239270
*
240271
* @return void
241-
* @throws \Exception
272+
* @throws \Magento\Framework\Exception\LocalizedException
242273
*/
243274
protected function _validateFile()
244275
{
@@ -251,8 +282,7 @@ protected function _validateFile()
251282

252283
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
253284
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.'));
256286
}
257287
//run validate callbacks
258288
foreach ($this->_validateCallbacks as $params) {
@@ -356,6 +386,7 @@ protected function _moveFile($tmpPath, $destPath)
356386
*/
357387
protected function chmod($file)
358388
{
389+
//phpcs:ignore Squiz.PHP.NonExecutableCode.ReturnNotRequired
359390
return;
360391
}
361392
}

0 commit comments

Comments
 (0)