forked from mirrors/bookwyrm
Update preview_images.py
This commit is contained in:
parent
6ba605cbd0
commit
37508d2583
1 changed files with 2 additions and 8 deletions
|
@ -11,7 +11,6 @@ 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
|
||||
|
@ -318,14 +317,13 @@ def save_and_cleanup(image, instance=None):
|
|||
if not isinstance(instance, (models.Book, models.User, models.SiteSettings)):
|
||||
return False
|
||||
uuid = uuid4()
|
||||
file_name = f"{instance.id}-{uuid}.jpg"
|
||||
image_buffer = BytesIO()
|
||||
|
||||
try:
|
||||
try:
|
||||
old_path = instance.preview_image.name
|
||||
file_name = instance.preview_image.name
|
||||
except ValueError:
|
||||
old_path = None
|
||||
file_name = f"{instance.id}-{uuid}.jpg"
|
||||
|
||||
# Save
|
||||
image.save(image_buffer, format="jpeg", quality=75)
|
||||
|
@ -345,10 +343,6 @@ def save_and_cleanup(image, instance=None):
|
|||
else:
|
||||
instance.save(update_fields=["preview_image"])
|
||||
|
||||
# Clean up old file after saving
|
||||
if old_path and default_storage.exists(old_path):
|
||||
default_storage.delete(old_path)
|
||||
|
||||
finally:
|
||||
image_buffer.close()
|
||||
return True
|
||||
|
|
Loading…
Reference in a new issue