Skip to content

Commit 4142480

Browse files
committed
improve error message + resolve dest path after FProcess
1 parent 0757451 commit 4142480

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

Controller/Adminhtml/Index/Save/FileHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private function processFile(DocumentTypeInterface $documentType, RequestInterfa
9999
$data['file_path'] = null;
100100
} elseif (isset($fileUploader[0]['path'], $fileUploader[0]['file'])) {
101101
$fileSrcPath = $fileUploader[0]['path'] . $fileUploader[0]['file'];
102+
$this->rollbackData['destFilePath'] = $fileSrcPath;
102103

103104
$this->validator->validate($fileSrcPath, $documentType->getFileAllowedExtensions());
104105

Model/Document/Operation/CreateFromFile.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public function __construct(
4343

4444
public function execute(DocumentTypeInterface $documentType, string $file): DocumentInterface
4545
{
46-
$destFilePath = $this->fileHelper->getFileDestPath($documentType, $file);
47-
$document = $this->processorFactory->get($documentType->getCode())->execute($documentType, $destFilePath);
46+
$document = $this->processorFactory->get($documentType->getCode())->execute($documentType, $file);
4847
$destFilePath = $this->fileHelper->getFilePath($document);
4948
$this->fileHelper->moveFile($file, $destFilePath);
5049

Model/Document/Processor/SimpleProcessor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ public function __construct(
4040

4141
public function execute(DocumentTypeInterface $documentType, string $filePath): DocumentInterface
4242
{
43-
$fileName = basename($filePath);
43+
$destFilePath = $this->fileHelper->getFileDestPath($documentType, $filePath);
44+
$fileName = basename($destFilePath);
45+
4446
$this->documentBuilder->setTypeId($documentType->getId());
4547
$this->documentBuilder->setCode(Format::formatCode($fileName));
4648
$this->documentBuilder->setName(Format::formatName($fileName));
4749
$this->documentBuilder->setFileName($fileName);
48-
$this->documentBuilder->setFilePath(dirname($this->fileHelper->getRelativeFilePath($filePath)));
50+
$this->documentBuilder->setFilePath(dirname($this->fileHelper->getRelativeFilePath($destFilePath)));
4951

5052
return $this->documentBuilder->create();
5153
}

Model/File/Validator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Exception\ValidatorException;
1111
use Magento\Framework\File\Mime;
1212
use Magento\Framework\Phrase;
13+
use function implode;
1314
use function in_array;
1415
use function pathinfo;
1516
use function strtolower;
@@ -84,7 +85,10 @@ public function validate(string $file, array $allowedExtensions): bool
8485
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
8586

8687
if (!in_array($extension, $allowedExtensions, true)) {
87-
throw new ValidatorException(new Phrase('The file extension "%1" is not allowed.', [$extension]));
88+
throw new ValidatorException(new Phrase(
89+
'The file extension "%1" is not part of the allowed list: "%2".',
90+
[$extension, implode(', ', $allowedExtensions)]
91+
));
8892
}
8993
if ($this->mime->getMimeType($file) !== (self::MIME_TYPES[$extension] ?? 'application/octet-stream')) {
9094
throw new ValidatorException(new Phrase('The file extension "%1" does not match its mime type.'));

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,19 @@ final class CustomProcessor implements ProcessorInterface
125125

126126
public function execute(DocumentTypeInterface $documentType, string $filePath): DocumentInterface
127127
{
128-
// $filePath is the path where the file is being moved
129-
// You can change the destination path if you edit the file path value
130-
// with $this->documentBuilder->setFilePath($newDestPath)
131-
// and $this->documentBuilder->setFileName($newFileName)
128+
// $filePath is the path where the source file is currently saved.
129+
// You can change the destination path if want to.
130+
// Edit the file path value with $this->documentBuilder->setFilePath($newDestPath).
131+
// You can also rename the file with $this->documentBuilder->setFileName($newFileName)
132+
133+
$destFilePath = $this->fileHelper->getFileDestPath($documentType, $filePath);
134+
$fileName = basename($destFilePath);
132135

133-
$fileName = basename($filePath);
134136
$this->documentBuilder->setTypeId($documentType->getId());
135137
$this->documentBuilder->setCode(Format::formatCode($fileName));
136138
$this->documentBuilder->setName(Format::formatName($fileName));
137139
$this->documentBuilder->setFileName($fileName);
138-
$this->documentBuilder->setFilePath(dirname($this->fileHelper->getRelativeFilePath($filePath)));
140+
$this->documentBuilder->setFilePath(dirname($this->fileHelper->getRelativeFilePath($destFilePath)));
139141

140142
return $this->documentBuilder->create();
141143
}

0 commit comments

Comments
 (0)