Skip to content

Commit 753f7b4

Browse files
AC-12682 use DriverInterface to work with remote storage
1 parent 6600516 commit 753f7b4

File tree

1 file changed

+26
-6
lines changed
  • app/code/Magento/Catalog/Model/View/Asset

1 file changed

+26
-6
lines changed

app/code/Magento/Catalog/Model/View/Asset/Image.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
use Magento\Framework\Config\ConfigOptionsListConstants;
1515
use Magento\Framework\Encryption\EncryptorInterface;
1616
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\Filesystem;
1718
use Magento\Framework\View\Asset\ContextInterface;
1819
use Magento\Framework\View\Asset\LocalInterface;
1920
use 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
@@ -25,6 +27,11 @@
2527
*/
2628
class 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

Comments
 (0)