@@ -286,9 +286,17 @@ 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+
289293 // New paths are generated without dependency on
290294 // an encryption key.
291- $ hashBasedPath = $ this ->generatePath ($ data );
295+ $ hashBasedPath = preg_replace (
296+ '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
297+ DIRECTORY_SEPARATOR ,
298+ sprintf ($ pathTemplate , hash (self ::HASH_ALGORITHM , $ data ))
299+ );
292300
293301 if ($ mediaDirectory ->isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ hashBasedPath )) {
294302 return $ hashBasedPath ;
@@ -298,15 +306,19 @@ private function getImageInfo()
298306 // existing encryption key based media gallery cache valid
299307 // even if an encryption key was changed.
300308 $ keys = explode ("\n" , $ this ->encryptor ->exportKeys ());
301-
302- if (count ($ keys ) === 1 ) {
303- return $ this ->generatePath ($ data , $ this ->decodeKey ($ keys [0 ]));
304- }
305-
306309 foreach ($ keys as $ key ) {
307- $ key = $ this ->decodeKey ($ 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+ }
308316
309- $ keyBasedPath = $ this ->generatePath ($ data , $ key );
317+ $ keyBasedPath = preg_replace (
318+ '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
319+ DIRECTORY_SEPARATOR ,
320+ sprintf ($ pathTemplate , hash_hmac (self ::HASH_ALGORITHM , $ data , $ key ))
321+ );
310322
311323 if ($ mediaDirectory ->isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ keyBasedPath )) {
312324 return $ keyBasedPath ;
@@ -316,54 +328,13 @@ private function getImageInfo()
316328 return $ hashBasedPath ;
317329 }
318330
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-
360331 /**
361332 * Converting bool into a string representation
362333 *
363334 * @param array $miscParams
364335 * @return array
365336 */
366- private function convertToReadableFormat (array $ miscParams ): array
337+ private function convertToReadableFormat (array $ miscParams )
367338 {
368339 return $ this ->convertImageMiscParamsToReadableFormat ->convertImageMiscParamsToReadableFormat ($ miscParams );
369340 }
0 commit comments