-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0996b5e
commit d766c75
Showing
3 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace AdrienDupuis\EzPlatformStandardBundle\FieldType\Validator; | ||
|
||
use eZ\Publish\Core\FieldType; | ||
use eZ\Publish\Core\FieldType\ValidationError; | ||
use eZ\Publish\Core\MVC\ConfigResolverInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class FileTypeWhiteList extends FieldType\Validator | ||
{ | ||
/** @var ConfigResolverInterface */ | ||
private $configResolver; | ||
|
||
/** @var RequestStack */ | ||
private $requestStack; | ||
|
||
/** @var \finfo */ | ||
private $mimeInfo; | ||
|
||
protected $constraints = [ | ||
'fileTypeWhiteList' => [], | ||
]; | ||
|
||
protected $constraintsSchema = [ | ||
'fileTypeWhiteList' => [ | ||
'type' => 'array', | ||
'default' => [], | ||
], | ||
]; | ||
|
||
public function __construct(ConfigResolverInterface $configResolver, RequestStack $requestStack) | ||
{ | ||
$this->configResolver = $configResolver; | ||
$this->requestStack = $requestStack; | ||
$this->mimeInfo = new \finfo(FILEINFO_MIME); | ||
|
||
$this->constraints['fileTypeWhiteList'] = $this->configResolver->getParameter( | ||
'io.file_storage.file_type_whitelist' | ||
); | ||
} | ||
|
||
public function validateConstraints($constraints) | ||
{ | ||
return []; | ||
} | ||
|
||
public function validate(FieldType\Value $value): bool | ||
{ | ||
$path = $this->getTmpFilePath($value); | ||
|
||
if (is_file($path) && !is_dir($path)) { | ||
$fileType = $this->getFileType($path); | ||
|
||
if (in_array($fileType, $this->constraints['fileTypeWhiteList'])) { | ||
return true; | ||
} | ||
|
||
$this->errors[] = new ValidationError( | ||
'A valid file is required. Following file type is not on the whitelist: %fileType%', | ||
null, | ||
[ | ||
'%fileType%' => $fileType, | ||
], | ||
'fileTypeWhiteList' | ||
); | ||
|
||
return false; | ||
} | ||
|
||
$this->errors[] = new ValidationError( | ||
'A valid file is required. File has been invalidated early in the process; Its size might exceed '.ini_get('upload_max_filesize'), | ||
null, | ||
[], | ||
'fileTypeWhiteList' | ||
); | ||
|
||
return false; | ||
} | ||
|
||
public function getTmpFilePath(FieldType\Value $value): string | ||
{ | ||
if (isset($value->inputUri)) { | ||
return $value->inputUri; | ||
} | ||
|
||
//TODO: Use abstraction; Handle DFS | ||
return "{$this->configResolver->getParameter('var_dir')}/{$this->configResolver->getParameter('storage_dir')}/original/{$value->id}"; | ||
} | ||
|
||
public function getFileType(string $path) | ||
{ | ||
return explode('; ', $this->mimeInfo->file($path))[0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters