Skip to content

Commit

Permalink
embedart: Preempt wrong-type error in AAC files
Browse files Browse the repository at this point in the history
  • Loading branch information
sampsyo committed May 12, 2015
1 parent 36bf49a commit dff4fea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions beets/art.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand All @@ -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]})


Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/test_embedart.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os.path
import shutil
from mock import patch
import tempfile

from test import _common
from test._common import unittest
Expand Down Expand Up @@ -87,6 +88,22 @@ def test_art_file_missing(self):
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)
Expand Down

0 comments on commit dff4fea

Please sign in to comment.