Skip to content

Commit cf17853

Browse files
committed
ACP2E-3927: Dynamic image generation generate large number of images
1 parent 536edc2 commit cf17853

File tree

4 files changed

+95
-15
lines changed

4 files changed

+95
-15
lines changed

app/code/Magento/MediaStorage/App/Media.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\MediaStorage\App;
@@ -24,9 +24,9 @@
2424
use Magento\MediaStorage\Model\File\Storage\Config;
2525
use Magento\MediaStorage\Model\File\Storage\ConfigFactory;
2626
use Magento\MediaStorage\Model\File\Storage\Response;
27-
use Magento\MediaStorage\Model\File\Storage\Synchronization;
2827
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
2928
use Magento\MediaStorage\Service\ImageResize;
29+
use Magento\Store\Model\StoreManagerInterface;
3030

3131
/**
3232
* The class resize original images
@@ -106,6 +106,11 @@ class Media implements AppInterface
106106
*/
107107
private $mediaUrlFormat;
108108

109+
/**
110+
* @var StoreManagerInterface
111+
*/
112+
private $storeManager;
113+
109114
/**
110115
* @param ConfigFactory $configFactory
111116
* @param SynchronizationFactory $syncFactory
@@ -120,6 +125,7 @@ class Media implements AppInterface
120125
* @param ImageResize $imageResize
121126
* @param File $file
122127
* @param CatalogMediaConfig $catalogMediaConfig
128+
* @param StoreManagerInterface|null $storeManager
123129
* @throws \Magento\Framework\Exception\FileSystemException
124130
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
125131
*/
@@ -136,7 +142,8 @@ public function __construct(
136142
State $state,
137143
ImageResize $imageResize,
138144
File $file,
139-
?CatalogMediaConfig $catalogMediaConfig = null
145+
?CatalogMediaConfig $catalogMediaConfig = null,
146+
?StoreManagerInterface $storeManager = null,
140147
) {
141148
$this->response = $response;
142149
$this->isAllowed = $isAllowed;
@@ -163,6 +170,7 @@ public function __construct(
163170

164171
$catalogMediaConfig = $catalogMediaConfig ?: App\ObjectManager::getInstance()->get(CatalogMediaConfig::class);
165172
$this->mediaUrlFormat = $catalogMediaConfig->getMediaUrlFormat();
173+
$this->storeManager = $storeManager ?? App\ObjectManager::getInstance()->get(StoreManagerInterface::class);
166174
}
167175

168176
/**
@@ -220,7 +228,10 @@ private function createLocalCopy(): void
220228
}
221229

222230
if ($this->mediaUrlFormat === CatalogMediaConfig::HASH) {
223-
$this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName));
231+
$this->imageResize->resizeFromImageName(
232+
$this->getOriginalImage($this->relativeFileName),
233+
[(int) $this->storeManager->getStore()->getWebsiteId()]
234+
);
224235
if (!$this->directoryPub->isReadable($this->relativeFileName)) {
225236
$synchronizer->synchronize($this->relativeFileName);
226237
}

app/code/Magento/MediaStorage/Service/ImageResize.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ public function __construct(
151151
* Create resized images of different sizes from an original image.
152152
*
153153
* @param string $originalImageName
154+
* @param array $websiteIds
154155
* @throws NotFoundException
155156
*/
156-
public function resizeFromImageName(string $originalImageName)
157+
public function resizeFromImageName(string $originalImageName, array $websiteIds = [])
157158
{
158159
$mediastoragefilename = $this->imageConfig->getMediaPath($originalImageName);
159160
$originalImagePath = $this->mediaDirectory->getAbsolutePath($mediastoragefilename);
@@ -167,7 +168,16 @@ public function resizeFromImageName(string $originalImageName)
167168
if (!$this->mediaDirectory->isFile($originalImagePath)) {
168169
throw new NotFoundException(__('Cannot resize image "%1" - original image not found', $originalImagePath));
169170
}
170-
foreach ($this->getViewImages($this->getThemesInUse()) as $viewImage) {
171+
172+
$viewImages = $this->getViewImages($this->getThemesInUse());
173+
if ($websiteIds) {
174+
$viewImages = array_filter(
175+
$viewImages,
176+
fn (string $index) => array_intersect($websiteIds, $this->paramsWebsitesMap[$index]),
177+
ARRAY_FILTER_USE_KEY
178+
);
179+
}
180+
foreach ($viewImages as $viewImage) {
171181
$this->resize($viewImage, $originalImagePath, $originalImageName);
172182
}
173183
}

app/code/Magento/MediaStorage/Test/Unit/App/MediaTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2013 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

8-
98
namespace Magento\MediaStorage\Test\Unit\App;
109

1110
use Exception;
@@ -27,6 +26,7 @@
2726
use Magento\MediaStorage\Model\File\Storage\Synchronization;
2827
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
2928
use Magento\MediaStorage\Service\ImageResize;
29+
use Magento\Store\Model\StoreManagerInterface;
3030
use PHPUnit\Framework\MockObject\MockObject;
3131
use PHPUnit\Framework\TestCase;
3232

@@ -81,6 +81,11 @@ class MediaTest extends TestCase
8181
*/
8282
private $directoryMediaMock;
8383

84+
/**
85+
* @var StoreManagerInterface|MockObject
86+
*/
87+
private $storeManagerMock;
88+
8489
/**
8590
* @var Read|MockObject
8691
*/
@@ -93,12 +98,13 @@ protected function setUp(): void
9398
{
9499
$this->configMock = $this->createMock(Config::class);
95100
$this->sync = $this->createMock(Synchronization::class);
96-
$this->configFactoryMock = $this->createPartialMock(ConfigFactory::class, ['create']);
101+
$this->configFactoryMock = $this->createMock(ConfigFactory::class);
97102
$this->responseMock = $this->createMock(Response::class);
98-
$this->syncFactoryMock = $this->createPartialMock(SynchronizationFactory::class, ['create']);
103+
$this->syncFactoryMock = $this->createMock(SynchronizationFactory::class);
99104
$this->filesystemMock = $this->createMock(Filesystem::class);
100-
$this->directoryPubMock = $this->getMockForAbstractClass(WriteInterface::class);
101-
$this->directoryMediaMock = $this->getMockForAbstractClass(WriteInterface::class);
105+
$this->directoryPubMock = $this->createMock(WriteInterface::class);
106+
$this->directoryMediaMock = $this->createMock(WriteInterface::class);
107+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
102108

103109
$this->configFactoryMock->method('create')
104110
->willReturn($this->configMock);
@@ -288,7 +294,8 @@ protected function createMediaModel(bool $isAllowed = true): Media
288294
$this->createMock(State::class),
289295
$this->createMock(ImageResize::class),
290296
$driverFile,
291-
$this->createMock(CatalogMediaConfig::class)
297+
$this->createMock(CatalogMediaConfig::class),
298+
$this->storeManagerMock,
292299
);
293300
}
294301
}

