mirror of
https://github.com/beetbox/beets.git
synced 2025-12-23 09:03:49 +01:00
images: Make sure all jpegs work everywhere
Apply #1545 to a public function used everywhere
This commit is contained in:
parent
3b372c05d6
commit
a0877dc092
4 changed files with 17 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue