Skip to content

Commit

Permalink
[performance] MC-37459: Support by Magento Catalog (#6223)
Browse files Browse the repository at this point in the history
* MC-37459: Support by Magento Catalog
  • Loading branch information
duhon authored Oct 22, 2020
1 parent 8953e85 commit 640cad5
Show file tree
Hide file tree
Showing 97 changed files with 1,044 additions and 1,953 deletions.
400 changes: 7 additions & 393 deletions .htaccess

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function setUp(): void

public function testGet()
{
$baseUrl = 'http://magento.local/pub/media/';
$baseUrl = 'http://magento.local/media/';
$fileInfoPath = 'analytics/data.tgz';
$fileInitializationVector = 'er312esq23eqq';
$this->fileInfoManagerMock->expects($this->once())
Expand Down
89 changes: 70 additions & 19 deletions app/code/Magento/AwsS3/Driver/AwsS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use League\Flysystem\Config;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Framework\Phrase;

/**
* Driver for AWS S3 IO operations.
Expand Down Expand Up @@ -232,14 +233,14 @@ public function getRealPathSafety($path)
*/
public function getAbsolutePath($basePath, $path, $scheme = null)
{
$basePath = $this->normalizeRelativePath((string)$basePath);
$path = $this->normalizeRelativePath((string)$path);
if ($basePath && $path && 0 === strpos($path, $basePath)) {
return $this->normalizeAbsolutePath(
$this->normalizeRelativePath($path)
);
return $this->normalizeAbsolutePath($path);
}

if ($basePath && $basePath !== '/') {
return $basePath . ltrim((string)$path, '/');
$path = $basePath . ltrim((string)$path, '/');
}

return $this->normalizeAbsolutePath($path);
Expand Down Expand Up @@ -328,7 +329,10 @@ public function isDirectory($path): bool

$path = rtrim($path, '/') . '/';

return $this->adapter->has($path) && $this->adapter->getMetadata($path)['type'] === self::TYPE_DIR;
if ($this->adapter->has($path) && ($meta = $this->adapter->getMetadata($path))) {
return ($meta['type'] ?? null) === self::TYPE_DIR;
}
return false;
}

/**
Expand Down Expand Up @@ -383,7 +387,7 @@ public function stat($path): array
$metaInfo = $this->adapter->getMetadata($path);

if (!$metaInfo) {
throw new FileSystemException(__('Cannot gather stats! %1', (array)$path));
throw new FileSystemException(__('Cannot gather stats! %1', [$this->getWarningMessage()]));
}

return [
Expand Down Expand Up @@ -486,7 +490,16 @@ public function touch($path, $modificationTime = null)
*/
public function fileReadLine($resource, $length, $ending = null): string
{
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
// phpcs:disable
$result = @stream_get_line($resource, $length, $ending);
// phpcs:enable
if (false === $result) {
throw new FileSystemException(
new Phrase('File cannot be read %1', [$this->getWarningMessage()])
);
}

return $result;
}

/**
Expand Down Expand Up @@ -521,23 +534,38 @@ public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure
*/
public function fileTell($resource): int
{
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
$result = @ftell($resource);
if ($result === null) {
throw new FileSystemException(
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
);
}
return $result;
}

/**
* @inheritDoc
*/
public function fileSeek($resource, $offset, $whence = SEEK_SET): int
{
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
$result = @fseek($resource, $offset, $whence);
if ($result === -1) {
throw new FileSystemException(
new Phrase(
'An error occurred during "%1" fileSeek execution.',
[$this->getWarningMessage()]
)
);
}
return $result;
}

/**
* @inheritDoc
*/
public function endOfFile($resource): bool
{
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
return feof($resource);
}

/**
Expand All @@ -554,23 +582,50 @@ public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure
*/
public function fileFlush($resource): bool
{
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
$result = @fflush($resource);
if (!$result) {
throw new FileSystemException(
new Phrase(
'An error occurred during "%1" fileFlush execution.',
[$this->getWarningMessage()]
)
);
}
return $result;
}

/**
* @inheritDoc
*/
public function fileLock($resource, $lockMode = LOCK_EX): bool
{
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
$result = @flock($resource, $lockMode);
if (!$result) {
throw new FileSystemException(
new Phrase(
'An error occurred during "%1" fileLock execution.',
[$this->getWarningMessage()]
)
);
}
return $result;
}

/**
* @inheritDoc
*/
public function fileUnlock($resource): bool
{
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
$result = @flock($resource, LOCK_UN);
if (!$result) {
throw new FileSystemException(
new Phrase(
'An error occurred during "%1" fileUnlock execution.',
[$this->getWarningMessage()]
)
);
}
return $result;
}

/**
Expand Down Expand Up @@ -627,15 +682,11 @@ public function fileOpen($path, $mode)
if (!isset($this->streams[$path])) {
$this->streams[$path] = tmpfile();
if ($this->adapter->has($path)) {
$file = tmpfile();
//phpcs:ignore Magento2.Functions.DiscouragedFunction
fwrite($file, $this->adapter->read($path)['contents']);
fwrite($this->streams[$path], $this->adapter->read($path)['contents']);
//phpcs:ignore Magento2.Functions.DiscouragedFunction
fseek($file, 0);
} else {
$file = tmpfile();
rewind($this->streams[$path]);
}
$this->streams[$path] = $file;
}

return $this->streams[$path];
Expand Down
30 changes: 30 additions & 0 deletions app/code/Magento/AwsS3/Test/Unit/Driver/AwsS3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,46 @@ public function getAbsolutePathDataProvider(): array
self::URL . 'test/test.png',
self::URL . 'test/test.png'
],
[
self::URL,
self::URL . 'media/catalog/test.png',
self::URL . 'media/catalog/test.png'
],
[
'',
self::URL . 'media/catalog/test.png',
self::URL . 'media/catalog/test.png'
],
[
self::URL . 'test/',
'test.txt',
self::URL . 'test/test.txt'
],
[
self::URL . 'media/',
'media/image.jpg',
self::URL . 'media/image.jpg'
],
[
self::URL . 'media/',
'/catalog/test.png',
self::URL . 'media/catalog/test.png'
],
[
self::URL,
'var/import/images',
self::URL . 'var/import/images'
],
[
self::URL . 'export/',
null,
self::URL . 'export/'
],
[
self::URL . 'var/import/images/product_images/',
self::URL . 'var/import/images/product_images/1.png',
self::URL . 'var/import/images/product_images/1.png'
],
[
'',
self::URL . 'media/catalog/test.png',
Expand Down
10 changes: 2 additions & 8 deletions app/code/Magento/Backup/Model/Fs/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
namespace Magento\Backup\Model\Fs;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\DocumentRoot;
use Magento\Framework\Filesystem\Directory\TargetDirectory;

/**
* Backup data collection
Expand Down Expand Up @@ -52,20 +50,16 @@ class Collection extends \Magento\Framework\Data\Collection\Filesystem
* @param \Magento\Backup\Helper\Data $backupData
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Backup\Model\Backup $backup
* @param TargetDirectory|null $targetDirectory
* @param DocumentRoot|null $documentRoot
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
\Magento\Backup\Helper\Data $backupData,
\Magento\Framework\Filesystem $filesystem,
\Magento\Backup\Model\Backup $backup,
TargetDirectory $targetDirectory = null,
DocumentRoot $documentRoot = null
\Magento\Backup\Model\Backup $backup
) {
$this->_backupData = $backupData;
parent::__construct($entityFactory, $targetDirectory, $documentRoot);
parent::__construct($entityFactory, $filesystem);

$this->_filesystem = $filesystem;
$this->_backup = $backup;
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Captcha/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function testGetImgDir()
*/
public function testGetImgUrl()
{
$this->assertEquals($this->helper->getImgUrl(), 'http://localhost/pub/media/captcha/base/');
$this->assertEquals($this->helper->getImgUrl(), 'http://localhost/media/captcha/base/');
}

/**
Expand All @@ -223,7 +223,7 @@ protected function _getStoreStub()
{
$store = $this->createMock(Store::class);

$store->expects($this->any())->method('getBaseUrl')->willReturn('http://localhost/pub/media/');
$store->expects($this->any())->method('getBaseUrl')->willReturn('http://localhost/media/');

return $store;
}
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/Captcha/Test/Unit/Model/DefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function testGetImgSrc()
{
$this->assertEquals(
$this->_object->getImgSrc(),
'http://localhost/pub/media/captcha/base/' . $this->_object->getId() . '.png'
'http://localhost/media/captcha/base/' . $this->_object->getId() . '.png'
);
}

Expand Down Expand Up @@ -310,7 +310,7 @@ protected function _getHelperStub()
)->method(
'getImgUrl'
)->willReturn(
'http://localhost/pub/media/captcha/base/'
'http://localhost/media/captcha/base/'
);

return $helper;
Expand Down Expand Up @@ -365,7 +365,7 @@ protected function _getStoreStub()
->onlyMethods(['getBaseUrl'])
->disableOriginalConstructor()
->getMock();
$store->expects($this->any())->method('getBaseUrl')->willReturn('http://localhost/pub/media/');
$store->expects($this->any())->method('getBaseUrl')->willReturn('http://localhost/media/');
$store->expects($this->any())->method('isAdmin')->willReturn(false);
return $store;
}
Expand Down
27 changes: 23 additions & 4 deletions app/code/Magento/Catalog/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/
namespace Magento\Catalog\Helper;

use Magento\Catalog\Model\Config\CatalogMediaConfig;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\Block\ArgumentInterface;

/**
Expand Down Expand Up @@ -133,27 +136,34 @@ class Image extends AbstractHelper implements ArgumentInterface
*/
private $viewAssetPlaceholderFactory;

/**
* @var CatalogMediaConfig
*/
private $mediaConfig;

/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Catalog\Model\Product\ImageFactory $productImageFactory
* @param \Magento\Framework\View\Asset\Repository $assetRepo
* @param \Magento\Framework\View\ConfigInterface $viewConfig
* @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory
* @param CatalogMediaConfig $mediaConfig
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Catalog\Model\Product\ImageFactory $productImageFactory,
\Magento\Framework\View\Asset\Repository $assetRepo,
\Magento\Framework\View\ConfigInterface $viewConfig,
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory = null
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory = null,
CatalogMediaConfig $mediaConfig = null
) {
$this->_productImageFactory = $productImageFactory;
parent::__construct($context);
$this->_assetRepo = $assetRepo;
$this->viewConfig = $viewConfig;
$this->viewAssetPlaceholderFactory = $placeholderFactory
?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class);
?: ObjectManager::getInstance()->get(\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class);
$this->mediaConfig = $mediaConfig ?: ObjectManager::getInstance()->get(CatalogMediaConfig::class);
}

/**
Expand Down Expand Up @@ -532,7 +542,16 @@ protected function isScheduledActionsAllowed()
public function getUrl()
{
try {
$this->applyScheduledActions();
switch ($this->mediaConfig->getMediaUrlFormat()) {
case CatalogMediaConfig::IMAGE_OPTIMIZATION_PARAMETERS:
$this->initBaseFile();
break;
case CatalogMediaConfig::HASH:
$this->applyScheduledActions();
break;
default:
throw new LocalizedException(__("The specified Catalog media URL format is not supported."));
}
return $this->_getModel()->getUrl();
} catch (\Exception $e) {
return $this->getDefaultPlaceholderUrl();
Expand Down
Loading

0 comments on commit 640cad5

Please sign in to comment.