app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,58 @@ public function testResizeFromImageNameMediaStorageDatabase()
444444
$this->service->resizeFromImageName($this->testfilename);
445445
}
446446

447+
public function testResizeFromImageNameWithAssignedWebsite()
448+
{
449+
$this->databaseMock->expects($this->atLeastOnce())
450+
->method('checkDbUsage')
451+
->willReturn(false);
452+
$this->mediaDirectoryMock->expects($this->exactly(2))
453+
->method('isFile')
454+
->with($this->testfilepath)
455+
->willReturnOnConsecutiveCalls(true, false);
456+
$this->themeCollectionMock->expects($this->once())
457+
->method('loadRegisteredThemes')
458+
->willReturn(
459+
[ new DataObject(['id' => '0']) ]
460+
);
461+
$this->themeCustomizationConfigMock->expects($this->once())
462+
->method('getStoresByThemes')
463+
->willReturn(
464+
['0' => []]
465+
);
466+
$imageMock = $this->createMock(Image::class);
467+
$this->imageFactoryMock->expects($this->once())
468+
->method('create')
469+
->willReturn($imageMock);
470+
471+
$this->service->resizeFromImageName($this->testfilename, [2]);
472+
}
473+
474+
public function testResizeFromImageNameWithNotAssignedWebsite()
475+
{
476+
$this->databaseMock->expects($this->atLeastOnce())
477+
->method('checkDbUsage')
478+
->willReturn(false);
479+
$this->mediaDirectoryMock->expects($this->once())
480+
->method('isFile')
481+
->with($this->testfilepath)
482+
->willReturn(true);
483+
$this->themeCollectionMock->expects($this->once())
484+
->method('loadRegisteredThemes')
485+
->willReturn(
486+
[ new DataObject(['id' => '0']) ]
487+
);
488+
$this->themeCustomizationConfigMock->expects($this->once())
489+
->method('getStoresByThemes')
490+
->willReturn(
491+
['0' => []]
492+
);
493+
$this->imageFactoryMock->expects($this->never())
494+
->method('create');
495+
496+
$this->service->resizeFromImageName($this->testfilename, [3]);
497+
}
498+
447499
public function testSkipResizingAlreadyResizedImageOnDisk()
448500
{
449501
$this->databaseMock->expects($this->atLeastOnce())

0 commit comments

Comments
 (0)