From 08c9bd84d8b8dfcbc1563b1f5f2a13b3d19d1b11 Mon Sep 17 00:00:00 2001 From: wordofglass Date: Tue, 21 Jun 2016 02:30:20 +0200 Subject: [PATCH 1/3] fetchart: testcase for wrong server-side extensions (#2053) --- test/test_art.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/test/test_art.py b/test/test_art.py index f88f2f1cf..58b15d8e5 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -46,7 +46,7 @@ class UseThePlugin(_common.TestCase): class FetchImageTest(UseThePlugin): - URL = 'http://example.com' + URL = 'http://example.com/test.jpg' @responses.activate def run(self, *args, **kwargs): @@ -61,18 +61,23 @@ class FetchImageTest(UseThePlugin): self.dpath = os.path.join(self.temp_dir, b'arttest') self.source = fetchart.RemoteArtSource(logger, self.plugin.config) self.extra = {'maxwidth': 0} + self.candidate = fetchart.Candidate(logger, url=self.URL) def test_invalid_type_returns_none(self): self.mock_response('image/watercolour') - candidate = fetchart.Candidate(logger, url=self.URL) - self.source.fetch_image(candidate, self.extra) - self.assertEqual(candidate.path, None) + self.source.fetch_image(self.candidate, self.extra) + self.assertEqual(self.candidate.path, None) def test_jpeg_type_returns_path(self): self.mock_response('image/jpeg') - candidate = fetchart.Candidate(logger, url=self.URL) - self.source.fetch_image(candidate, self.extra) - self.assertNotEqual(candidate.path, None) + self.source.fetch_image(self.candidate, self.extra) + self.assertNotEqual(self.candidate.path, None) + + def test_extension_set_by_content_type(self): + self.mock_response('image/png') + self.source.fetch_image(self.candidate, self.extra) + self.assertEqual(os.path.splitext(self.candidate.path)[1], b'.png') + self.assertExists(self.candidate.path) class FSArtTest(UseThePlugin): From c4617c2c1fb4b64a1317f73350ebe2170d2262d0 Mon Sep 17 00:00:00 2001 From: wordofglass Date: Tue, 21 Jun 2016 10:57:45 +0200 Subject: [PATCH 2/3] fetchart: do not hardcode the downloaded extension to jpg, fixes #2053 --- beetsplug/fetchart.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 7f40e83f6..e10a35270 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -39,9 +39,11 @@ try: except ImportError: HAVE_ITUNES = False -IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg'] -CONTENT_TYPES = ('image/jpeg', 'image/png') -DOWNLOAD_EXTENSION = '.jpg' +CONTENT_TYPES = { + 'image/jpeg': [b'jpg', b'jpeg'], + 'image/png': [b'png'] +} +IMAGE_EXTENSIONS = [ext for exts in CONTENT_TYPES.values() for ext in exts] class Candidate(object): @@ -227,8 +229,8 @@ class RemoteArtSource(ArtSource): try: with closing(self.request(candidate.url, stream=True, message=u'downloading image')) as resp: - if 'Content-Type' not in resp.headers \ - or resp.headers['Content-Type'] not in CONTENT_TYPES: + ct = resp.headers.get('Content-Type', None) + if ct not in CONTENT_TYPES: self._log.debug( u'not a supported image: {}', resp.headers.get('Content-Type') or u'no content type', @@ -237,7 +239,7 @@ class RemoteArtSource(ArtSource): return # Generate a temporary file with the correct extension. - with NamedTemporaryFile(suffix=DOWNLOAD_EXTENSION, + with NamedTemporaryFile(suffix=b'.' + CONTENT_TYPES[ct][0], delete=False) as fh: for chunk in resp.iter_content(chunk_size=1024): fh.write(chunk) From f07e6265ee59b5410da2678f46974a57d20edcf3 Mon Sep 17 00:00:00 2001 From: wordofglass Date: Wed, 22 Jun 2016 13:02:15 +0200 Subject: [PATCH 3/3] Changelog for the Content-Type based image extension detection --- docs/changelog.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 6f0333582..567927a1d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -34,6 +34,10 @@ Fixes: installations. Thanks to :user:`bismark`. :bug:`2038` * Fix a crash introduced in the previous version when the standard input was connected to a Unix pipe. :bug:`2041` +* :doc:`/plugins/fetchart`: Determine the file extension for downloaded images + based on the ``Content-Type`` header instead of hardcoding it to .jpg. + Reported in :bug:`2053`, which for now it does not fix due to + server-side issues, though. 1.3.18 (May 31, 2016) ---------------------