Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
ipf wants to merge1 commit intosymfony:masterfromipf:mime
Closed

Conversation

@ipf
Copy link

@ipfipf commentedMar 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).

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsno
LicenseMIT

hason, Nek-, and r-sugawara0394 reacted with thumbs up emoji
@Hanmac
Copy link
Contributor

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.

@ipfipf changed the title[HttpFoundation] Add mime type Guessing by file extension[HttpFoundation] Add mime type guessing by file extensionMar 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 thatguess() must expect the file path and not only its extension.

@xabbuh
Copy link
Member

What about registering this also as an alternative inMimeTypeGuesser that will be used when neitherFileinfoMimeTypeGuesser norFileBinaryMimeTypeGuesser yield a result?

@xabbuh
Copy link
Member

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).

teohhanhui reacted with thumbs up emoji

This adds mime type guessing by file extension.Sometimes file information is only stored as metadata, so we don'thave access to a file. So, for guessing the mime type this does it justby 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 commentedMar 14, 2017

Hi@xabbuh, thanks a lot for your reviews. Rebuilding theMimeTypeGuesser 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

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-grekasnicolas-grekas added this to the3.x milestoneMar 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?

javiereguiluz, Nek-, and sstok reacted with thumbs up emoji
*/
public function testMimeTypeGuessing($extension, $mimeType)
{
$result = $this->guesser->guess($extension);
Copy link
Contributor

@dmaicherdmaicherMar 14, 2017
edited
Loading

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 = (newMimeTypeByExtensionGuesser())->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

The list of extensions is already part of theMimeTypeExtensionGuesser 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-grekasnicolas-grekas modified the milestones:4.1,nextApr 20, 2018
@fabpotfabpot mentioned this pull requestJan 15, 2019
@fabpot
Copy link
Member

Closing in favor of#29896

@fabpotfabpot closed thisJan 16, 2019
fabpot added a commit that referenced this pull requestJan 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     <!-- seehttps://symfony.com/bc -->| Deprecations? | yes| Tests pass?   | yes| Fixed tickets |#28832#21985 makes#15460 trivial| License       | MIT| Doc PR        |symfony/symfony-docs#10886This has been on my todo-list for X years :)Commits-------bdca5d9 tweaked code5268389 [Mime] added freedesktop as a source for mime types74ca91d [Mime] added the componentd7ee0ec [HttpFoundation] updated File code
@nicolas-grekasnicolas-grekas modified the milestones:next,4.3Apr 30, 2019
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@xabbuhxabbuhxabbuh left review comments

+2 more reviewers

@dostendostendosten left review comments

@dmaicherdmaicherdmaicher left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.3

Development

Successfully merging this pull request may close these issues.

9 participants

@ipf@Hanmac@xabbuh@nicolas-grekas@fabpot@teohhanhui@dosten@dmaicher@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp