diff --git a/beets/art.py b/beets/art.py index 4f873fc60..b371c2c95 100644 --- a/beets/art.py +++ b/beets/art.py @@ -22,7 +22,6 @@ from __future__ import division, absolute_import, print_function import subprocess import platform from tempfile import NamedTemporaryFile -import imghdr import os from beets.util import displayable_path, syspath, bytestring_path diff --git a/beets/mediafile.py b/beets/mediafile.py index 6dd7bc816..616587597 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -308,6 +308,17 @@ def _sc_encode(gain, peak): # Cover art and other images. +def _imghdr_what_wrapper(data): + """A wrapper around imghdr.what to account for jpeg files that can only be + identified as such using their magic bytes + See #1545 + See https://github.com/file/file/blob/master/magic/Magdir/jpeg#L12 + """ + # imghdr.what returns none for jpegs with only the magic bytes, so + # _wider_test_jpeg is run in that case. It still returns None if it didn't + # match such a jpeg file. + return imghdr.what(None, h=data) or _wider_test_jpeg(data) + def _wider_test_jpeg(data): """Test for a jpeg file following the UNIX file implementation which @@ -318,14 +329,14 @@ def _wider_test_jpeg(data): return 'jpeg' -def _image_mime_type(data): +def image_mime_type(data): """Return the MIME type of the image data (a bytestring). """ # 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) + kind = _imghdr_what_wrapper(data) if kind in ['gif', 'jpeg', 'png', 'tiff', 'bmp']: return 'image/{0}'.format(kind) elif kind == 'pgm': @@ -394,7 +405,7 @@ class Image(object): @property def mime_type(self): if self.data: - return _image_mime_type(self.data) + return image_mime_type(self.data) @property def type_index(self): diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 4db22397e..27ffa49cb 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -29,7 +29,7 @@ from beets import importer from beets import ui from beets import util from beets import config -from beets.mediafile import _image_mime_type +from beets.mediafile import image_mime_type from beets.util.artresizer import ArtResizer from beets.util import confit from beets.util import syspath, bytestring_path, py3_path @@ -250,7 +250,7 @@ class RemoteArtSource(ArtSource): # server didn't return enough data, i.e. corrupt image return - real_ct = _image_mime_type(header) + real_ct = image_mime_type(header) if real_ct is None: # detection by file magic failed, fall back to the # server-supplied Content-Type diff --git a/test/test_mediafile_edge.py b/test/test_mediafile_edge.py index 4e053192a..d09b11fb8 100644 --- a/test/test_mediafile_edge.py +++ b/test/test_mediafile_edge.py @@ -91,7 +91,7 @@ class EdgeTest(unittest.TestCase): with open(magic_bytes_file, 'rb') as f: jpg_data = f.read() self.assertEqual( - beets.mediafile._image_mime_type(jpg_data), + beets.mediafile.image_mime_type(jpg_data), 'image/jpeg') def test_soundcheck_non_ascii(self):