1414use Magento \Framework \Config \ConfigOptionsListConstants ;
1515use Magento \Framework \Encryption \EncryptorInterface ;
1616use Magento \Framework \Exception \LocalizedException ;
17+ use Magento \Framework \Filesystem ;
1718use Magento \Framework \View \Asset \ContextInterface ;
1819use Magento \Framework \View \Asset \LocalInterface ;
1920use Magento \Store \Model \StoreManagerInterface ;
21+ use Magento \Framework \App \Filesystem \DirectoryList ;
2022
2123/**
2224 * A locally available image file asset that can be referred with a file path
2527 */
2628class Image implements LocalInterface
2729{
30+ /**
31+ * Current hashing algorithm
32+ */
33+ private const HASH_ALGORITHM = 'md5 ' ;
34+
2835 /**
2936 * Image type of image (thumbnail,small_image,image,swatch_image,swatch_thumb)
3037 *
@@ -84,6 +91,11 @@ class Image implements LocalInterface
8491 */
8592 private $ convertImageMiscParamsToReadableFormat ;
8693
94+ /**
95+ * @var Filesystem|null
96+ */
97+ private ?Filesystem $ fileSystem ;
98+
8799 /**
88100 * Image constructor.
89101 *
@@ -96,6 +108,9 @@ class Image implements LocalInterface
96108 * @param CatalogMediaConfig $catalogMediaConfig
97109 * @param StoreManagerInterface $storeManager
98110 * @param ConvertImageMiscParamsToReadableFormat $convertImageMiscParamsToReadableFormat
111+ * @param Filesystem|null $fileSystem
112+ *
113+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
99114 */
100115 public function __construct (
101116 ConfigInterface $ mediaConfig ,
@@ -106,7 +121,8 @@ public function __construct(
106121 ImageHelper $ imageHelper = null ,
107122 CatalogMediaConfig $ catalogMediaConfig = null ,
108123 StoreManagerInterface $ storeManager = null ,
109- ?ConvertImageMiscParamsToReadableFormat $ convertImageMiscParamsToReadableFormat = null
124+ ?ConvertImageMiscParamsToReadableFormat $ convertImageMiscParamsToReadableFormat = null ,
125+ ?Filesystem $ fileSystem = null
110126 ) {
111127 if (isset ($ miscParams ['image_type ' ])) {
112128 $ this ->sourceContentType = $ miscParams ['image_type ' ];
@@ -126,6 +142,7 @@ public function __construct(
126142 $ this ->mediaFormatUrl = $ catalogMediaConfig ->getMediaUrlFormat ();
127143 $ this ->convertImageMiscParamsToReadableFormat = $ convertImageMiscParamsToReadableFormat ?:
128144 ObjectManager::getInstance ()->get (ConvertImageMiscParamsToReadableFormat::class);
145+ $ this ->fileSystem = $ fileSystem ?: ObjectManager::getInstance ()->get (Filesystem::class);
129146 }
130147
131148 /**
@@ -266,6 +283,7 @@ public function getModule()
266283 */
267284 private function getImageInfo ()
268285 {
286+ $ mediaDirectory = $ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA );
269287 $ data = implode ('_ ' , $ this ->convertToReadableFormat ($ this ->miscParams ));
270288
271289 $ pathTemplate = $ this ->getModule ()
@@ -277,18 +295,20 @@ private function getImageInfo()
277295 $ hashBasedPath = preg_replace (
278296 '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
279297 DIRECTORY_SEPARATOR ,
280- sprintf ($ pathTemplate , hash (' md5 ' , $ data ))
298+ sprintf ($ pathTemplate , hash (self :: HASH_ALGORITHM , $ data ))
281299 );
282300
283- if (is_readable ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ hashBasedPath )) {
301+ if ($ mediaDirectory -> isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ hashBasedPath )) {
284302 return $ hashBasedPath ;
285303 }
286304
287305 // This loop is intended to preserve backward compatibility and keep
288306 // existing encryption key based media gallery cache valid
289307 // even if an encryption key was changed.
290- foreach (preg_split ('/\s+/s ' , $ this ->encryptor ->exportKeys ()) as $ key ) {
308+ $ keys = explode ("\n" , $ this ->encryptor ->exportKeys ());
309+ foreach ($ keys as $ key ) {
291310 if (str_starts_with ($ key , ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX )) {
311+ // phpcs:disable Magento2.Functions.DiscouragedFunction
292312 $ key = base64_decode (
293313 substr ($ key , strlen (ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX ))
294314 );
@@ -297,10 +317,10 @@ private function getImageInfo()
297317 $ keyBasedPath = preg_replace (
298318 '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
299319 DIRECTORY_SEPARATOR ,
300- sprintf ($ pathTemplate , hash_hmac (" md5 " , $ data , $ key ))
320+ sprintf ($ pathTemplate , hash_hmac (self :: HASH_ALGORITHM , $ data , $ key ))
301321 );
302322
303- if (is_readable ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ keyBasedPath )) {
323+ if ($ mediaDirectory -> isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ keyBasedPath )) {
304324 return $ keyBasedPath ;
305325 }
306326 }
0 commit comments