diff --git a/beets/art.py b/beets/art.py index 66eec88a7b..cc23e420cb 100644 --- a/beets/art.py +++ b/beets/art.py @@ -193,7 +193,7 @@ def extract(log, outpath, item): return # Add an extension to the filename. - ext = imghdr.what(None, h=art) + ext = mediafile.image_extension(art) if not ext: log.warning(u'Unknown image type in {0}.', displayable_path(item.path)) diff --git a/beets/mediafile.py b/beets/mediafile.py index 6165875971..3faf2f60ea 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -59,6 +59,7 @@ from beets import logging from beets.util import displayable_path, syspath, as_string +from beets.util.collections import IdentityUnlessDict import six @@ -81,6 +82,8 @@ 'aiff': 'AIFF', } +PREFERRED_IMAGE_EXTENSIONS = IdentityUnlessDict({'jpeg': 'jpg'}) + # Exceptions. @@ -351,6 +354,10 @@ def image_mime_type(data): return 'image/x-{0}'.format(kind) +def image_extension(data): + return PREFERRED_IMAGE_EXTENSIONS[_imghdr_what_wrapper(data)] + + class ImageType(enum.Enum): """Indicates the kind of an `Image` stored in a file's tag. """ diff --git a/beets/util/collections.py b/beets/util/collections.py new file mode 100644 index 0000000000..dad49a4a40 --- /dev/null +++ b/beets/util/collections.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# This file is part of beets. +# Copyright 2016, Adrian Sampson. +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +"""Custom collections classes +""" + + +class IdentityUnlessDict(dict): + """A dictionary which is "transparent" (maps keys to themselves) for all + keys not in it. + """ + def __getitem__(self, key): + try: + return dict.__getitem__(self, key) + except KeyError: + return key