Skip to content

[HttpFoundation] Add mime type guessing by file extension #21985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

[HttpFoundation] Add mime type guessing by file extension #21985

wants to merge 1 commit into from

Conversation

ipf
Copy link

@ipf ipf commented Mar 13, 2017

This adds mime type guessing by file extension.
Sometimes file information is only stored as metadata, so we don't have access
to a file. So, for guessing the mime type this does it just by using a string (i.e. 'jpg).

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets no
License MIT

@Hanmac
Copy link
Contributor

Hanmac commented Mar 13, 2017

i am okay with adding such a "extension" to "mime-type" guesser,
but i would prefer if it can use some system files like that, like the mime.types from apache.

@ipf ipf changed the title [HttpFoundation] Add mime type Guessing by file extension [HttpFoundation] Add mime type guessing by file extension Mar 13, 2017
*
* @var array
*/
protected $defaultExtensions = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private

/**
* {@inheritdoc}
*/
public function guess($extension)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. The interface states that guess() must expect the file path and not only its extension.

@xabbuh
Copy link
Member

xabbuh commented Mar 14, 2017

What about registering this also as an alternative in MimeTypeGuesser that will be used when neither FileinfoMimeTypeGuesser nor FileBinaryMimeTypeGuesser yield a result?

@xabbuh
Copy link
Member

xabbuh commented Mar 14, 2017

Alternatively, instead of inlining the map of extensions to mime types inside the class, we could include the original resources file and allow to pass the file to be used in the constructor (defaulting to the file that is shipped with the HttpFoundation component).

This adds mime type guessing by file extension.
Sometimes file information is only stored as metadata, so we don't
have access to a file. So, for guessing the mime type this does it just
by using a string (i.e. 'jpg).

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | no
| License       | MIT
@ipf
Copy link
Author

ipf commented Mar 14, 2017

Hi @xabbuh, thanks a lot for your reviews. Rebuilding the MimeTypeGuesser is definetly a nice thing, but rebuilding all that is quite a lot of work.
Using the original file is also a great idea, but parsing it on each call may be too much overhead instead of my small and simple solution. What do you think?

@xabbuh
Copy link
Member

xabbuh commented Mar 14, 2017

but rebuilding all that is quite a lot of work

What do you mean with "rebuilding all that"?

Using the original file is also a great idea, but parsing it on each call may be too much overhead instead of my small and simple solution.

This should indeed not happen inside the constructor to avoid overhead for requests that don't make use of the mime type guesser or for cases where one of the other guesser returned a result, but only when the extension based guesser actually does have to perform some work. I don't expect the parsing to be too heavy in these cases. But you may prove me wrong and we should then indeed think about some caching.

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Mar 14, 2017
*/
public function guess($extension)
{
return isset($this->defaultExtensions[$extension]) ? $this->defaultExtensions[$extension] : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it worth it, but PNG, JPEG, etc are valid extensions, what about lowercase the extension before use it?

*/
public function testMimeTypeGuessing($extension, $mimeType)
{
$result = $this->guesser->guess($extension);
Copy link
Contributor

@dmaicher dmaicher Mar 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be a bit shorter like this? Imho no setup method or member variable needed here?

$result = (new MimeTypeByExtensionGuesser())->guess($extension);

/**
* @dataProvider extensionDataProvider
*
* @param $extension
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless without any type info

@nicolas-grekas
Copy link
Member

I agree with @xabbuh about the list: we dont want to maintain it, so we definitely should build it from a reference source. This could happen with a cache warmer I guess.

@nicolas-grekas
Copy link
Member

Moving to 4.1. Rebase on master might be needed, where PHP 7.1 features can be used btw.

@fabpot
Copy link
Member

fabpot commented Mar 31, 2018

The list of extensions is already part of the MimeTypeExtensionGuesser class, so I would extract it from there so that we can reuse it easily. Anyway, any interest in finishing this one? Or should it be closed?

@teohhanhui
Copy link
Contributor

This may not be the right place to raise the issue, but the DX around MIME type guessing is kind of broken. See #15460 (comment) for example.

@nicolas-grekas nicolas-grekas modified the milestones: 4.1, next Apr 20, 2018
@fabpot
Copy link
Member

fabpot commented Jan 16, 2019

Closing in favor of #29896

@fabpot fabpot closed this Jan 16, 2019
fabpot added a commit that referenced this pull request Jan 17, 2019
This PR was squashed before being merged into the 4.3-dev branch (closes #29896).

Discussion
----------

[Mime] Add the component

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #28832 #21985 makes #15460 trivial
| License       | MIT
| Doc PR        | symfony/symfony-docs#10886

This has been on my todo-list for X years :)

Commits
-------

bdca5d9 tweaked code
5268389 [Mime] added freedesktop as a source for mime types
74ca91d [Mime] added the component
d7ee0ec [HttpFoundation] updated File code
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants