Opened 8 years ago
Closed 8 years ago
#40017 closed defect (bug) (fixed)
wp_get_image_mime() returns 'application/octet-stream' for non-image files.
Reported by: | blobfolio | Owned by: | joemcgill |
---|---|---|---|
Milestone: | 4.7.4 | Priority: | normal |
Severity: | normal | Version: | 4.7.1 |
Component: | Media | Keywords: | has-patch has-unit-tests fixed-major |
Focuses: | Cc: |
Description
In the newly-added function wp_get_image_mime()
, the following line should be split into two parts:
<?php $mime = image_type_to_mime_type( exif_imagetype( $file ) );
exif_imagetype()
will return FALSE
on failure.
image_type_to_mime_type()
will cast FALSE
as an integer, resulting in a response of "application/octet-stream" even for invalid files.
Some EXIF types are "application/octet-stream", so to differentiate between failures and merely strange formats, it would be better written like:
<?php if ( is_callable( 'exif_imagetype' ) ) { if ( false !== $exif_type = exif_imagetype( $file ) ) { $mime = image_type_to_mime_type( $exif_type ); } else { $mime = false; } }
Attachments (4)
Change History (24)
#1
@
8 years ago
- Keywords has-patch dev-feedback added
Added a patch, tweaked a little bit to match the coding style of nearby lines.
This ticket was mentioned in Slack in #core-media by blobfolio. View the logs.
8 years ago
This ticket was mentioned in Slack in #core-media by blobfolio. View the logs.
8 years ago
#5
@
8 years ago
- Keywords needs-unit-tests added
The patch looks really straightforward, but some tests would be helpful to ensure we really fix the issue.
#6
@
8 years ago
- Keywords dev-feedback removed
Good catch @blobfolio. After chatting on Slack about this, it sounds like we don't need the strict checking on 'false' there and could simplify to ( ! $imagetype )
. Whenever strict checking of bool values makes no difference, I tend to prefer the simpler style.
I also agree with @swissspidy about adding a simple PHPUnit test for this, if practical.
#8
@
8 years ago
Simple unit test, one image, one non-image. Both sources were already present in the test data.
$ phpunit tests/phpunit/tests/functions/wpGetImageMime.php Installing... Running as single site... To run multisite, use -c tests/phpunit/multisite.xml Not running ajax tests. To execute these, use --group ajax. Not running ms-files tests. To execute these, use --group ms-files. Not running external-http tests. To execute these, use --group external-http. PHPUnit 5.4.6 by Sebastian Bergmann and contributors. . 1 / 1 (100%) Time: 728 ms, Memory: 20.00Mb OK (1 test, 2 assertions)
This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.
8 years ago
This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.
8 years ago
This ticket was mentioned in Slack in #core by swissspidy. View the logs.
8 years ago
#15
follow-up:
↓ 17
@
8 years ago
I'd probably split test_wp_get_image_mime()
up into two separate tests with one assertion each.
This ticket was mentioned in Slack in #core-media by swissspidy. View the logs.
8 years ago
#17
in reply to:
↑ 15
@
8 years ago
Replying to swissspidy:
I'd probably split
test_wp_get_image_mime()
up into two separate tests with one assertion each.
I moved the assertions to a data provider function and added GIF, PNG, and misnamed JPEG to the batch. Should make it easier to extend going forward.
Speaking of extensions, image_type_to_mime_type()
is another function that will return inconsistent values depending on the PHP version, so for the time being I've limited the valid extension tests to the main three (which shouldn't vary): JPG, GIF, PNG.
If/when #39963 gets pushed we can add alias-capable unit tests to match exif
responses for formats like BMP (which could be either of image/bmp
or image/x-ms-bmp
).
Implements fix.