Skip to content

Commit 3eda046

Browse files
Merge MAGETWO-92163 into 2.3-bugfixes-161018
2 parents 27b3d48 + 1e25d77 commit 3eda046

File tree

9 files changed

+127
-429
lines changed

9 files changed

+127
-429
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Model\Product\Option\Type\File;
10+
11+
/**
12+
* Validator for existing (already saved) files.
13+
*/
14+
class ExistingValidate extends \Zend_Validate
15+
{
16+
/**
17+
* @inheritDoc
18+
*
19+
* @param string $value File's full path.
20+
* @param string|null $originalName Original file's name (when uploaded).
21+
*/
22+
public function isValid($value, string $originalName = null)
23+
{
24+
$this->_messages = [];
25+
$this->_errors = [];
26+
27+
if (!is_string($value)) {
28+
$this->_messages[] = __('Full file path is expected.')->render();
29+
return false;
30+
}
31+
32+
$result = true;
33+
$fileInfo = null;
34+
if ($originalName) {
35+
$fileInfo = ['name' => $originalName];
36+
}
37+
foreach ($this->_validators as $element) {
38+
$validator = $element['instance'];
39+
if ($validator->isValid($value, $fileInfo)) {
40+
continue;
41+
}
42+
$result = false;
43+
$messages = $validator->getMessages();
44+
$this->_messages = array_merge($this->_messages, $messages);
45+
$this->_errors = array_merge($this->_errors, array_keys($messages));
46+
if ($element['breakChainOnFailure']) {
47+
break;
48+
}
49+
}
50+
return $result;
51+
}
52+
}

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

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

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

9+
/**
10+
* Class ValidateFactory. Creates Validator with type "ExistingValidate"
11+
*/
912
class ValidateFactory
1013
{
1114
/**
15+
* Main factory method
16+
*
1217
* @return \Zend_Validate
1318
*/
1419
public function create()
1520
{
16-
return new \Zend_Validate();
21+
return new ExistingValidate();
1722
}
1823
}

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
use Magento\Framework\App\Filesystem\DirectoryList;
1111
use Magento\Catalog\Model\Product\Exception as ProductException;
1212
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Math\Random;
14+
use Magento\Framework\App\ObjectManager;
1315

1416
/**
17+
* Validator class. Represents logic for validation file given from product option
18+
*
1519
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1620
*/
1721
class ValidatorFile extends Validator
@@ -63,28 +67,41 @@ class ValidatorFile extends Validator
6367
protected $isImageValidator;
6468

6569
/**
70+
* @var Random
71+
*/
72+
private $random;
73+
74+
/**
75+
* Constructor method
76+
*
6677
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
6778
* @param \Magento\Framework\Filesystem $filesystem
6879
* @param \Magento\Framework\File\Size $fileSize
6980
* @param \Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory
7081
* @param \Magento\Framework\Validator\File\IsImage $isImageValidator
82+
* @param Random|null $random
7183
* @throws \Magento\Framework\Exception\FileSystemException
7284
*/
7385
public function __construct(
7486
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
7587
\Magento\Framework\Filesystem $filesystem,
7688
\Magento\Framework\File\Size $fileSize,
7789
\Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory,
78-
\Magento\Framework\Validator\File\IsImage $isImageValidator
90+
\Magento\Framework\Validator\File\IsImage $isImageValidator,
91+
Random $random = null
7992
) {
8093
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
8194
$this->filesystem = $filesystem;
8295
$this->httpFactory = $httpFactory;
8396
$this->isImageValidator = $isImageValidator;
97+
$this->random = $random
98+
?? ObjectManager::getInstance()->get(Random::class);
8499
parent::__construct($scopeConfig, $filesystem, $fileSize);
85100
}
86101

87102
/**
103+
* Setter method for the product
104+
*
88105
* @param Product $product
89106
* @return $this
90107
*/
@@ -95,6 +112,8 @@ public function setProduct(Product $product)
95112
}
96113

