Set the content type and disposition of webp files uploaded to S3

This commit is contained in:
Tyler Kennedy 2022-12-14 01:26:19 -05:00 committed by GitHub
parent 3404b155de
commit 0d8b7db272
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 2 deletions

View File

@ -216,6 +216,7 @@ class ImageUpload(FormView):
name=form.cleaned_data.get("description"),
state=PostAttachmentStates.fetched,
)
attachment.file.save(
main_file.name,
main_file,

View File

@ -2,6 +2,7 @@ import os
import secrets
from django.utils import timezone
from storages.backends.s3boto3 import S3Boto3Storage
def upload_namer(prefix, instance, filename):
@ -12,3 +13,14 @@ def upload_namer(prefix, instance, filename):
_, old_extension = os.path.splitext(filename)
new_filename = secrets.token_urlsafe(20)
return f"{prefix}/{now.year}/{now.month}/{now.day}/{new_filename}{old_extension}"
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

View File

@ -1 +1 @@
python-3.11.0
python-3.11.1

View File

@ -346,7 +346,8 @@ if SETUP.MEDIA_BACKEND:
GS_BUCKET_NAME = parsed.hostname
GS_QUERYSTRING_AUTH = False
elif parsed.scheme == "s3":
DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
# DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
DEFAULT_FILE_STORAGE = "core.uploads.TakaheS3Storage"
AWS_STORAGE_BUCKET_NAME = parsed.path.lstrip("/")
AWS_QUERYSTRING_AUTH = False
AWS_DEFAULT_ACL = "public-read"