Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete uploaded resource after failed validation #174

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions Classes/Validation/FileTypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
* source code.
*/

use Neos\Flow\Annotations as Flow;
use Neos\Flow\ResourceManagement\PersistentResource;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Flow\Validation\Validator\AbstractValidator;

/**
Expand All @@ -20,6 +22,12 @@
*/
class FileTypeValidator extends AbstractValidator
{
/**
* @Flow\Inject
* @var ResourceManager
*/
protected ResourceManager $resourceManager;

/**
* @var array
*/
Expand All @@ -41,13 +49,18 @@ protected function isValid($resource)
$this->addError('The given value was not a PersistentResource instance.', 1327865587);
return;
}

$fileExtension = $resource->getFileExtension();

if ($fileExtension === null || $fileExtension === '') {
$this->addError('The file has no file extension.', 1327865808);
$this->resourceManager->deleteResource($resource);
return;
}

if (!in_array($fileExtension, $this->options['allowedExtensions'])) {
$this->addError('The file extension "%s" is not allowed.', 1327865764, array($resource->getFileExtension()));
$this->resourceManager->deleteResource($resource);
return;
}
}
Expand Down
Loading