Fixed broken cover import when cover file is empty

environments/review-front-927-m6zslj/deployments/2768
Eliot Berriot 2019-09-27 12:27:28 +02:00
rodzic 3cc28cd729
commit 93b9e14f8c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: DD6965E2476E5C27
2 zmienionych plików z 15 dodań i 4 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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
)