diff --git a/beets/mediafile.py b/beets/mediafile.py index 23738d693..b2a72d84c 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -270,24 +270,23 @@ def _sc_encode(gain, peak): # Cover art and other images. -def _wider_test_jpeg(h, f): +def _wider_test_jpeg(data): """Test for a jpeg file following the UNIX file implementation which uses the magic bytes rather than just looking for the bytes b'JFIF' or b'EXIF' at a fixed position. """ - if h[:2] == b'\xff\xd8': + if data[:2] == b'\xff\xd8': return 'jpeg' def _image_mime_type(data): """Return the MIME type of the image data (a bytestring). """ - # imghdr provides access to the list of tests performed on an image to - # determine its mimetype - if not (_wider_test_jpeg in imghdr.tests): - imghdr.tests.append(_wider_test_jpeg) - - kind = imghdr.what(None, h=data) + # This checks for a jpeg file with only the magic bytes (unrecognized by + # imghdr.what). imghdr.what returns none for that type of file, so + # _wider_test_jpeg is run in that case. It still returns None if it didn't + # match such a jpeg file. + kind = imghdr.what(None, h=data) or _wider_test_jpeg(data) if kind in ['gif', 'jpeg', 'png', 'tiff', 'bmp']: return 'image/{0}'.format(kind) elif kind == 'pgm':