Fixed broken cover import when cover file is empty

This commit is contained in:
Eliot Berriot 2019-09-27 12:27:28 +02:00
parent 3cc28cd729
commit 93b9e14f8c
No known key found for this signature in database
GPG Key ID: DD6965E2476E5C27
2 changed files with 15 additions and 4 deletions

View File

@ -330,6 +330,7 @@ class Album(APIModelMixin):
if data: if data:
extensions = {"image/jpeg": "jpg", "image/png": "png", "image/gif": "gif"} extensions = {"image/jpeg": "jpg", "image/png": "png", "image/gif": "gif"}
extension = extensions.get(data["mimetype"], "jpg") extension = extensions.get(data["mimetype"], "jpg")
f = None
if data.get("content"): if data.get("content"):
# we have to cover itself # we have to cover itself
f = ContentFile(data["content"]) f = ContentFile(data["content"])
@ -349,6 +350,7 @@ class Album(APIModelMixin):
return return
else: else:
f = ContentFile(response.content) f = ContentFile(response.content)
if f:
self.cover.save("{}.{}".format(self.uuid, extension), f, save=False) self.cover.save("{}.{}".format(self.uuid, extension), f, save=False)
self.save(update_fields=["cover"]) self.save(update_fields=["cover"])
return self.cover.file return self.cover.file
@ -357,6 +359,7 @@ class Album(APIModelMixin):
f = ContentFile(image_data) f = ContentFile(image_data)
self.cover.save("{0}.jpg".format(self.mbid), f, save=False) self.cover.save("{0}.jpg".format(self.mbid), f, save=False)
self.save(update_fields=["cover"]) self.save(update_fields=["cover"])
if self.cover:
return self.cover.file return self.cover.file
def __str__(self): def __str__(self):

View File

@ -133,3 +133,11 @@ def test_can_download_image_file_for_album(binary_cover, mocker, factories):
album.save() album.save()
assert album.cover.file.read() == binary_cover 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
)