2022-11-17 15:21:42 +00:00
|
|
|
import os
|
2022-12-03 21:06:55 +00:00
|
|
|
import secrets
|
2022-11-17 15:21:42 +00:00
|
|
|
|
|
|
|
from django.utils import timezone
|
2022-12-14 06:26:19 +00:00
|
|
|
from storages.backends.s3boto3 import S3Boto3Storage
|
2022-11-17 15:21:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def upload_namer(prefix, instance, filename):
|
|
|
|
"""
|
|
|
|
Names uploaded images, obscuring their original name with a random UUID.
|
|
|
|
"""
|
|
|
|
now = timezone.now()
|
|
|
|
_, old_extension = os.path.splitext(filename)
|
2022-12-03 21:06:55 +00:00
|
|
|
new_filename = secrets.token_urlsafe(20)
|
2022-11-17 15:21:42 +00:00
|
|
|
return f"{prefix}/{now.year}/{now.month}/{now.day}/{new_filename}{old_extension}"
|
2022-12-14 06:26:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TakaheS3Storage(S3Boto3Storage):
|
|
|
|
def get_object_parameters(self, name: str):
|
|
|
|
params = self.object_parameters.copy()
|
|
|
|
|
|
|
|
if name.endswith(".webp"):
|
|
|
|
params["ContentDisposition"] = "inline"
|
|
|
|
params["ContentType"] = "image/webp"
|
|
|
|
|
|
|
|
return params
|