Stop saving infinite files. (#208)

Use a consistent name for Identity.icon as a quick fix to stop flooding object storage.
This commit is contained in:
Tyler Kennedy 2022-12-19 05:53:13 -05:00 committed by GitHub
parent 5ba9ed6428
commit b5895e4d28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,6 @@
import os import os
import secrets import secrets
import urllib.parse
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from django.utils import timezone from django.utils import timezone
@ -12,11 +13,20 @@ if TYPE_CHECKING:
def upload_namer(prefix, instance, filename): def upload_namer(prefix, instance, filename):
""" """
Names uploaded images, obscuring their original name with a random UUID. Names uploaded images.
By default, obscures the original name with a random UUID.
""" """
now = timezone.now()
_, old_extension = os.path.splitext(filename) _, old_extension = os.path.splitext(filename)
if prefix == "profile_images":
# If we're saving images for an Identity, we only keep the most recently
# received. Ideally, we should hash the file content and de-duplicate
# but this is the easy and immediate solution.
return f"{prefix}/{urllib.parse.quote(instance.handle)}{old_extension}"
new_filename = secrets.token_urlsafe(20) new_filename = secrets.token_urlsafe(20)
now = timezone.now()
return f"{prefix}/{now.year}/{now.month}/{now.day}/{new_filename}{old_extension}" return f"{prefix}/{now.year}/{now.month}/{now.day}/{new_filename}{old_extension}"