Skip to content
Snippets Groups Projects
Verified Commit 93b9e14f authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Fixed broken cover import when cover file is empty

parent 3cc28cd7
No related branches found
No related tags found
No related merge requests found
......@@ -330,6 +330,7 @@ class Album(APIModelMixin):
if data:
extensions = {"image/jpeg": "jpg", "image/png": "png", "image/gif": "gif"}
extension = extensions.get(data["mimetype"], "jpg")
f = None
if data.get("content"):
# we have to cover itself
f = ContentFile(data["content"])
......@@ -349,15 +350,17 @@ class Album(APIModelMixin):
return
else:
f = ContentFile(response.content)
self.cover.save("{}.{}".format(self.uuid, extension), f, save=False)
self.save(update_fields=["cover"])
return self.cover.file
if f:
self.cover.save("{}.{}".format(self.uuid, extension), f, save=False)
self.save(update_fields=["cover"])
return self.cover.file
if self.mbid:
image_data = musicbrainz.api.images.get_front(str(self.mbid))
f = ContentFile(image_data)
self.cover.save("{0}.jpg".format(self.mbid), f, save=False)
self.save(update_fields=["cover"])
return self.cover.file
if self.cover:
return self.cover.file
def __str__(self):
return self.title
......
......@@ -133,3 +133,11 @@ def test_can_download_image_file_for_album(binary_cover, mocker, factories):
album.save()
assert album.cover.file.read() == binary_cover
def test_album_get_image_doesnt_crash_with_empty_data(mocker, factories):
album = factories["music.Album"](mbid=None, cover=None)
assert (
album.get_image(data={"content": "", "url": "", "mimetype": "image/png"})
is None
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment