Skip to content

Commit b657309

Browse files
authored
FFWEB-3210: Fix upload validation SSH key file issue
Fix upload validation SSH key file issue
1 parent 03fc054 commit b657309

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### Fix
77
- Support MSI - checking all inventory sources during export feed
88
- Fix feed path for UI export type
9+
- Fix upload validation SSH key file issue
910

1011
## [v5.0.0] - 2024.07.08
1112
### BREAKING

src/Model/Config/Backend/Rsa.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,66 @@
44

55
namespace Omikron\Factfinder\Model\Config\Backend;
66

7+
use Exception;
78
use Magento\Config\Model\Config\Backend\File;
89
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\MediaStorage\Model\File\Uploader;
912

13+
/**
14+
* @SuppressWarnings(PHPMD)
15+
*/
1016
class Rsa extends File
1117
{
18+
public function beforeSave()
19+
{
20+
$value = $this->getValue();
21+
$file = $this->getFileData();
22+
23+
if (!empty($file)) {
24+
$uploadDir = $this->_getUploadDir();
25+
try {
26+
/** @var Uploader $uploader */
27+
$uploader = $this->_uploaderFactory->create(['fileId' => $file]);
28+
$uploader->setAllowedExtensions($this->_getAllowedExtensions());
29+
$uploader->setAllowRenameFiles(true);
30+
$uploader->addValidateCallback('size', $this, 'validateMaxSize');
31+
$result = $uploader->save($uploadDir);
32+
} catch (Exception $e) {
33+
throw new LocalizedException(__('%1', $e->getMessage()));
34+
}
35+
if ($result !== false) {
36+
$filename = $result['file'];
37+
if ($this->_addWhetherScopeInfo()) {
38+
$filename = $this->_prependScopeInfo($filename);
39+
}
40+
$this->setValue($filename);
41+
}
42+
} else {
43+
if (is_array($value) && !empty($value['delete'])) {
44+
$this->setValue('');
45+
} elseif (is_array($value) && !empty($value['value'])) {
46+
$this->setValueAfterValidation($value['value']);
47+
} else {
48+
$this->unsValue();
49+
}
50+
}
51+
52+
return $this;
53+
}
54+
1255
protected function getUploadDirPath($uploadDir)
1356
{
1457
return $this->_filesystem->getDirectoryWrite(DirectoryList::CONFIG)->getAbsolutePath($uploadDir);
1558
}
59+
60+
private function setValueAfterValidation(string $value): void
61+
{
62+
// avoid intercepting value
63+
if (preg_match('/[^a-z0-9_\/\\-\\.]+/i', $value)) {
64+
throw new LocalizedException(__('Invalid file name'));
65+
}
66+
67+
$this->setValue($value);
68+
}
1669
}

0 commit comments

Comments
 (0)