Skip to content

Commit 6600516

Browse files
AC-12682: Detach image cache generation from encryption key
1 parent 29cd372 commit 6600516

File tree

1 file changed

+41
-18
lines changed
  • app/code/Magento/Catalog/Model/View/Asset

1 file changed

+41
-18
lines changed

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

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
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\Encryption\Encryptor;
14+
use Magento\Framework\Config\ConfigOptionsListConstants;
1515
use Magento\Framework\Encryption\EncryptorInterface;
1616
use Magento\Framework\Exception\LocalizedException;
1717
use Magento\Framework\View\Asset\ContextInterface;
@@ -260,29 +260,52 @@ public function getModule()
260260
}
261261

262262
/**
263-
* Retrieve part of path based on misc params
263+
* Generate path from image info.
264264
*
265265
* @return string
266266
*/
267-
private function getMiscPath()
267+
private function getImageInfo()
268268
{
269-
return $this->encryptor->hash(
270-
implode('_', $this->convertToReadableFormat($this->miscParams)),
271-
Encryptor::HASH_VERSION_MD5
269+
$data = implode('_', $this->convertToReadableFormat($this->miscParams));
270+
271+
$pathTemplate = $this->getModule()
272+
. DIRECTORY_SEPARATOR . "%s" . DIRECTORY_SEPARATOR
273+
. $this->getFilePath();
274+
275+
// New paths are generated without dependency on
276+
// an encryption key.
277+
$hashBasedPath = preg_replace(
278+
'|\Q' . DIRECTORY_SEPARATOR . '\E+|',
279+
DIRECTORY_SEPARATOR,
280+
sprintf($pathTemplate, hash('md5', $data))
272281
);
273-
}
274282

275-
/**
276-
* Generate path from image info
277-
*
278-
* @return string
279-
*/
280-
private function getImageInfo()
281-
{
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);
283+
if (is_readable($this->context->getPath() . DIRECTORY_SEPARATOR . $hashBasedPath)) {
284+
return $hashBasedPath;
285+
}
286+
287+
// This loop is intended to preserve backward compatibility and keep
288+
// existing encryption key based media gallery cache valid
289+
// even if an encryption key was changed.
290+
foreach (preg_split('/\s+/s', $this->encryptor->exportKeys()) as $key) {
291+
if (str_starts_with($key, ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX)) {
292+
$key = base64_decode(
293+
substr($key, strlen(ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX))
294+
);
295+
}
296+
297+
$keyBasedPath = preg_replace(
298+
'|\Q' . DIRECTORY_SEPARATOR . '\E+|',
299+
DIRECTORY_SEPARATOR,
300+
sprintf($pathTemplate, hash_hmac("md5", $data, $key))
301+
);
302+
303+
if (is_readable($this->context->getPath() . DIRECTORY_SEPARATOR . $keyBasedPath)) {
304+
return $keyBasedPath;
305+
}
306+
}
307+
308+
return $hashBasedPath;
286309
}
287310

288311
/**

0 commit comments

Comments
 (0)