Skip to content

Commit

Permalink
Use Mime component to determine mime type for file validator
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed May 19, 2020
1 parent b1c7383 commit 4c06e60
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Expand Up @@ -13,8 +13,10 @@

use Symfony\Component\HttpFoundation\File\File as FileObject;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

Expand Down Expand Up @@ -170,12 +172,12 @@ public function validate($value, Constraint $constraint)
}

if ($constraint->mimeTypes) {
if (!$value instanceof FileObject) {
$value = new FileObject($value);
if (!class_exists(MimeTypes::class)) {
throw new LogicException('Validating mime types requires the "Mime" component. Install "symfony/mime" to use it.');
}

$mimeTypes = (array) $constraint->mimeTypes;
$mime = $value->getMimeType();
$mime = MimeTypes::getDefault()->guessMimeType($value instanceof FileObject ? $value->getPathname() : realpath($value));

foreach ($mimeTypes as $mimeType) {
if ($mimeType === $mime) {
Expand Down

0 comments on commit 4c06e60

Please sign in to comment.