Tweak to predictable image namer

This commit is contained in:
Andrew Godwin 2022-12-18 23:47:49 +00:00
parent b5895e4d28
commit 5e343d2129
1 changed files with 7 additions and 4 deletions

View File

@ -20,10 +20,13 @@ def upload_namer(prefix, instance, 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}"
# If we're saving images for an Identity, we name predictably as
# avatar images are not considered "secret", and otherwise we'll waste
# space.
handle_safe = urllib.parse.quote(instance.handle)
return (
f"{prefix}/{handle_safe[:2]}/{handle_safe[:4]}/{handle_safe}{old_extension}"
)
new_filename = secrets.token_urlsafe(20)
now = timezone.now()