diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Uploader.php b/app/code/Magento/CatalogImportExport/Model/Import/Uploader.php index a95c5c864089f..24ceeb3cffa35 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Uploader.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Uploader.php @@ -6,6 +6,7 @@ namespace Magento\CatalogImportExport\Model\Import; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Filesystem\DriverPool; /** @@ -15,6 +16,19 @@ */ class Uploader extends \Magento\MediaStorage\Model\File\Uploader { + + /** + * HTTP scheme + * used to compare against the filename and select the proper DriverPool adapter + */ + const HTTP_SCHEME = 'http://'; + + /** + * HTTPS scheme + * used to compare against the filename and select the proper DriverPool adapter + */ + const HTTPS_SCHEME = 'https://'; + /** * Temp directory. * @@ -145,7 +159,13 @@ public function move($fileName, $renameFileOff = false) } if (preg_match('/\bhttps?:\/\//i', $fileName, $matches)) { $url = str_replace($matches[0], '', $fileName); - $read = $this->_readFactory->create($url, DriverPool::HTTP); + + if ($matches[0] === self::HTTP_SCHEME) { + $read = $this->_readFactory->create($url, DriverPool::HTTP); + } else { + $read = $this->_readFactory->create($url, DriverPool::HTTPS); + } + $fileName = preg_replace('/[^a-z0-9\._-]+/i', '', $fileName); $this->_directory->writeFile( $this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName), diff --git a/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/UploaderTest.php b/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/UploaderTest.php index d56044894c96b..ac5d173f91ef5 100644 --- a/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/UploaderTest.php +++ b/app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/UploaderTest.php @@ -152,6 +152,63 @@ public function testMoveFileName() $this->assertEquals(['name' => $fileName], $this->uploader->move($fileName)); } + /** + * @dataProvider moveFileUrlDriverPoolDataProvider + */ + public function testMoveFileUrlDrivePool($fileUrl, $expectedHost, $expectedDriverPool, $expectedScheme) + { + + $driverPool = $this->getMock(\Magento\Framework\Filesystem\DriverPool::class, ['getDriver']); + $driverMock = $this->getMock($expectedDriverPool, ['readAll']); + $driverMock->expects($this->any())->method('isExists')->willReturn(true); + $driverMock->expects($this->any())->method('readAll')->willReturn(null); + $driverPool->expects($this->any())->method('getDriver')->willReturn($driverMock); + + $readFactory = $this->getMockBuilder(\Magento\Framework\Filesystem\File\ReadFactory::class) + ->setConstructorArgs( + [ + $driverPool, + ] + ) + ->setMethods(['create']) + ->getMock(); + + $readFactory->expects($this->any())->method('create') + ->with($expectedHost, $expectedScheme) + ->willReturn($driverMock); + + $uploaderMock = $this->getMockBuilder(\Magento\CatalogImportExport\Model\Import\Uploader::class) + ->setConstructorArgs([ + $this->coreFileStorageDb, + $this->coreFileStorage, + $this->imageFactory, + $this->validator, + $this->filesystem, + $readFactory, + ]) + ->getMock(); + + $uploaderMock->move($fileUrl); + } + + public function moveFileUrlDriverPoolDataProvider() + { + return [ + [ + '$fileUrl' => 'http://test_uploader_file', + '$expectedHost' => 'test_uploader_file', + '$expectedDriverPool' => \Magento\Framework\Filesystem\Driver\Http::class, + '$expectedScheme' => \Magento\Framework\Filesystem\DriverPool::HTTP, + ], + [ + '$fileUrl' => 'https://!:^&`;file', + '$expectedHost' => '!:^&`;file', + '$expectedDriverPool' => \Magento\Framework\Filesystem\Driver\Https::class, + '$expectedScheme' => \Magento\Framework\Filesystem\DriverPool::HTTPS, + ], + ]; + } + public function moveFileUrlDataProvider() { return [