Skip to content

Commit b78af31

Browse files
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
2 parents 13edce2 + cd70a52 commit b78af31

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

File/MimeType/FileinfoMimeTypeGuesser.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ public function guess($path)
6363
if (!$finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
6464
return null;
6565
}
66+
$mimeType = $finfo->file($path);
6667

67-
return $finfo->file($path);
68+
if ($mimeType && 0 === (\strlen($mimeType) % 2)) {
69+
$mimeStart = substr($mimeType, 0, \strlen($mimeType) >> 1);
70+
$mimeType = $mimeStart.$mimeStart === $mimeType ? $mimeStart : $mimeType;
71+
}
72+
73+
return $mimeType;
6874
}
6975
}

Tests/File/Fixtures/test.docx

5.92 KB
Binary file not shown.

Tests/File/MimeType/MimeTypeTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ public function testGuessFileWithUnknownExtension()
6060
$this->assertEquals('application/octet-stream', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/.unknownextension'));
6161
}
6262

63+
/**
64+
* @requires PHP 7.0
65+
*/
66+
public function testGuessWithDuplicatedFileType()
67+
{
68+
$this->assertEquals('application/vnd.openxmlformats-officedocument.wordprocessingml.document', MimeTypeGuesser::getInstance()->guess(__DIR__.'/../Fixtures/test.docx'));
69+
}
70+
6371
public function testGuessWithIncorrectPath()
6472
{
6573
$this->expectException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');

0 commit comments

Comments
 (0)