14
14
use Magento \Framework \Config \ConfigOptionsListConstants ;
15
15
use Magento \Framework \Encryption \EncryptorInterface ;
16
16
use Magento \Framework \Exception \LocalizedException ;
17
+ use Magento \Framework \Filesystem ;
17
18
use Magento \Framework \View \Asset \ContextInterface ;
18
19
use Magento \Framework \View \Asset \LocalInterface ;
19
20
use Magento \Store \Model \StoreManagerInterface ;
21
+ use Magento \Framework \App \Filesystem \DirectoryList ;
20
22
21
23
/**
22
24
* A locally available image file asset that can be referred with a file path
25
27
*/
26
28
class Image implements LocalInterface
27
29
{
30
+ /**
31
+ * Current hashing algorithm
32
+ */
33
+ private const HASH_ALGORITHM = 'md5 ' ;
34
+
28
35
/**
29
36
* Image type of image (thumbnail,small_image,image,swatch_image,swatch_thumb)
30
37
*
@@ -84,6 +91,11 @@ class Image implements LocalInterface
84
91
*/
85
92
private $ convertImageMiscParamsToReadableFormat ;
86
93
94
+ /**
95
+ * @var Filesystem|null
96
+ */
97
+ private ?Filesystem $ fileSystem ;
98
+
87
99
/**
88
100
* Image constructor.
89
101
*
@@ -96,6 +108,9 @@ class Image implements LocalInterface
96
108
* @param CatalogMediaConfig $catalogMediaConfig
97
109
* @param StoreManagerInterface $storeManager
98
110
* @param ConvertImageMiscParamsToReadableFormat $convertImageMiscParamsToReadableFormat
111
+ * @param Filesystem|null $fileSystem
112
+ *
113
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
99
114
*/
100
115
public function __construct (
101
116
ConfigInterface $ mediaConfig ,
@@ -106,7 +121,8 @@ public function __construct(
106
121
ImageHelper $ imageHelper = null ,
107
122
CatalogMediaConfig $ catalogMediaConfig = null ,
108
123
StoreManagerInterface $ storeManager = null ,
109
- ?ConvertImageMiscParamsToReadableFormat $ convertImageMiscParamsToReadableFormat = null
124
+ ?ConvertImageMiscParamsToReadableFormat $ convertImageMiscParamsToReadableFormat = null ,
125
+ ?Filesystem $ fileSystem = null
110
126
) {
111
127
if (isset ($ miscParams ['image_type ' ])) {
112
128
$ this ->sourceContentType = $ miscParams ['image_type ' ];
@@ -126,6 +142,7 @@ public function __construct(
126
142
$ this ->mediaFormatUrl = $ catalogMediaConfig ->getMediaUrlFormat ();
127
143
$ this ->convertImageMiscParamsToReadableFormat = $ convertImageMiscParamsToReadableFormat ?:
128
144
ObjectManager::getInstance ()->get (ConvertImageMiscParamsToReadableFormat::class);
145
+ $ this ->fileSystem = $ fileSystem ?: ObjectManager::getInstance ()->get (Filesystem::class);
129
146
}
130
147
131
148
/**
@@ -266,6 +283,7 @@ public function getModule()
266
283
*/
267
284
private function getImageInfo ()
268
285
{
286
+ $ mediaDirectory = $ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA );
269
287
$ data = implode ('_ ' , $ this ->convertToReadableFormat ($ this ->miscParams ));
270
288
271
289
$ pathTemplate = $ this ->getModule ()
@@ -277,18 +295,20 @@ private function getImageInfo()
277
295
$ hashBasedPath = preg_replace (
278
296
'|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
279
297
DIRECTORY_SEPARATOR ,
280
- sprintf ($ pathTemplate , hash (' md5 ' , $ data ))
298
+ sprintf ($ pathTemplate , hash (self :: HASH_ALGORITHM , $ data ))
281
299
);
282
300
283
- if (is_readable ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ hashBasedPath )) {
301
+ if ($ mediaDirectory -> isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ hashBasedPath )) {
284
302
return $ hashBasedPath ;
285
303
}
286
304
287
305
// This loop is intended to preserve backward compatibility and keep
288
306
// existing encryption key based media gallery cache valid
289
307
// 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 ) {
291
310
if (str_starts_with ($ key , ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX )) {
311
+ // phpcs:disable Magento2.Functions.DiscouragedFunction
292
312
$ key = base64_decode (
293
313
substr ($ key , strlen (ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX ))
294
314
);
@@ -297,10 +317,10 @@ private function getImageInfo()
297
317
$ keyBasedPath = preg_replace (
298
318
'|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
299
319
DIRECTORY_SEPARATOR ,
300
- sprintf ($ pathTemplate , hash_hmac (" md5 " , $ data , $ key ))
320
+ sprintf ($ pathTemplate , hash_hmac (self :: HASH_ALGORITHM , $ data , $ key ))
301
321
);
302
322
303
- if (is_readable ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ keyBasedPath )) {
323
+ if ($ mediaDirectory -> isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ keyBasedPath )) {
304
324
return $ keyBasedPath ;
305
325
}
306
326
}
0 commit comments