Skip to content

Commit b2b7b32

Browse files
Merge pull request #9308 from magento-cia/AC-12682
AC-12682: Detach image cache generation from encryption key
2 parents 082d981 + 823180c commit b2b7b32

File tree

3 files changed

+72
-36
lines changed

3 files changed

+72
-36
lines changed

app/code/Magento/Catalog/Model/Product/Image/RemoveDeletedImagesFromCache.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
<?php
2-
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
2+
/************************************************************************
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained
13+
* from Adobe.
14+
* ***********************************************************************
515
*/
16+
617
declare(strict_types=1);
718

819
namespace Magento\Catalog\Model\Product\Image;
920

1021
use Magento\Catalog\Helper\Image;
1122
use Magento\Catalog\Model\Product\Media\Config;
1223
use Magento\Framework\App\Filesystem\DirectoryList;
13-
use Magento\Framework\Encryption\Encryptor;
1424
use Magento\Framework\Encryption\EncryptorInterface;
1525
use Magento\Framework\Filesystem;
1626
use Magento\Framework\Filesystem\Directory\WriteInterface;
@@ -21,6 +31,11 @@
2131
*/
2232
class RemoveDeletedImagesFromCache
2333
{
34+
/**
35+
* Current hashing algorithm
36+
*/
37+
private const HASH_ALGORITHM = 'md5';
38+
2439
/**
2540
* @var ConfigInterface
2641
*/
@@ -103,10 +118,10 @@ public function removeDeletedImagesFromCache(array $files): void
103118
unset($imageMiscParams['image_type']);
104119
}
105120

106-
$cacheId = $this->encryptor->hash(
121+
$cacheId = hash(
122+
self::HASH_ALGORITHM,
107123
implode('_', $this->convertImageMiscParamsToReadableFormat
108-
->convertImageMiscParamsToReadableFormat($imageMiscParams)),
109-
Encryptor::HASH_VERSION_MD5
124+
->convertImageMiscParamsToReadableFormat($imageMiscParams))
110125
);
111126

112127
foreach ($files as $filePath) {

app/code/Magento/Catalog/Model/View/Asset/Image.php

+36-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
<?php
2-
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
2+
/************************************************************************
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained
13+
* from Adobe.
14+
* ***********************************************************************
515
*/
616

717
namespace Magento\Catalog\Model\View\Asset;
@@ -11,7 +21,6 @@
1121
use Magento\Catalog\Model\Product\Image\ConvertImageMiscParamsToReadableFormat;
1222
use Magento\Catalog\Model\Product\Media\ConfigInterface;
1323
use Magento\Framework\App\ObjectManager;
14-
use Magento\Framework\Encryption\Encryptor;
1524
use Magento\Framework\Encryption\EncryptorInterface;
1625
use Magento\Framework\Exception\LocalizedException;
1726
use Magento\Framework\View\Asset\ContextInterface;
@@ -25,6 +34,11 @@
2534
*/
2635
class Image implements LocalInterface
2736
{
37+
/**
38+
* Current hashing algorithm
39+
*/
40+
private const HASH_ALGORITHM = 'md5';
41+
2842
/**
2943
* Image type of image (thumbnail,small_image,image,swatch_image,swatch_thumb)
3044
*
@@ -96,6 +110,8 @@ class Image implements LocalInterface
96110
* @param CatalogMediaConfig $catalogMediaConfig
97111
* @param StoreManagerInterface $storeManager
98112
* @param ConvertImageMiscParamsToReadableFormat $convertImageMiscParamsToReadableFormat
113+
*
114+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
99115
*/
100116
public function __construct(
101117
ConfigInterface $mediaConfig,
@@ -260,29 +276,27 @@ public function getModule()
260276
}
261277

262278
/**
263-
* Retrieve part of path based on misc params
264-
*
265-
* @return string
266-
*/
267-
private function getMiscPath()
268-
{
269-
return $this->encryptor->hash(
270-
implode('_', $this->convertToReadableFormat($this->miscParams)),
271-
Encryptor::HASH_VERSION_MD5
272-
);
273-
}
274-
275-
/**
276-
* Generate path from image info
279+
* Generate path from image info.
277280
*
278281
* @return string
279282
*/
280283
private function getImageInfo()
281284
{
282-
$path = $this->getModule()
283-
. DIRECTORY_SEPARATOR . $this->getMiscPath()
284-
. DIRECTORY_SEPARATOR . $this->getFilePath();
285-
return preg_replace('|\Q'. DIRECTORY_SEPARATOR . '\E+|', DIRECTORY_SEPARATOR, $path);
285+
$data = implode('_', $this->convertToReadableFormat($this->miscParams));
286+
287+
$pathTemplate = $this->getModule()
288+
. DIRECTORY_SEPARATOR . "%s" . DIRECTORY_SEPARATOR
289+
. $this->getFilePath();
290+
291+
/**
292+
* New paths are generated without dependency on
293+
* an encryption key.
294+
*/
295+
return preg_replace(
296+
'|\Q' . DIRECTORY_SEPARATOR . '\E+|',
297+
DIRECTORY_SEPARATOR,
298+
sprintf($pathTemplate, hash(self::HASH_ALGORITHM, $data))
299+
);
286300
}
287301

288302
/**

app/code/Magento/Catalog/Test/Unit/Model/Product/Image/RemoveDeletedImagesFromCacheTest.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
<?php
2-
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
2+
/************************************************************************
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained
13+
* from Adobe.
14+
* ***********************************************************************
515
*/
16+
617
declare(strict_types=1);
718

819
namespace Magento\Catalog\Test\Unit\Model\Product\Image;
@@ -171,10 +182,6 @@ public function getRespectiveMethodMockObjForRemoveDeletedImagesFromCache(array
171182
->method('convertImageMiscParamsToReadableFormat')
172183
->willReturn($data['convertImageParamsToReadableFormat']);
173184

174-
$this->encryptor->expects($this->once())
175-
->method('hash')
176-
->willReturn('85b0304775df23c13f08dd2c1f9c4c28');
177-
178185
$this->mediaConfig->expects($this->once())
179186
->method('getBaseMediaPath')
180187
->willReturn('catalog/product');

0 commit comments

Comments
 (0)