|
11 | 11 | use Magento\Catalog\Model\Product\Image\ConvertImageMiscParamsToReadableFormat; |
12 | 12 | use Magento\Catalog\Model\Product\Media\ConfigInterface; |
13 | 13 | use Magento\Framework\App\ObjectManager; |
14 | | -use Magento\Framework\Encryption\Encryptor; |
| 14 | +use Magento\Framework\Config\ConfigOptionsListConstants; |
15 | 15 | use Magento\Framework\Encryption\EncryptorInterface; |
16 | 16 | use Magento\Framework\Exception\LocalizedException; |
17 | 17 | use Magento\Framework\View\Asset\ContextInterface; |
@@ -260,29 +260,52 @@ public function getModule() |
260 | 260 | } |
261 | 261 |
|
262 | 262 | /** |
263 | | - * Retrieve part of path based on misc params |
| 263 | + * Generate path from image info. |
264 | 264 | * |
265 | 265 | * @return string |
266 | 266 | */ |
267 | | - private function getMiscPath() |
| 267 | + private function getImageInfo() |
268 | 268 | { |
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)) |
272 | 281 | ); |
273 | | - } |
274 | 282 |
|
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; |
286 | 309 | } |
287 | 310 |
|
288 | 311 | /** |
|
0 commit comments