Skip to content

Commit 9a8ed48

Browse files
committed
B2B-2061: Validation for files(image) fails while not saving the remote storage file on local FS
1 parent b2cd0c1 commit 9a8ed48

File tree

5 files changed

+212
-73
lines changed

5 files changed

+212
-73
lines changed

app/code/Magento/Catalog/Model/Product/Option/Type/File/ValidateFactory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,30 @@
66

77
namespace Magento\Catalog\Model\Product\Option\Type\File;
88

9+
use Magento\Framework\ObjectManagerInterface;
10+
911
/**
1012
* Class ValidateFactory. Creates Validator with type "ExistingValidate"
1113
*/
1214
class ValidateFactory
1315
{
16+
private ObjectManagerInterface $objectManager;
17+
18+
/**
19+
* @param ObjectManagerInterface $objectManager
20+
*/
21+
public function __construct(ObjectManagerInterface $objectManager)
22+
{
23+
$this->objectManager = $objectManager;
24+
}
25+
1426
/**
1527
* Main factory method
1628
*
1729
* @return \Zend_Validate
1830
*/
1931
public function create()
2032
{
21-
return new ExistingValidate();
33+
return $this->objectManager->create(ExistingValidate::class);
2234
}
2335
}

app/code/Magento/Catalog/Model/Product/Option/Type/File/Validator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,12 @@ protected function isImage($fileInfo)
193193
if (!$this->rootDirectory->isReadable($this->rootDirectory->getRelativePath($fileInfo))) {
194194
return false;
195195
}
196-
$imageInfo = getimagesize($fileInfo);
197-
if (!$imageInfo) {
196+
197+
$fileContent = $this->rootDirectory->readFile($fileInfo);
198+
if (empty($fileContent) || !getimagesizefromstring($fileContent)) {
198199
return false;
199200
}
201+
200202
return true;
201203
}
202204
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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\Exception\RuntimeException;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\TargetDirectory;
15+
use Magento\Framework\Image\Adapter\AbstractAdapter;
16+
use Magento\RemoteStorage\Model\Config;
17+
use Psr\Log\LoggerInterface;
18+
use Magento\Catalog\Model\Product\Option\Type\File\ExistingValidate as Subject;
19+
20+
/**
21+
* @see AbstractAdapter
22+
*/
23+
class ExistingValidate
24+
{
25+
/**
26+
* @var Filesystem\Directory\WriteInterface
27+
*/
28+
private $tmpDirectoryWrite;
29+
30+
/**
31+
* @var Filesystem\Directory\WriteInterface
32+
*/
33+
private $remoteDirectoryWrite;
34+
35+
/**
36+
* @var array
37+
*/
38+
private $tmpFiles = [];
39+
40+
/**
41+
* @var bool
42+
*/
43+
private $isEnabled;
44+
45+
/**
46+
* @var LoggerInterface
47+
*/
48+
private $logger;
49+
50+
/**
51+
* @param Filesystem $filesystem
52+
* @param TargetDirectory $targetDirectory
53+
* @param Config $config
54+
* @param LoggerInterface $logger
55+
* @throws FileSystemException
56+
* @throws RuntimeException
57+
*/
58+
public function __construct(
59+
Filesystem $filesystem,
60+
TargetDirectory $targetDirectory,
61+
Config $config,
62+
LoggerInterface $logger
63+
) {
64+
$this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP);
65+
$this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT);
66+
$this->isEnabled = $config->isEnabled();
67+
$this->logger = $logger;
68+
}
69+
70+
/**
71+
* Copies file from the remote server to the tmp directory
72+
*
73+
* @param Subject $subject
74+
* @param $value
75+
* @param string|null $originalName
76+
* @return array
77+
* @throws FileSystemException
78+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
79+
*/
80+
public function beforeIsValid(Subject $subject, $value, string $originalName = null)
81+
{
82+
if ($this->isEnabled) {
83+
$value = $this->copyFileToTmp($value);
84+
}
85+
return [$value, $originalName];
86+
}
87+
88+
/**
89+
* Remove created tmp files
90+
*/
91+
public function __destruct()
92+
{
93+
try {
94+
foreach ($this->tmpFiles as $key => $tmpFile) {
95+
$this->tmpDirectoryWrite->delete($tmpFile);
96+
unset($this->tmpFiles[$key]);
97+
}
98+
} catch (\Exception $e) {
99+
$this->logger->error($e->getMessage());
100+
}
101+
}
102+
103+
/**
104+
* Move files from storage to tmp folder
105+
*
106+
* @param string $filePath
107+
* @return string
108+
* @throws FileSystemException
109+
*/
110+
private function copyFileToTmp(string $filePath): string
111+
{
112+
if (isset($this->tmpFiles[$filePath])) {
113+
return $this->tmpFiles[$filePath];
114+
}
115+
116+
$absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath);
117+
if ($this->remoteDirectoryWrite->isFile($absolutePath)) {
118+
$this->tmpDirectoryWrite->create();
119+
$tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath);
120+
$content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath);
121+
if ($this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content) >= 0) {
122+
$filePath = $tmpPath;
123+
$this->tmpFiles[$tmpPath] = $tmpPath;
124+
}
125+
}
126+
return $filePath;
127+
}
128+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>
9797
</arguments>
9898
</type>
99+
<type name="Magento\Catalog\Model\Product\Option\Type\File\ExistingValidate">
100+
<plugin name="remoteValidatorInfo" type="Magento\RemoteStorage\Plugin\ExistingValidate" sortOrder="10"/>
101+
</type>
99102
<type name="Magento\Framework\Image\Adapter\AbstractAdapter">
100103
<plugin name="remoteImageFile" type="Magento\RemoteStorage\Plugin\Image" sortOrder="10"/>
101104
</type>

0 commit comments

Comments
 (0)