Skip to content

Commit

Permalink
images: Make sure all jpegs work everywhere
Browse files Browse the repository at this point in the history
Apply #1545 to a public function used everywhere
  • Loading branch information
nathdwek@laptop committed Nov 7, 2016
1 parent 5dd6eed commit 4f6e0d1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 0 additions & 1 deletion beets/art.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import subprocess
import platform
from tempfile import NamedTemporaryFile
import imghdr
import os

from beets.util import displayable_path, syspath, bytestring_path
Expand Down
17 changes: 14 additions & 3 deletions beets/mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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':
Expand Down Expand Up @@ -394,7 +405,7 @@ def __init__(self, data, desc=None, type=None):
@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):
Expand Down
4 changes: 2 additions & 2 deletions beetsplug/fetchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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
Expand Down Expand Up @@ -250,7 +250,7 @@ def fetch_image(self, candidate, extra):
# 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
Expand Down
2 changes: 1 addition & 1 deletion test/test_mediafile_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_only_magic_bytes_jpeg(self):
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):
Expand Down

0 comments on commit 4f6e0d1

Please sign in to comment.