Typically, if you try to access an image which doesn't exist, you would get a 404 error response.
If you try to access the same missing image through a custom image-styles URL, the image module gives a 500 server error instead.
This makes it harder to diagnose issues with missing images (you can't tell if it's a missing image, an image that can't be handled by the image toolkit, or some other software error).
Steps to reproduce
1. Create a base install of Drupal (with image module enabled).
2. Go to admin/config/media/image-styles/add and create a simple style (e.g. resize to 100x100 pixels).
3. Visit an image URL which doesn't exist: e.g. /sites/default/files/zzzzz.png - observe that the HTTP error code is 404.
4. Visit the same image URL through the image-styles handler: e.g. /sites/default/files/styles/my_custom_style/public/zzzzz.png - observe that the HTTP error code is 500.
Comment | File | Size | Author |
---|---|---|---|
#19 | source_image_exists-1226512-19.patch | 950 bytes | marianov |
#15 | performance-test.php_.txt | 1.05 KB | oriol_e9g |
#15 | ok.txt | 0 bytes | oriol_e9g |
#16 | performance-test-fixed.php_.txt | 1.05 KB | oriol_e9g |
#6 | source_image_exists-1226512-6.patch | 648 bytes | 13rac1 |
Comments
Comment #1
manarth CreditAttribution: manarth commentedPatch for review.
Comment #3
manarth CreditAttribution: manarth commentedAh, testbot, like ye not p0? How about a p1 patch then? :-)
Comment #4
manarth CreditAttribution: manarth commentedComment #6
13rac1 CreditAttribution: 13rac1 commentedTestbot wants git diff patches ;)
Comment #8
marcingy CreditAttribution: marcingy commentedPlease update to 7.8.
Comment #9
13rac1 CreditAttribution: 13rac1 commentedBlast! My patch was against HEAD.
Comment #10
13rac1 CreditAttribution: 13rac1 commented#6: source_image_exists-1226512-6.patch queued for re-testing.
Comment #11
marcingy CreditAttribution: marcingy commentedMakes sense to me.
Comment #12
sunTo my knowledge, file_exists() is way faster than is_file() and has way less unintended side-effects.
Overall, however, this patch could use some more eyes before getting RTBC again.
Comment #13
manarth CreditAttribution: manarth commentedFor me, is_file is more appropriate than file_exists, because file_exists will accept a directory, and of course a directory couldn't be handled by the image module.
I don't think the micro-optimisation of file_exists vs is_file is meaningful in this case, because it's a single call per request, but please chip in if you disagree :-)
Comment #14
oriol_e9gI'm with @manarth file_exists return true with directories and symbolic links
Comment #15
oriol_e9gOnly as a curiosity, IS_FILE is faster than FILE_EXISTS, if the file exists xD
Attached the performance test files.
Comment #16
oriol_e9gComment #17
sunok, checking whether the file is actually a file makes sense.
Two more things though:
1) I'm not sure I understand why this check happens that late in the request in a delivery callback? (which is pretty much one of the very last things being invoked during a request) I would have expected that a fundamental check like this happens at the very beginning of the request (in the page callback) instead. Perhaps this check even exists already, but doesn't work correctly?
2) It looks like this expectation can be easily asserted in a test.
3) Generating and rendering a full-blown 404 HTML page seems like a lot of overhead, given that in 99% of all cases, the output will not be visible (and the browser renders a red X for a missing image instead).
Comment #18
127356 CreditAttribution: 127356 commented#1: 1226512-image-check_source_image_exists.patch queued for re-testing.
Comment #19
marianov CreditAttribution: marianov commentedIt clearly makes sense to issue a 404 instead of a 500 error when the file does not exists.
I also think a call to drupal_not_found() should be added when checking that the style defined and the scheme is valid. As it is right now you get a 200 OK status when you request /sites/default/files/styles/my_custom_style/zzzzz.png (an inexistant file).
Comment #20
onequad CreditAttribution: onequad commentedThe patch looks good and works as expected. Changing the drupal_not_found() to a lighter alternative could be useful as well (the same way status 500 is handled):
Comment #21
webchickThanks! This looks like a sensible fix; however, as far as I can see:
1) We still need tests.
2) We need a fix in D8 before we backport to D7.
Marking needs work.
Comment #22
bas.hr CreditAttribution: bas.hr commentedLooks like this is a duplicate of #927138: Fail image generation with 404 instead of 500, when source file is missing