From dff4feaec2aaf2b1cc18164c34f6e3d8993c5e84 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 11 May 2015 17:46:21 -0700 Subject: [PATCH] embedart: Preempt wrong-type error in AAC files --- beets/art.py | 10 ++++++++++ docs/changelog.rst | 2 ++ test/test_embedart.py | 17 +++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/beets/art.py b/beets/art.py index c0c4a2b962..8b77899fbc 100644 --- a/beets/art.py +++ b/beets/art.py @@ -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]}) diff --git a/docs/changelog.rst b/docs/changelog.rst index 72e60b0918..4488e821b9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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) diff --git a/test/test_embedart.py b/test/test_embedart.py index 31187d0cc0..20de295005 100644 --- a/test/test_embedart.py +++ b/test/test_embedart.py @@ -18,6 +18,7 @@ import os.path import shutil from mock import patch +import tempfile from test import _common from test._common import unittest @@ -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)