Skip to content

Commit 3577b28

Browse files
committed
Use Mime component to determine mime type for file validator
1 parent b1c7383 commit 3577b28

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/Symfony/Component/Validator/Constraints/FileValidator.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
use Symfony\Component\HttpFoundation\File\File as FileObject;
1515
use Symfony\Component\HttpFoundation\File\UploadedFile;
16+
use Symfony\Component\Mime\MimeTypes;
1617
use Symfony\Component\Validator\Constraint;
1718
use Symfony\Component\Validator\ConstraintValidator;
19+
use Symfony\Component\Validator\Exception\LogicException;
1820
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1921
use Symfony\Component\Validator\Exception\UnexpectedValueException;
2022

@@ -170,12 +172,12 @@ public function validate($value, Constraint $constraint)
170172
}
171173

172174
if ($constraint->mimeTypes) {
173-
if (!$value instanceof FileObject) {
174-
$value = new FileObject($value);
175+
if (!class_exists(MimeTypes::class)) {
176+
throw new LogicException('You cannot validate the mime-type of files as the Mime component is not installed. Try running "composer require symfony/mime".');
175177
}
176178

177179
$mimeTypes = (array) $constraint->mimeTypes;
178-
$mime = $value->getMimeType();
180+
$mime = $value instanceof FileObject ? $value->getMimeType() : MimeTypes::getDefault()->guessMimeType($path);
179181

180182
foreach ($mimeTypes as $mimeType) {
181183
if ($mimeType === $mime) {

0 commit comments

Comments
 (0)