Skip to content

Commit a1d87e3

Browse files
AC-12682: Detach image cache generation from encryption key
1 parent 31c4908 commit a1d87e3

File tree

2 files changed

+14
-48
lines changed

2 files changed

+14
-48
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
*/
2222
class RemoveDeletedImagesFromCache
2323
{
24+
/**
25+
* Current hashing algorithm
26+
*/
27+
private const HASH_ALGORITHM = 'md5';
28+
2429
/**
2530
* @var ConfigInterface
2631
*/
@@ -103,10 +108,10 @@ public function removeDeletedImagesFromCache(array $files): void
103108
unset($imageMiscParams['image_type']);
104109
}
105110

106-
$cacheId = $this->encryptor->hash(
111+
$cacheId = hash(
112+
self::HASH_ALGORITHM,
107113
implode('_', $this->convertImageMiscParamsToReadableFormat
108-
->convertImageMiscParamsToReadableFormat($imageMiscParams)),
109-
Encryptor::HASH_VERSION_MD5
114+
->convertImageMiscParamsToReadableFormat($imageMiscParams))
110115
);
111116

112117
foreach ($files as $filePath) {

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

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111
use Magento\Catalog\Model\Product\Image\ConvertImageMiscParamsToReadableFormat;
1212
use Magento\Catalog\Model\Product\Media\ConfigInterface;
1313
use Magento\Framework\App\ObjectManager;
14-
use Magento\Framework\Config\ConfigOptionsListConstants;
1514
use Magento\Framework\Encryption\EncryptorInterface;
1615
use Magento\Framework\Exception\LocalizedException;
17-
use Magento\Framework\Filesystem;
1816
use Magento\Framework\View\Asset\ContextInterface;
1917
use Magento\Framework\View\Asset\LocalInterface;
2018
use Magento\Store\Model\StoreManagerInterface;
21-
use Magento\Framework\App\Filesystem\DirectoryList;
2219

2320
/**
2421
* A locally available image file asset that can be referred with a file path
@@ -91,11 +88,6 @@ class Image implements LocalInterface
9188
*/
9289
private $convertImageMiscParamsToReadableFormat;
9390

94-
/**
95-
* @var Filesystem|null
96-
*/
97-
private ?Filesystem $fileSystem;
98-
9991
/**
10092
* Image constructor.
10193
*
@@ -108,7 +100,6 @@ class Image implements LocalInterface
108100
* @param CatalogMediaConfig $catalogMediaConfig
109101
* @param StoreManagerInterface $storeManager
110102
* @param ConvertImageMiscParamsToReadableFormat $convertImageMiscParamsToReadableFormat
111-
* @param Filesystem|null $fileSystem
112103
*
113104
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
114105
*/
@@ -121,8 +112,7 @@ public function __construct(
121112
ImageHelper $imageHelper = null,
122113
CatalogMediaConfig $catalogMediaConfig = null,
123114
StoreManagerInterface $storeManager = null,
124-
?ConvertImageMiscParamsToReadableFormat $convertImageMiscParamsToReadableFormat = null,
125-
?Filesystem $fileSystem = null
115+
?ConvertImageMiscParamsToReadableFormat $convertImageMiscParamsToReadableFormat = null
126116
) {
127117
if (isset($miscParams['image_type'])) {
128118
$this->sourceContentType = $miscParams['image_type'];
@@ -142,7 +132,6 @@ public function __construct(
142132
$this->mediaFormatUrl = $catalogMediaConfig->getMediaUrlFormat();
143133
$this->convertImageMiscParamsToReadableFormat = $convertImageMiscParamsToReadableFormat ?:
144134
ObjectManager::getInstance()->get(ConvertImageMiscParamsToReadableFormat::class);
145-
$this->fileSystem = $fileSystem ?: ObjectManager::getInstance()->get(Filesystem::class);
146135
}
147136

148137
/**
@@ -283,49 +272,21 @@ public function getModule()
283272
*/
284273
private function getImageInfo()
285274
{
286-
$mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
287275
$data = implode('_', $this->convertToReadableFormat($this->miscParams));
288276

289277
$pathTemplate = $this->getModule()
290278
. DIRECTORY_SEPARATOR . "%s" . DIRECTORY_SEPARATOR
291279
. $this->getFilePath();
292280

293-
// New paths are generated without dependency on
294-
// an encryption key.
295-
$hashBasedPath = preg_replace(
281+
/**
282+
* New paths are generated without dependency on
283+
* an encryption key.
284+
*/
285+
return preg_replace(
296286
'|\Q' . DIRECTORY_SEPARATOR . '\E+|',
297287
DIRECTORY_SEPARATOR,
298288
sprintf($pathTemplate, hash(self::HASH_ALGORITHM, $data))
299289
);
300-
301-
if ($mediaDirectory->isExist($this->context->getPath() . DIRECTORY_SEPARATOR . $hashBasedPath)) {
302-
return $hashBasedPath;
303-
}
304-
305-
// This loop is intended to preserve backward compatibility and keep
306-
// existing encryption key based media gallery cache valid
307-
// even if an encryption key was changed.
308-
$keys = explode("\n", $this->encryptor->exportKeys());
309-
foreach ($keys as $key) {
310-
if (str_starts_with($key, ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX)) {
311-
// phpcs:disable Magento2.Functions.DiscouragedFunction
312-
$key = base64_decode(
313-
substr($key, strlen(ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX))
314-
);
315-
}
316-
317-
$keyBasedPath = preg_replace(
318-
'|\Q' . DIRECTORY_SEPARATOR . '\E+|',
319-
DIRECTORY_SEPARATOR,
320-
sprintf($pathTemplate, hash_hmac(self::HASH_ALGORITHM, $data, $key))
321-
);
322-
323-
if ($mediaDirectory->isExist($this->context->getPath() . DIRECTORY_SEPARATOR . $keyBasedPath)) {
324-
return $keyBasedPath;
325-
}
326-
}
327-
328-
return $hashBasedPath;
329290
}
330291

331292
/**

0 commit comments

Comments
 (0)