bug #37291 [MimeType] Duplicated MimeType due to PHP Bug (juanmrad)

This PR was squashed before being merged into the 3.4 branch.

Discussion
----------

[MimeType] Duplicated MimeType due to PHP Bug

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #... <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

## Issue:

Currently there is a PHP bug https://bugs.php.net/bug.php?id=77784 that is causing several MimeTypes to be given as duplicated:

```
application/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
```
Instead of:
```
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
```

This Patch will fix the Issues by checking if a returned MimeType is duplicated and return appropriate MimeType.

This patch should be reverted if the PHP Bug is ever fixed.

Commits
-------

7cb29c8b4b [MimeType] Duplicated MimeType due to PHP Bug
This commit is contained in:
Nicolas Grekas 2020-06-28 16:49:48 +02:00
commit 6ef3fee863
3 changed files with 15 additions and 1 deletions

View File

@ -63,7 +63,13 @@ class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
if (!$finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
return null;
}
$mimeType = $finfo->file($path);
return $finfo->file($path);
if ($mimeType && 0 === (\strlen($mimeType) % 2)) {
$mimeStart = substr($mimeType, 0, \strlen($mimeType) >> 1);
$mimeType = $mimeStart.$mimeStart === $mimeType ? $mimeStart : $mimeType;
}
return $mimeType;
}
}

View File

@ -60,6 +60,14 @@ class MimeTypeTest extends TestCase
$this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
}
/**
* @requires PHP 7.0
*/
public function testGuessWithDuplicatedFileType()
{
$this->assertEquals('application/vnd.openxmlformats-officedocument.wordprocessingml.document', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.docx'));
}
public function testGuessWithIncorrectPath()
{
$this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');