Skip to content

Commit 9953ca1

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-92165: Redundancy in Custom Option Filenames
1 parent b6be7f3 commit 9953ca1

File tree

9 files changed

+86
-428
lines changed

9 files changed

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class ValidateFactory
1313
*/
1414
public function create()
1515
{
16-
return new \Zend_Validate();
16+
return new ExistingValidate();
1717
}
1818
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
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
/**
1517
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -62,25 +64,34 @@ class ValidatorFile extends Validator
6264
*/
6365
protected $isImageValidator;
6466

67+
/**
68+
* @var Random
69+
*/
70+
private $random;
71+
6572
/**
6673
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
6774
* @param \Magento\Framework\Filesystem $filesystem
6875
* @param \Magento\Framework\File\Size $fileSize
6976
* @param \Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory
7077
* @param \Magento\Framework\Validator\File\IsImage $isImageValidator
78+
* @param Random|null $random
7179
* @throws \Magento\Framework\Exception\FileSystemException
7280
*/
7381
public function __construct(
7482
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
7583
\Magento\Framework\Filesystem $filesystem,
7684
\Magento\Framework\File\Size $fileSize,
7785
\Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory,
78-
\Magento\Framework\Validator\File\IsImage $isImageValidator
86+
\Magento\Framework\Validator\File\IsImage $isImageValidator,
87+
Random $random = null
7988
) {
8089
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
8190
$this->filesystem = $filesystem;
8291
$this->httpFactory = $httpFactory;
8392
$this->isImageValidator = $isImageValidator;
93+
$this->random = $random
94+
?? ObjectManager::getInstance()->get(Random::class);
8495
parent::__construct($scopeConfig, $filesystem, $fileSize);
8596
}
8697

@@ -147,16 +158,15 @@ public function validate($processingParams, $option)
147158
$userValue = [];
148159

149160
if ($upload->isUploaded($file) && $upload->isValid($file)) {
150-
$extension = pathinfo(strtolower($fileInfo['name']), PATHINFO_EXTENSION);
151-
152161
$fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($fileInfo['name']);
153162
$dispersion = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
154163

155164
$filePath = $dispersion;
156165

157166
$tmpDirectory = $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
158167
$fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name'])));
159-
$filePath .= '/' . $fileHash . '.' . $extension;
168+
$fileRandomName = $this->random->getRandomString(32);
169+
$filePath .= '/' .$fileRandomName;
160170
$fileFullPath = $this->mediaDirectory->getAbsolutePath($this->quotePath . $filePath);
161171

162172
$upload->addFilter(new \Zend_Filter_File_Rename(['target' => $fileFullPath, 'overwrite' => true]));

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

Lines changed: 4 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
/**
@@ -90,7 +93,7 @@ public function validate($optionValue, $option)
9093
}
9194

9295
$result = false;
93-
if ($validatorChain->isValid($this->fileFullPath)) {
96+
if ($validatorChain->isValid($this->fileFullPath, $optionValue['title'])) {
9497
$result = $this->rootDirectory->isReadable($this->fileRelativePath)
9598
&& isset($optionValue['secret_key'])
9699
&& $this->buildSecretKey($this->fileRelativePath) == $optionValue['secret_key'];

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public function downloadFile($info)
7878
$this->_fileFactory->create(
7979
$info['title'],
8080
['value' => $this->_rootDir->getRelativePath($relativePath), 'type' => 'filename'],
81-
$this->rootDirBasePath
81+
$this->rootDirBasePath,
82+
$info['type']
8283
);
8384
}
8485

0 commit comments

Comments
 (0)