mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-22 15:21:01 +00:00
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:
parent
5ba9ed6428
commit
b5895e4d28
1 changed files with 12 additions and 2 deletions
|
@ -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}"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue