@@ -286,17 +286,9 @@ private function getImageInfo()
286286 $ mediaDirectory = $ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA );
287287 $ data = implode ('_ ' , $ this ->convertToReadableFormat ($ this ->miscParams ));
288288
289- $ pathTemplate = $ this ->getModule ()
290- . DIRECTORY_SEPARATOR . "%s " . DIRECTORY_SEPARATOR
291- . $ this ->getFilePath ();
292-
293289 // New paths are generated without dependency on
294290 // 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 );
300292
301293 if ($ mediaDirectory ->isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ hashBasedPath )) {
302294 return $ hashBasedPath ;
@@ -306,19 +298,15 @@ private function getImageInfo()
306298 // existing encryption key based media gallery cache valid
307299 // even if an encryption key was changed.
308300 $ keys = explode ("\n" , $ this ->encryptor ->exportKeys ());
301+
302+ if (count ($ keys ) === 1 ) {
303+ return $ this ->generatePath ($ data , $ this ->decodeKey ($ keys [0 ]));
304+ }
305+
309306 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 );
316308
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 );
322310
323311 if ($ mediaDirectory ->isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ keyBasedPath )) {
324312 return $ keyBasedPath ;
@@ -328,13 +316,54 @@ private function getImageInfo()
328316 return $ hashBasedPath ;
329317 }
330318
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+
331360 /**
332361 * Converting bool into a string representation
333362 *
334363 * @param array $miscParams
335364 * @return array
336365 */
337- private function convertToReadableFormat (array $ miscParams )
366+ private function convertToReadableFormat (array $ miscParams ): array
338367 {
339368 return $ this ->convertImageMiscParamsToReadableFormat ->convertImageMiscParamsToReadableFormat ($ miscParams );
340369 }
0 commit comments