Skip to content

Commit 05416a2

Browse files
committed
Use Mime component to determine mime type for file validator
1 parent 96d2d19 commit 05416a2

File tree

4 files changed

+28
-60
lines changed

4 files changed

+28
-60
lines changed

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

+14-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,21 @@ 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+
$mime = MimeTypes::getDefault()->guessMimeType($path);
177+
} else {
178+
if (!$value instanceof FileObject) {
179+
if (!class_exists(FileObject::class)) {
180+
throw new LogicException('You cannot validate the mime-type of files as the Mime component is not installed. Try running "composer require symfony/mime".');
181+
}
182+
183+
$value = new FileObject($value);
184+
}
185+
186+
$mime = $value->getMimeType();
175187
}
176188

177189
$mimeTypes = (array) $constraint->mimeTypes;
178-
$mime = $value->getMimeType();
179190

180191
foreach ($mimeTypes as $mimeType) {
181192
if ($mimeType === $mime) {

src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php

+14-57
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\HttpFoundation\File\File as FileObject;
1415
use Symfony\Component\HttpFoundation\File\UploadedFile;
1516
use Symfony\Component\Validator\Constraints\File;
1617
use Symfony\Component\Validator\Constraints\FileValidator;
@@ -283,21 +284,10 @@ public function testBinaryFormat($bytesWritten, $limit, $binaryFormat, $sizeAsSt
283284

284285
public function testValidMimeType()
285286
{
286-
$file = $this
287-
->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
288-
->setConstructorArgs([__DIR__.'/Fixtures/foo'])
289-
->getMock();
290-
$file
291-
->expects($this->once())
292-
->method('getPathname')
293-
->willReturn($this->path);
294-
$file
295-
->expects($this->once())
296-
->method('getMimeType')
297-
->willReturn('image/jpg');
287+
$file = new FileObject(__DIR__.'/Fixtures/blank.jpg');
298288

299289
$constraint = new File([
300-
'mimeTypes' => ['image/png', 'image/jpg'],
290+
'mimeTypes' => ['image/png', 'image/jpeg'],
301291
]);
302292

303293
$this->validator->validate($file, $constraint);
@@ -307,18 +297,7 @@ public function testValidMimeType()
307297

308298
public function testValidWildcardMimeType()
309299
{
310-
$file = $this
311-
->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
312-
->setConstructorArgs([__DIR__.'/Fixtures/foo'])
313-
->getMock();
314-
$file
315-
->expects($this->once())
316-
->method('getPathname')
317-
->willReturn($this->path);
318-
$file
319-
->expects($this->once())
320-
->method('getMimeType')
321-
->willReturn('image/jpg');
300+
$file = new FileObject(__DIR__.'/Fixtures/test.gif');
322301

323302
$constraint = new File([
324303
'mimeTypes' => ['image/*'],
@@ -331,62 +310,40 @@ public function testValidWildcardMimeType()
331310

332311
public function testInvalidMimeType()
333312
{
334-
$file = $this
335-
->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
336-
->setConstructorArgs([__DIR__.'/Fixtures/foo'])
337-
->getMock();
338-
$file
339-
->expects($this->once())
340-
->method('getPathname')
341-
->willReturn($this->path);
342-
$file
343-
->expects($this->once())
344-
->method('getMimeType')
345-
->willReturn('application/pdf');
313+
$file = new FileObject(__DIR__.'/Fixtures/blank.pdf');
346314

347315
$constraint = new File([
348-
'mimeTypes' => ['image/png', 'image/jpg'],
316+
'mimeTypes' => ['image/png', 'image/jpeg'],
349317
'mimeTypesMessage' => 'myMessage',
350318
]);
351319

352320
$this->validator->validate($file, $constraint);
353321

354322
$this->buildViolation('myMessage')
355323
->setParameter('{{ type }}', '"application/pdf"')
356-
->setParameter('{{ types }}', '"image/png", "image/jpg"')
357-
->setParameter('{{ file }}', '"'.$this->path.'"')
358-
->setParameter('{{ name }}', '"'.basename($this->path).'"')
324+
->setParameter('{{ types }}', '"image/png", "image/jpeg"')
325+
->setParameter('{{ file }}', '"'.__DIR__.'/Fixtures/blank.pdf'.'"')
326+
->setParameter('{{ name }}', '"blank.pdf"')
359327
->setCode(File::INVALID_MIME_TYPE_ERROR)
360328
->assertRaised();
361329
}
362330

363331
public function testInvalidWildcardMimeType()
364332
{
365-
$file = $this
366-
->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
367-
->setConstructorArgs([__DIR__.'/Fixtures/foo'])
368-
->getMock();
369-
$file
370-
->expects($this->once())
371-
->method('getPathname')
372-
->willReturn($this->path);
373-
$file
374-
->expects($this->once())
375-
->method('getMimeType')
376-
->willReturn('application/pdf');
333+
$file = new FileObject(__DIR__.'/Fixtures/blank.pdf');
377334

378335
$constraint = new File([
379-
'mimeTypes' => ['image/*', 'image/jpg'],
336+
'mimeTypes' => ['image/*', 'image/jpeg'],
380337
'mimeTypesMessage' => 'myMessage',
381338
]);
382339

383340
$this->validator->validate($file, $constraint);
384341

385342
$this->buildViolation('myMessage')
386343
->setParameter('{{ type }}', '"application/pdf"')
387-
->setParameter('{{ types }}', '"image/*", "image/jpg"')
388-
->setParameter('{{ file }}', '"'.$this->path.'"')
389-
->setParameter('{{ name }}', '"'.basename($this->path).'"')
344+
->setParameter('{{ types }}', '"image/*", "image/jpeg"')
345+
->setParameter('{{ file }}', '"'.__DIR__.'/Fixtures/blank.pdf'.'"')
346+
->setParameter('{{ name }}', '"blank.pdf"')
390347
->setCode(File::INVALID_MIME_TYPE_ERROR)
391348
->assertRaised();
392349
}
Binary file not shown.

0 commit comments

Comments
 (0)