Skip to content

Commit

Permalink
bug #31084 [HttpFoundation] Make MimeTypeExtensionGuesser case insens…
Browse files Browse the repository at this point in the history
…itive (vermeirentony)

This PR was merged into the 3.4 branch.

Discussion
----------

[HttpFoundation] Make MimeTypeExtensionGuesser case insensitive

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #...   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Some mime types have a camelCase word in them.
The Apache HTTPD project list items are all lower case.
So I suggest making the $mimeType string lowercase while checking the array key.
That way, we can keep the list in sync.

Example: xlsm file mime type is `application/vnd.ms-excel.sheet.macroEnabled.12`
The key that matches the xlsm extension in the `$defaultExtensions` array is `application/vnd.ms-excel.sheet.macroenabled.12`

Example xlsm file:
https://github.com/vermeirentony/xlsm-example

Commits
-------

e294ee6 Make MimeTypeExtensionGuesser case insensitive
  • Loading branch information
nicolas-grekas committed Apr 17, 2019
2 parents 55a21fb + e294ee6 commit 1311324
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Expand Up @@ -808,6 +808,12 @@ class MimeTypeExtensionGuesser implements ExtensionGuesserInterface
*/
public function guess($mimeType)
{
return isset($this->defaultExtensions[$mimeType]) ? $this->defaultExtensions[$mimeType] : null;
if (isset($this->defaultExtensions[$mimeType])) {
return $this->defaultExtensions[$mimeType];
}

$lcMimeType = strtolower($mimeType);

return isset($this->defaultExtensions[$lcMimeType]) ? $this->defaultExtensions[$lcMimeType] : null;
}
}
Binary file not shown.
Expand Up @@ -90,6 +90,19 @@ public function testGuessClientExtensionWithIncorrectMimeType()
$this->assertEquals('jpeg', $file->guessClientExtension());
}

public function testCaseSensitiveMimeType()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/case-sensitive-mime-type.xlsm',
'test.xlsm',
'application/vnd.ms-excel.sheet.macroEnabled.12',
filesize(__DIR__.'/Fixtures/case-sensitive-mime-type.xlsm'),
null
);

$this->assertEquals('xlsm', $file->guessClientExtension());
}

public function testErrorIsOkByDefault()
{
$file = new UploadedFile(
Expand Down

0 comments on commit 1311324

Please sign in to comment.