97114
/**
115+
* Validation method
116+
*
98117
* @param \Magento\Framework\DataObject $processingParams
99118
* @param \Magento\Catalog\Model\Product\Option $option
100119
* @return array
@@ -154,16 +173,15 @@ public function validate($processingParams, $option)
154173
$userValue = [];
155174

156175
if ($upload->isUploaded($file) && $upload->isValid($file)) {
157-
$extension = pathinfo(strtolower($fileInfo['name']), PATHINFO_EXTENSION);
158-
159176
$fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($fileInfo['name']);
160177
$dispersion = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
161178

162179
$filePath = $dispersion;
163180

164181
$tmpDirectory = $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
165182
$fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name'])));
166-
$filePath .= '/' . $fileHash . '.' . $extension;
183+
$fileRandomName = $this->random->getRandomString(32);
184+
$filePath .= '/' .$fileRandomName;
167185
$fileFullPath = $this->mediaDirectory->getAbsolutePath($this->quotePath . $filePath);
168186

169187
$upload->addFilter(new \Zend_Filter_File_Rename(['target' => $fileFullPath, 'overwrite' => true]));
@@ -243,6 +261,8 @@ protected function initFilesystem()
243261
}
244262

245263
/**
264+
* Validate contents length method
265+
*
246266
* @return bool
247267
* @todo need correctly name
248268
*/

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

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

9+
/**
10+
* Validator for existing files.
11+
*/
912
class ValidatorInfo extends Validator
1013
{
1114
/**
@@ -34,6 +37,8 @@ class ValidatorInfo extends Validator
3437
protected $fileRelativePath;
3538

3639
/**
40+
* Construct method
41+
*
3742
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
3843
* @param \Magento\Framework\Filesystem $filesystem
3944
* @param \Magento\Framework\File\Size $fileSize
@@ -53,6 +58,8 @@ public function __construct(
5358
}
5459

5560
/**
61+
* Setter method for property "useQuotePath"
62+
*
5663
* @param mixed $useQuotePath
5764
* @return $this
5865
*/
@@ -63,6 +70,8 @@ public function setUseQuotePath($useQuotePath)
6370
}
6471

6572
/**
73+
* Validate method for the option value depends on an option
74+
*
6675
* @param array $optionValue
6776
* @param \Magento\Catalog\Model\Product\Option $option
6877
* @return bool
@@ -90,7 +99,7 @@ public function validate($optionValue, $option)
9099
}
91100

92101
$result = false;
93-
if ($validatorChain->isValid($this->fileFullPath)) {
102+
if ($validatorChain->isValid($this->fileFullPath, $optionValue['title'])) {
94103
$result = $this->rootDirectory->isReadable($this->fileRelativePath)
95104
&& isset($optionValue['secret_key'])
96105
&& $this->buildSecretKey($this->fileRelativePath) == $optionValue['secret_key'];
@@ -109,6 +118,8 @@ public function validate($optionValue, $option)
109118
}
110119

111120
/**
121+
* Method for creation secret key for the given file
122+
*
112123
* @param string $fileRelativePath
113124
* @return string
114125
*/
@@ -118,6 +129,8 @@ protected function buildSecretKey($fileRelativePath)
118129
}
119130

120131
/**
132+
* Calculates path for the file
133+
*
121134
* @param array $optionValue
122135
* @return void
123136
*/

app/code/Magento/Sales/Model/Download.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\Exception\LocalizedException;
1010

11+
/**
12+
* Class Download. Represents download logic for files
13+
*/
1114
class Download
1215
{
1316
/**
@@ -36,6 +39,8 @@ class Download
3639
protected $rootDirBasePath;
3740

3841
/**
42+
* Constructor method
43+
*
3944
* @param \Magento\Framework\Filesystem $filesystem
4045
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase
4146
* @param \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $storageDatabaseFactory
@@ -78,11 +83,14 @@ public function downloadFile($info)
7883
$this->_fileFactory->create(
7984
$info['title'],
8085
['value' => $this->_rootDir->getRelativePath($relativePath), 'type' => 'filename'],
81-
$this->rootDirBasePath
86+
$this->rootDirBasePath,
87+
$info['type']
8288
);
8389
}
8490

8591
/**
92+
* Method checks, if file can be returned depends on the given filepath
93+
*
8694
* @param string $relativePath
8795
* @return bool
8896
*/

0 commit comments

Comments
 (0)