@@ -286,17 +286,9 @@ private function getImageInfo()
286
286
$ mediaDirectory = $ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA );
287
287
$ data = implode ('_ ' , $ this ->convertToReadableFormat ($ this ->miscParams ));
288
288
289
- $ pathTemplate = $ this ->getModule ()
290
- . DIRECTORY_SEPARATOR . "%s " . DIRECTORY_SEPARATOR
291
- . $ this ->getFilePath ();
292
-
293
289
// New paths are generated without dependency on
294
290
// an encryption key.
295
- $ hashBasedPath = preg_replace (
296
- '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
297
- DIRECTORY_SEPARATOR ,
298
- sprintf ($ pathTemplate , hash (self ::HASH_ALGORITHM , $ data ))
299
- );
291
+ $ hashBasedPath = $ this ->generatePath ($ data );
300
292
301
293
if ($ mediaDirectory ->isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ hashBasedPath )) {
302
294
return $ hashBasedPath ;
@@ -306,19 +298,15 @@ private function getImageInfo()
306
298
// existing encryption key based media gallery cache valid
307
299
// even if an encryption key was changed.
308
300
$ keys = explode ("\n" , $ this ->encryptor ->exportKeys ());
301
+
302
+ if (count ($ keys ) === 1 ) {
303
+ return $ this ->generatePath ($ data , $ this ->decodeKey ($ keys [0 ]));
304
+ }
305
+
309
306
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
- }
307
+ $ key = $ this ->decodeKey ($ key );
316
308
317
- $ keyBasedPath = preg_replace (
318
- '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
319
- DIRECTORY_SEPARATOR ,
320
- sprintf ($ pathTemplate , hash_hmac (self ::HASH_ALGORITHM , $ data , $ key ))
321
- );
309
+ $ keyBasedPath = $ this ->generatePath ($ data , $ key );
322
310
323
311
if ($ mediaDirectory ->isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ keyBasedPath )) {
324
312
return $ keyBasedPath ;
@@ -328,13 +316,54 @@ private function getImageInfo()
328
316
return $ hashBasedPath ;
329
317
}
330
318
319
+ /**
320
+ * Generate path based on data and key, If key is not provided, the path is generated without it
321
+ *
322
+ * @param string $data
323
+ * @param string|null $key
324
+ * @return string
325
+ */
326
+ private function generatePath (string $ data , ?string $ key = null ): string
327
+ {
328
+ $ pathTemplate = $ this ->getModule ()
329
+ . DIRECTORY_SEPARATOR . "%s " . DIRECTORY_SEPARATOR
330
+ . $ this ->getFilePath ();
331
+
332
+ $ hash = $ key ? hash_hmac (self ::HASH_ALGORITHM , $ data , $ key ) : hash (self ::HASH_ALGORITHM , $ data );
333
+
334
+ return preg_replace (
335
+ '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
336
+ DIRECTORY_SEPARATOR ,
337
+ sprintf ($ pathTemplate , $ hash )
338
+ );
339
+ }
340
+
341
+ /**
342
+ * Decode key if it was base64 encoded
343
+ *
344
+ * @param string $key
345
+ *
346
+ * @return string
347
+ */
348
+ private function decodeKey (string $ key ): string
349
+ {
350
+ if (str_starts_with ($ key , ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX )) {
351
+ // phpcs:disable Magento2.Functions.DiscouragedFunction
352
+ return base64_decode (
353
+ substr ($ key , strlen (ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX ))
354
+ );
355
+ }
356
+
357
+ return $ key ;
358
+ }
359
+
331
360
/**
332
361
* Converting bool into a string representation
333
362
*
334
363
* @param array $miscParams
335
364
* @return array
336
365
*/
337
- private function convertToReadableFormat (array $ miscParams )
366
+ private function convertToReadableFormat (array $ miscParams ): array
338
367
{
339
368
return $ this ->convertImageMiscParamsToReadableFormat ->convertImageMiscParamsToReadableFormat ($ miscParams );
340
369
}
0 commit comments