2022-11-17 16:21:42 +01:00
|
|
|
import os
|
2022-12-03 22:06:55 +01:00
|
|
|
import secrets
|
2022-12-15 08:50:54 +01:00
|
|
|
from typing import TYPE_CHECKING
|
2022-11-17 16:21:42 +01:00
|
|
|
|
|
|
|
from django.utils import timezone
|
2022-12-15 05:21:18 +01:00
|
|
|
from storages.backends.gcloud import GoogleCloudStorage
|
2022-12-14 07:26:19 +01:00
|
|
|
from storages.backends.s3boto3 import S3Boto3Storage
|
2022-11-17 16:21:42 +01:00
|
|
|
|
2022-12-15 08:50:54 +01:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from activities.models import Emoji
|
|
|
|
|
2022-11-17 16:21:42 +01: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 22:06:55 +01:00
|
|
|
new_filename = secrets.token_urlsafe(20)
|
2022-11-17 16:21:42 +01:00
|
|
|
return f"{prefix}/{now.year}/{now.month}/{now.day}/{new_filename}{old_extension}"
|
2022-12-14 07:26:19 +01:00
|
|
|
|
|
|
|
|
2022-12-15 08:50:54 +01:00
|
|
|
def upload_emoji_namer(prefix, instance: "Emoji", filename):
|
|
|
|
"""
|
|
|
|
Names uploaded emoji per domain
|
|
|
|
"""
|
|
|
|
_, old_extension = os.path.splitext(filename)
|
|
|
|
if instance.domain is None:
|
|
|
|
domain = "_default"
|
|
|
|
else:
|
|
|
|
domain = instance.domain.domain
|
|
|
|
return f"{prefix}/{domain}/{instance.shortcode}{old_extension}"
|
|
|
|
|
|
|
|
|
2022-12-14 07:26:19 +01:00
|
|
|
class TakaheS3Storage(S3Boto3Storage):
|
2022-12-15 05:21:18 +01:00
|
|
|
"""
|
|
|
|
Custom override backend that makes webp files store correctly
|
|
|
|
"""
|
|
|
|
|
2022-12-14 07:26:19 +01:00
|
|
|
def get_object_parameters(self, name: str):
|
|
|
|
params = self.object_parameters.copy()
|
|
|
|
if name.endswith(".webp"):
|
|
|
|
params["ContentDisposition"] = "inline"
|
|
|
|
params["ContentType"] = "image/webp"
|
2022-12-15 05:21:18 +01:00
|
|
|
return params
|
|
|
|
|
2022-12-14 07:26:19 +01:00
|
|
|
|
2022-12-15 05:21:18 +01:00
|
|
|
class TakaheGoogleCloudStorage(GoogleCloudStorage):
|
|
|
|
"""
|
|
|
|
Custom override backend that makes webp files store correctly
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_object_parameters(self, name: str):
|
|
|
|
params = self.object_parameters.copy()
|
|
|
|
if name.endswith(".webp"):
|
|
|
|
params["content_disposition"] = "inline"
|
|
|
|
params["content_type"] = "image/webp"
|
2022-12-14 07:26:19 +01:00
|
|
|
return params
|