Skip to content

Commit f9d370f

Browse files
committed
B2B-2061: Merge
2 parents 156ab35 + 2b95196 commit f9d370f

File tree

5 files changed

+219
-74
lines changed

5 files changed

+219
-74
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,33 @@
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+
/**
17+
* @var ObjectManagerInterface
18+
*/
19+
private ObjectManagerInterface $objectManager;
20+
21+
/**
22+
* @param ObjectManagerInterface $objectManager
23+
*/
24+
public function __construct(ObjectManagerInterface $objectManager)
25+
{
26+
$this->objectManager = $objectManager;
27+
}
28+
1429
/**
1530
* Main factory method
1631
*
17-
* @return \Zend_Validate
32+
* @return ExistingValidate
1833
*/
1934
public function create()
2035
{
21-
return new ExistingValidate();
36+
return $this->objectManager->create(ExistingValidate::class);
2237
}
2338
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ protected function parseExtensionsString($extensions)
133133
}
134134

135135
/**
136+
* Adds required validators to th $object
137+
*
136138
* @param \Zend_File_Transfer_Adapter_Http|\Zend_Validate $object
137139
* @param \Magento\Catalog\Model\Product\Option $option
138140
* @param array $fileFullPath
@@ -193,10 +195,12 @@ protected function isImage($fileInfo)
193195
if (!$this->rootDirectory->isReadable($this->rootDirectory->getRelativePath($fileInfo))) {
194196
return false;
195197
}
196-
$imageInfo = getimagesize($fileInfo);
197-
if (!$imageInfo) {
198+
199+
$fileContent = $this->rootDirectory->readFile($fileInfo);
200+
if (empty($fileContent) || !getimagesizefromstring($fileContent)) {
198201
return false;
199202
}
203+
200204
return true;
201205
}
202206
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 string $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+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
120+
$tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath);
121+
$content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath);
122+
if ($this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content) >= 0) {
123+
$filePath = $tmpPath;
124+
$this->tmpFiles[$tmpPath] = $tmpPath;
125+
}
126+
}
127+
return $filePath;
128+
}
129+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@
101101
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>
102102
</arguments>
103103
</type>
104+
<type name="Magento\Catalog\Model\Product\Option\Type\File\ExistingValidate">
105+
<plugin name="remoteValidatorInfo" type="Magento\RemoteStorage\Plugin\ExistingValidate" sortOrder="10"/>
106+
</type>
104107
<type name="Magento\Framework\Image\Adapter\AbstractAdapter">
105108
<plugin name="remoteImageFile" type="Magento\RemoteStorage\Plugin\Image" sortOrder="10"/>
106109
</type>

0 commit comments

Comments
 (0)