Skip to content

Commit 5313c8e

Browse files
committed
Merge remote-tracking branch 'origin/B2B-2067' into B2B-2022
2 parents 9d60d6f + 59087ba commit 5313c8e

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\RemoteStorage\Plugin;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Exception\FileSystemException;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Directory\TargetDirectory;
14+
use Magento\Catalog\Block\Product\Gallery;
15+
use Magento\RemoteStorage\Model\Config;
16+
17+
/**
18+
* Class to copy the image to tmp folder
19+
*/
20+
class GalleryImageCopy
21+
{
22+
/**
23+
* @var Filesystem\Directory\WriteInterface
24+
*/
25+
private $tmpDirectoryWrite;
26+
27+
/**
28+
* @var Filesystem\Directory\WriteInterface
29+
*/
30+
private $remoteDirectoryWrite;
31+
32+
/**
33+
* @var array
34+
*/
35+
private $tmpFiles = [];
36+
37+
/**
38+
* @var Config
39+
*/
40+
private Config $config;
41+
42+
/**
43+
* @param Filesystem $filesystem
44+
* @param TargetDirectory $targetDirectory
45+
* @param Config $config
46+
* @throws FileSystemException
47+
*/
48+
public function __construct(
49+
Filesystem $filesystem,
50+
TargetDirectory $targetDirectory,
51+
Config $config
52+
) {
53+
$this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP);
54+
$this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT);
55+
$this->config = $config;
56+
}
57+
58+
/**
59+
* Copy file from remote server to tmp directory of Magento to get Image width
60+
*
61+
* @param Gallery $subject
62+
* @param callable $proceed
63+
* @return string|null
64+
* @throws FileSystemException
65+
* @throws \Magento\Framework\Exception\RuntimeException
66+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
67+
*/
68+
public function aroundGetImageWidth(Gallery $subject, callable $proceed): string
69+
{
70+
return $this->copyFileToTmp($subject, $proceed);
71+
}
72+
73+
/**
74+
* Move files from storage to tmp folder
75+
*
76+
* @param Gallery $subject
77+
* @param callable $proceed
78+
* @return string|null
79+
* @throws FileSystemException
80+
* @throws \Magento\Framework\Exception\RuntimeException
81+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
82+
*/
83+
private function copyFileToTmp(Gallery $subject, callable $proceed): string
84+
{
85+
if ($this->config->IsEnabled()) {
86+
$filePath = $subject->getCurrentImage()->getPath();
87+
if ($this->fileExistsInTmp($filePath)) {
88+
return $this->tmpFiles[$filePath];
89+
}
90+
$absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath);
91+
if ($this->remoteDirectoryWrite->isFile($absolutePath)) {
92+
$this->tmpDirectoryWrite->create();
93+
$tmpPath = $this->storeTmpName($filePath);
94+
$content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath);
95+
return $this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content)
96+
? $tmpPath
97+
: $filePath;
98+
}
99+
}
100+
}
101+
102+
/**
103+
* Store created tmp image path
104+
*
105+
* @param string $filePath
106+
* @return string
107+
*/
108+
private function storeTmpName(string $filePath): string
109+
{
110+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
111+
$tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath);
112+
$this->tmpFiles[$filePath] = $tmpPath;
113+
return $tmpPath;
114+
}
115+
116+
/**
117+
* Check is file exist in tmp folder
118+
*
119+
* @param string $filePath
120+
* @return bool
121+
*/
122+
private function fileExistsInTmp(string $filePath): bool
123+
{
124+
return array_key_exists($filePath, $this->tmpFiles);
125+
}
126+
}

app/code/Magento/RemoteStorage/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@
115115
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>
116116
</arguments>
117117
</type>
118+
<type name="Magento\Catalog\Block\Product\Gallery">
119+
<plugin name="galleryImageCopy" type="Magento\RemoteStorage\Plugin\GalleryImageCopy" />
120+
</type>
118121
<type name="Magento\ImportExport\Model\Import">
119122
<arguments>
120123
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>

0 commit comments

Comments
 (0)