|
10 | 10 | use Magento\Framework\App\Filesystem\DirectoryList;
|
11 | 11 | use Magento\Catalog\Model\Product\Exception as ProductException;
|
12 | 12 | use Magento\Framework\Exception\LocalizedException;
|
| 13 | +use Magento\Framework\Math\Random; |
| 14 | +use Magento\Framework\App\ObjectManager; |
13 | 15 |
|
14 | 16 | /**
|
15 | 17 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
@@ -62,25 +64,34 @@ class ValidatorFile extends Validator
|
62 | 64 | */
|
63 | 65 | protected $isImageValidator;
|
64 | 66 |
|
| 67 | + /** |
| 68 | + * @var Random |
| 69 | + */ |
| 70 | + private $random; |
| 71 | + |
65 | 72 | /**
|
66 | 73 | * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
67 | 74 | * @param \Magento\Framework\Filesystem $filesystem
|
68 | 75 | * @param \Magento\Framework\File\Size $fileSize
|
69 | 76 | * @param \Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory
|
70 | 77 | * @param \Magento\Framework\Validator\File\IsImage $isImageValidator
|
| 78 | + * @param Random|null $random |
71 | 79 | * @throws \Magento\Framework\Exception\FileSystemException
|
72 | 80 | */
|
73 | 81 | public function __construct(
|
74 | 82 | \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
|
75 | 83 | \Magento\Framework\Filesystem $filesystem,
|
76 | 84 | \Magento\Framework\File\Size $fileSize,
|
77 | 85 | \Magento\Framework\HTTP\Adapter\FileTransferFactory $httpFactory,
|
78 |
| - \Magento\Framework\Validator\File\IsImage $isImageValidator |
| 86 | + \Magento\Framework\Validator\File\IsImage $isImageValidator, |
| 87 | + Random $random = null |
79 | 88 | ) {
|
80 | 89 | $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
81 | 90 | $this->filesystem = $filesystem;
|
82 | 91 | $this->httpFactory = $httpFactory;
|
83 | 92 | $this->isImageValidator = $isImageValidator;
|
| 93 | + $this->random = $random |
| 94 | + ? $random : ObjectManager::getInstance()->get(Random::class); |
84 | 95 | parent::__construct($scopeConfig, $filesystem, $fileSize);
|
85 | 96 | }
|
86 | 97 |
|
@@ -147,16 +158,14 @@ public function validate($processingParams, $option)
|
147 | 158 | $userValue = [];
|
148 | 159 |
|
149 | 160 | if ($upload->isUploaded($file) && $upload->isValid($file)) {
|
150 |
| - $extension = pathinfo(strtolower($fileInfo['name']), PATHINFO_EXTENSION); |
151 |
| - |
152 | 161 | $fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($fileInfo['name']);
|
153 | 162 | $dispersion = \Magento\MediaStorage\Model\File\Uploader::getDispretionPath($fileName);
|
154 | 163 |
|
155 | 164 | $filePath = $dispersion;
|
156 | 165 |
|
157 | 166 | $tmpDirectory = $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
|
158 |
| - $fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name']))); |
159 |
| - $filePath .= '/' . $fileHash . '.' . $extension; |
| 167 | + $fileHash = $this->random->getRandomString(32); |
| 168 | + $filePath .= '/' . $fileHash; |
160 | 169 | $fileFullPath = $this->mediaDirectory->getAbsolutePath($this->quotePath . $filePath);
|
161 | 170 |
|
162 | 171 | $upload->addFilter(new \Zend_Filter_File_Rename(['target' => $fileFullPath, 'overwrite' => true]));
|
|
0 commit comments