Clean up file before saving

This commit is contained in:
Joachim 2021-11-20 16:59:05 +01:00
parent 40264e372d
commit d74df047c5

View file

@ -11,6 +11,7 @@ from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageColor
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.files.storage import default_storage
from django.db.models import Avg
from bookwyrm import models, settings
@ -325,6 +326,10 @@ def save_and_cleanup(image, instance=None):
uuid = uuid4()
file_name = f"{instance.id}-{uuid}.jpg"
# Clean up old file before saving
if file_name and default_storage.exists(file_name):
default_storage.delete(file_name)
# Save
image.save(image_buffer, format="jpeg", quality=75)