embedart: Preempt wrong-type error in AAC files

This commit is contained in:
Adrian Sampson 2015-05-11 17:46:21 -07:00
parent 36bf49aa8d
commit dff4feaec2
3 changed files with 29 additions and 0 deletions

View file

@ -53,6 +53,7 @@ def embed_item(log, item, imagepath, maxwidth=None, itempath=None,
compare_threshold=0, ifempty=False, as_album=False):
"""Embed an image into the item's media file.
"""
# Conditions and filters.
if compare_threshold:
if not check_art_similarity(log, item, imagepath, compare_threshold):
log.info(u'Image not similar; skipping.')
@ -63,12 +64,21 @@ def embed_item(log, item, imagepath, maxwidth=None, itempath=None,
if maxwidth and not as_album:
imagepath = resize_image(log, imagepath, maxwidth)
# Get the `Image` object from the file.
try:
log.debug(u'embedding {0}', displayable_path(imagepath))
image = mediafile_image(imagepath, maxwidth)
except IOError as exc:
log.warning(u'could not read image file: {0}', exc)
return
# Make sure the image kind is safe (some formats only support PNG
# and JPEG).
if image.mime_type not in ('image/jpeg', 'image/png'):
log.info('not embedding image of unsupported type: {}',
image.mime_type)
return
item.try_write(path=itempath, tags={'images': [image]})

View file

@ -13,6 +13,8 @@ Fixes:
and :doc:`/plugins/thumbnails`). :bug:`1448`
* :doc:`/plugins/permissions`: Fix an error with non-ASCII paths. :bug:`1449`
* Fix sorting by paths when case-insensitive. :bug:`1451`
* :doc:`/plugins/embedart`: Avoid an error when trying to embed invalid images
into MPEG-4 files.
1.3.13 (April 24, 2015)

View file

@ -18,6 +18,7 @@ from __future__ import (division, absolute_import, print_function,
import os.path
import shutil
from mock import patch
import tempfile
from test import _common
from test._common import unittest
@ -87,6 +88,22 @@ class EmbedartCliTest(_common.TestCase, TestHelper):
with self.assertRaises(ui.UserError):
self.run_command('embedart', '-f', '/doesnotexist')
def test_embed_non_image_file(self):
album = self.add_album_fixture()
logging.getLogger('beets.embedart').setLevel(logging.DEBUG)
handle, tmp_path = tempfile.mkstemp()
os.write(handle, 'I am not an image.')
os.close(handle)
try:
self.run_command('embedart', '-f', tmp_path)
finally:
os.remove(tmp_path)
mediafile = MediaFile(syspath(album.items()[0].path))
self.assertFalse(mediafile.images) # No image added.
@require_artresizer_compare
def test_reject_different_art(self):
self._setup_data(self.abbey_artpath)