Skip to content

Commit 9ae22f6

Browse files
authored
Merge branch '2.4-develop' into 2.4-develop-fast-lane-prs
2 parents 24517f8 + fa1824e commit 9ae22f6

File tree

27 files changed

+898
-199
lines changed

27 files changed

+898
-199
lines changed

app/code/Magento/Catalog/Block/Product/Gallery.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class Gallery extends \Magento\Framework\View\Element\Template
2525
{
2626
/**
27-
* Core registry
27+
* Framework class for Core Registry
2828
*
2929
* @var \Magento\Framework\Registry
3030
*/
@@ -122,9 +122,9 @@ public function getImageFile()
122122
public function getImageWidth()
123123
{
124124
$file = $this->getCurrentImage()->getPath();
125-
125+
$fileStat = $this->getMediaDirectory()->stat($file);
126126
if ($this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->isFile($file)) {
127-
$size = getimagesize($file);
127+
$size = $fileStat['size'];
128128
if (isset($size[0])) {
129129
if ($size[0] > 600) {
130130
return 600;

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
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public function __construct(
4040
}
4141

4242
/**
43+
* Saves file
44+
*
4345
* @param ImageContentInterface $imageContent
4446
* @return string
47+
* @throws \Magento\Framework\Exception\InputException
4548
*/
4649
protected function saveFile(ImageContentInterface $imageContent)
4750
{
@@ -50,6 +53,8 @@ protected function saveFile(ImageContentInterface $imageContent)
5053
}
5154

5255
/**
56+
* Save file content and return file details
57+
*
5358
* @param ImageContentInterface $imageContent
5459
* @return array
5560
* @throws \Magento\Framework\Exception\LocalizedException
@@ -58,16 +63,19 @@ public function processFileContent(ImageContentInterface $imageContent)
5863
{
5964
$filePath = $this->saveFile($imageContent);
6065

61-
$fileAbsolutePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($filePath);
62-
$fileHash = hash('sha256', $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->readFile($filePath));
63-
$imageSize = getimagesize($fileAbsolutePath);
66+
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
67+
$fileAbsolutePath = $mediaDirectory->getAbsolutePath($filePath);
68+
$fileContent = $mediaDirectory->readFile($filePath);
69+
$fileHash = hash('sha256', $fileContent);
70+
$imageSize = getimagesizefromstring($fileContent);
71+
$stat = $mediaDirectory->stat($fileAbsolutePath);
6472
$result = [
6573
'type' => $imageContent->getType(),
6674
'title' => $imageContent->getName(),
6775
'fullpath' => $fileAbsolutePath,
6876
'quote_path' => $filePath,
6977
'order_path' => $filePath,
70-
'size' => filesize($fileAbsolutePath),
78+
'size' => $stat['size'],
7179
'width' => $imageSize ? $imageSize[0] : 0,
7280
'height' => $imageSize ? $imageSize[1] : 0,
7381
'secret_key' => substr($fileHash, 0, 20),

app/code/Magento/ImportExport/Model/Import/Source/Zip.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\ImportExport\Model\Import\Source;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\Exception\ValidatorException;
910

1011
/**
@@ -16,15 +17,17 @@ class Zip extends Csv
1617
* @param string $file
1718
* @param \Magento\Framework\Filesystem\Directory\Write $directory
1819
* @param string $options
20+
* @param \Magento\Framework\Archive\Zip|null $zipArchive
1921
* @throws \Magento\Framework\Exception\LocalizedException
2022
* @throws \Magento\Framework\Exception\ValidatorException
2123
*/
2224
public function __construct(
2325
$file,
2426
\Magento\Framework\Filesystem\Directory\Write $directory,
25-
$options
27+
$options,
28+
\Magento\Framework\Archive\Zip $zipArchive = null
2629
) {
27-
$zip = new \Magento\Framework\Archive\Zip();
30+
$zip = $zipArchive ?? ObjectManager::getInstance()->get(\Magento\Framework\Archive\Zip::class);
2831
$csvFile = $zip->unpack(
2932
$file,
3033
preg_replace('/\.zip$/i', '.csv', $file)
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+
}

0 commit comments

Comments
 (0)