Skip to content

Commit 54f3e2d

Browse files
committed
B2B-2067:Gallery Image is not saving the remote storage file on to local
- Added fix for the ProductTest.testGalleryAction
1 parent ba0b59c commit 54f3e2d

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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
64+
* @throws FileSystemException
65+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
66+
*/
67+
public function aroundGetImageWidth(Gallery $subject, callable $proceed): string
68+
{
69+
if($this->config->IsEnabled()){
70+
$file = $subject->getCurrentImage()->getPath();
71+
return $this->copyFileToTmp($file);
72+
}
73+
}
74+
75+
/**
76+
* Move files from storage to tmp folder
77+
*
78+
* @param string $filePath
79+
* @return string
80+
* @throws FileSystemException
81+
*/
82+
private function copyFileToTmp(string $filePath): string
83+
{
84+
if ($this->fileExistsInTmp($filePath)) {
85+
return $this->tmpFiles[$filePath];
86+
}
87+
$absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath);
88+
if ($this->remoteDirectoryWrite->isFile($absolutePath)) {
89+
$this->tmpDirectoryWrite->create();
90+
$tmpPath = $this->storeTmpName($filePath);
91+
$content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath);
92+
$filePath = $this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content)
93+
? $tmpPath
94+
: $filePath;
95+
}
96+
return $filePath;
97+
}
98+
99+
/**
100+
* Store created tmp image path
101+
*
102+
* @param string $filePath
103+
* @return string
104+
*/
105+
private function storeTmpName(string $filePath): string
106+
{
107+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
108+
$tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath);
109+
$this->tmpFiles[$filePath] = $tmpPath;
110+
return $tmpPath;
111+
}
112+
113+
/**
114+
* Check is file exist in tmp folder
115+
*
116+
* @param string $filePath
117+
* @return bool
118+
*/
119+
private function fileExistsInTmp(string $filePath): bool
120+
{
121+
return array_key_exists($filePath, $this->tmpFiles);
122+
}
123+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>
105105
</arguments>
106106
</type>
107+
<type name="Magento\Catalog\Block\Product\Gallery">
108+
<plugin name="galleryImageCopy" type="Magento\RemoteStorage\Plugin\GalleryImageCopy" />
109+
</type>
107110
<type name="Magento\ImportExport\Model\Import">
108111
<arguments>
109112
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>

0 commit comments

Comments
 (0)