Skip to content

Commit 640cad5

Browse files
authored
[performance] MC-37459: Support by Magento Catalog (#6223)
* MC-37459: Support by Magento Catalog
1 parent 8953e85 commit 640cad5

File tree

97 files changed

+1044
-1953
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1044
-1953
lines changed

.htaccess

+7-393
Large diffs are not rendered by default.

app/code/Magento/Analytics/Test/Unit/Model/LinkProviderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function setUp(): void
9090

9191
public function testGet()
9292
{
93-
$baseUrl = 'http://magento.local/pub/media/';
93+
$baseUrl = 'http://magento.local/media/';
9494
$fileInfoPath = 'analytics/data.tgz';
9595
$fileInitializationVector = 'er312esq23eqq';
9696
$this->fileInfoManagerMock->expects($this->once())

app/code/Magento/AwsS3/Driver/AwsS3.php

+70-19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use League\Flysystem\Config;
1212
use Magento\Framework\Exception\FileSystemException;
1313
use Magento\Framework\Filesystem\DriverInterface;
14+
use Magento\Framework\Phrase;
1415

1516
/**
1617
* Driver for AWS S3 IO operations.
@@ -232,14 +233,14 @@ public function getRealPathSafety($path)
232233
*/
233234
public function getAbsolutePath($basePath, $path, $scheme = null)
234235
{
236+
$basePath = $this->normalizeRelativePath((string)$basePath);
237+
$path = $this->normalizeRelativePath((string)$path);
235238
if ($basePath && $path && 0 === strpos($path, $basePath)) {
236-
return $this->normalizeAbsolutePath(
237-
$this->normalizeRelativePath($path)
238-
);
239+
return $this->normalizeAbsolutePath($path);
239240
}
240241

241242
if ($basePath && $basePath !== '/') {
242-
return $basePath . ltrim((string)$path, '/');
243+
$path = $basePath . ltrim((string)$path, '/');
243244
}
244245

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

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

331-
return $this->adapter->has($path) && $this->adapter->getMetadata($path)['type'] === self::TYPE_DIR;
332+
if ($this->adapter->has($path) && ($meta = $this->adapter->getMetadata($path))) {
333+
return ($meta['type'] ?? null) === self::TYPE_DIR;
334+
}
335+
return false;
332336
}
333337

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

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

389393
return [
@@ -486,7 +490,16 @@ public function touch($path, $modificationTime = null)
486490
*/
487491
public function fileReadLine($resource, $length, $ending = null): string
488492
{
489-
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
493+
// phpcs:disable
494+
$result = @stream_get_line($resource, $length, $ending);
495+
// phpcs:enable
496+
if (false === $result) {
497+
throw new FileSystemException(
498+
new Phrase('File cannot be read %1', [$this->getWarningMessage()])
499+
);
500+
}
501+
502+
return $result;
490503
}
491504

492505
/**
@@ -521,23 +534,38 @@ public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure
521534
*/
522535
public function fileTell($resource): int
523536
{
524-
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
537+
$result = @ftell($resource);
538+
if ($result === null) {
539+
throw new FileSystemException(
540+
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
541+
);
542+
}
543+
return $result;
525544
}
526545

527546
/**
528547
* @inheritDoc
529548
*/
530549
public function fileSeek($resource, $offset, $whence = SEEK_SET): int
531550
{
532-
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
551+
$result = @fseek($resource, $offset, $whence);
552+
if ($result === -1) {
553+
throw new FileSystemException(
554+
new Phrase(
555+
'An error occurred during "%1" fileSeek execution.',
556+
[$this->getWarningMessage()]
557+
)
558+
);
559+
}
560+
return $result;
533561
}
534562

535563
/**
536564
* @inheritDoc
537565
*/
538566
public function endOfFile($resource): bool
539567
{
540-
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
568+
return feof($resource);
541569
}
542570

543571
/**
@@ -554,23 +582,50 @@ public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure
554582
*/
555583
public function fileFlush($resource): bool
556584
{
557-
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
585+
$result = @fflush($resource);
586+
if (!$result) {
587+
throw new FileSystemException(
588+
new Phrase(
589+
'An error occurred during "%1" fileFlush execution.',
590+
[$this->getWarningMessage()]
591+
)
592+
);
593+
}
594+
return $result;
558595
}
559596

560597
/**
561598
* @inheritDoc
562599
*/
563600
public function fileLock($resource, $lockMode = LOCK_EX): bool
564601
{
565-
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
602+
$result = @flock($resource, $lockMode);
603+
if (!$result) {
604+
throw new FileSystemException(
605+
new Phrase(
606+
'An error occurred during "%1" fileLock execution.',
607+
[$this->getWarningMessage()]
608+
)
609+
);
610+
}
611+
return $result;
566612
}
567613

568614
/**
569615
* @inheritDoc
570616
*/
571617
public function fileUnlock($resource): bool
572618
{
573-
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
619+
$result = @flock($resource, LOCK_UN);
620+
if (!$result) {
621+
throw new FileSystemException(
622+
new Phrase(
623+
'An error occurred during "%1" fileUnlock execution.',
624+
[$this->getWarningMessage()]
625+
)
626+
);
627+
}
628+
return $result;
574629
}
575630

576631
/**
@@ -627,15 +682,11 @@ public function fileOpen($path, $mode)
627682
if (!isset($this->streams[$path])) {
628683
$this->streams[$path] = tmpfile();
629684
if ($this->adapter->has($path)) {
630-
$file = tmpfile();
631685
//phpcs:ignore Magento2.Functions.DiscouragedFunction
632-
fwrite($file, $this->adapter->read($path)['contents']);
686+
fwrite($this->streams[$path], $this->adapter->read($path)['contents']);
633687
//phpcs:ignore Magento2.Functions.DiscouragedFunction
634-
fseek($file, 0);
635-
} else {
636-
$file = tmpfile();
688+
rewind($this->streams[$path]);
637689
}
638-
$this->streams[$path] = $file;
639690
}
640691

641692
return $this->streams[$path];

app/code/Magento/AwsS3/Test/Unit/Driver/AwsS3Test.php

+30
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,46 @@ public function getAbsolutePathDataProvider(): array
105105
self::URL . 'test/test.png',
106106
self::URL . 'test/test.png'
107107
],
108+
[
109+
self::URL,
110+
self::URL . 'media/catalog/test.png',
111+
self::URL . 'media/catalog/test.png'
112+
],
113+
[
114+
'',
115+
self::URL . 'media/catalog/test.png',
116+
self::URL . 'media/catalog/test.png'
117+
],
108118
[
109119
self::URL . 'test/',
110120
'test.txt',
111121
self::URL . 'test/test.txt'
112122
],
123+
[
124+
self::URL . 'media/',
125+
'media/image.jpg',
126+
self::URL . 'media/image.jpg'
127+
],
113128
[
114129
self::URL . 'media/',
115130
'/catalog/test.png',
116131
self::URL . 'media/catalog/test.png'
117132
],
133+
[
134+
self::URL,
135+
'var/import/images',
136+
self::URL . 'var/import/images'
137+
],
138+
[
139+
self::URL . 'export/',
140+
null,
141+
self::URL . 'export/'
142+
],
143+
[
144+
self::URL . 'var/import/images/product_images/',
145+
self::URL . 'var/import/images/product_images/1.png',
146+
self::URL . 'var/import/images/product_images/1.png'
147+
],
118148
[
119149
'',
120150
self::URL . 'media/catalog/test.png',

app/code/Magento/Backup/Model/Fs/Collection.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
namespace Magento\Backup\Model\Fs;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9-
use Magento\Framework\Config\DocumentRoot;
10-
use Magento\Framework\Filesystem\Directory\TargetDirectory;
119

1210
/**
1311
* Backup data collection
@@ -52,20 +50,16 @@ class Collection extends \Magento\Framework\Data\Collection\Filesystem
5250
* @param \Magento\Backup\Helper\Data $backupData
5351
* @param \Magento\Framework\Filesystem $filesystem
5452
* @param \Magento\Backup\Model\Backup $backup
55-
* @param TargetDirectory|null $targetDirectory
56-
* @param DocumentRoot|null $documentRoot
5753
* @throws \Magento\Framework\Exception\FileSystemException
5854
*/
5955
public function __construct(
6056
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
6157
\Magento\Backup\Helper\Data $backupData,
6258
\Magento\Framework\Filesystem $filesystem,
63-
\Magento\Backup\Model\Backup $backup,
64-
TargetDirectory $targetDirectory = null,
65-
DocumentRoot $documentRoot = null
59+
\Magento\Backup\Model\Backup $backup
6660
) {
6761
$this->_backupData = $backupData;
68-
parent::__construct($entityFactory, $targetDirectory, $documentRoot);
62+
parent::__construct($entityFactory, $filesystem);
6963

7064
$this->_filesystem = $filesystem;
7165
$this->_backup = $backup;

app/code/Magento/Captcha/Test/Unit/Helper/DataTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function testGetImgDir()
197197
*/
198198
public function testGetImgUrl()
199199
{
200-
$this->assertEquals($this->helper->getImgUrl(), 'http://localhost/pub/media/captcha/base/');
200+
$this->assertEquals($this->helper->getImgUrl(), 'http://localhost/media/captcha/base/');
201201
}
202202

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

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

228228
return $store;
229229
}

app/code/Magento/Captcha/Test/Unit/Model/DefaultTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function testGetImgSrc()
217217
{
218218
$this->assertEquals(
219219
$this->_object->getImgSrc(),
220-
'http://localhost/pub/media/captcha/base/' . $this->_object->getId() . '.png'
220+
'http://localhost/media/captcha/base/' . $this->_object->getId() . '.png'
221221
);
222222
}
223223

@@ -310,7 +310,7 @@ protected function _getHelperStub()
310310
)->method(
311311
'getImgUrl'
312312
)->willReturn(
313-
'http://localhost/pub/media/captcha/base/'
313+
'http://localhost/media/captcha/base/'
314314
);
315315

316316
return $helper;
@@ -365,7 +365,7 @@ protected function _getStoreStub()
365365
->onlyMethods(['getBaseUrl'])
366366
->disableOriginalConstructor()
367367
->getMock();
368-
$store->expects($this->any())->method('getBaseUrl')->willReturn('http://localhost/pub/media/');
368+
$store->expects($this->any())->method('getBaseUrl')->willReturn('http://localhost/media/');
369369
$store->expects($this->any())->method('isAdmin')->willReturn(false);
370370
return $store;
371371
}

app/code/Magento/Catalog/Helper/Image.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66
namespace Magento\Catalog\Helper;
77

8+
use Magento\Catalog\Model\Config\CatalogMediaConfig;
89
use Magento\Framework\App\Helper\AbstractHelper;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\LocalizedException;
912
use Magento\Framework\View\Element\Block\ArgumentInterface;
1013

1114
/**
@@ -133,27 +136,34 @@ class Image extends AbstractHelper implements ArgumentInterface
133136
*/
134137
private $viewAssetPlaceholderFactory;
135138

139+
/**
140+
* @var CatalogMediaConfig
141+
*/
142+
private $mediaConfig;
143+
136144
/**
137145
* @param \Magento\Framework\App\Helper\Context $context
138146
* @param \Magento\Catalog\Model\Product\ImageFactory $productImageFactory
139147
* @param \Magento\Framework\View\Asset\Repository $assetRepo
140148
* @param \Magento\Framework\View\ConfigInterface $viewConfig
141149
* @param \Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory
150+
* @param CatalogMediaConfig $mediaConfig
142151
*/
143152
public function __construct(
144153
\Magento\Framework\App\Helper\Context $context,
145154
\Magento\Catalog\Model\Product\ImageFactory $productImageFactory,
146155
\Magento\Framework\View\Asset\Repository $assetRepo,
147156
\Magento\Framework\View\ConfigInterface $viewConfig,
148-
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory = null
157+
\Magento\Catalog\Model\View\Asset\PlaceholderFactory $placeholderFactory = null,
158+
CatalogMediaConfig $mediaConfig = null
149159
) {
150160
$this->_productImageFactory = $productImageFactory;
151161
parent::__construct($context);
152162
$this->_assetRepo = $assetRepo;
153163
$this->viewConfig = $viewConfig;
154164
$this->viewAssetPlaceholderFactory = $placeholderFactory
155-
?: \Magento\Framework\App\ObjectManager::getInstance()
156-
->get(\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class);
165+
?: ObjectManager::getInstance()->get(\Magento\Catalog\Model\View\Asset\PlaceholderFactory::class);
166+
$this->mediaConfig = $mediaConfig ?: ObjectManager::getInstance()->get(CatalogMediaConfig::class);
157167
}
158168

159169
/**
@@ -532,7 +542,16 @@ protected function isScheduledActionsAllowed()
532542
public function getUrl()
533543
{
534544
try {
535-
$this->applyScheduledActions();
545+
switch ($this->mediaConfig->getMediaUrlFormat()) {
546+
case CatalogMediaConfig::IMAGE_OPTIMIZATION_PARAMETERS:
547+
$this->initBaseFile();
548+
break;
549+
case CatalogMediaConfig::HASH:
550+
$this->applyScheduledActions();
551+
break;
552+
default:
553+
throw new LocalizedException(__("The specified Catalog media URL format is not supported."));
554+
}
536555
return $this->_getModel()->getUrl();
537556
} catch (\Exception $e) {
538557
return $this->getDefaultPlaceholderUrl();

0 commit comments

Comments
 (0)