diff --git a/core/uploads.py b/core/uploads.py index f6c0e89..2e9125f 100644 --- a/core/uploads.py +++ b/core/uploads.py @@ -1,5 +1,6 @@ import os import secrets +import urllib.parse from typing import TYPE_CHECKING from django.utils import timezone @@ -12,11 +13,20 @@ if TYPE_CHECKING: 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) + + 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) + now = timezone.now() return f"{prefix}/{now.year}/{now.month}/{now.day}/{new_filename}{old_extension}"