From 0d8b7db2729d94338824c748901637c625c103b0 Mon Sep 17 00:00:00 2001 From: Tyler Kennedy Date: Wed, 14 Dec 2022 01:26:19 -0500 Subject: [PATCH] Set the content type and disposition of webp files uploaded to S3 --- activities/views/compose.py | 1 + core/uploads.py | 12 ++++++++++++ runtime.txt | 2 +- takahe/settings.py | 3 ++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/activities/views/compose.py b/activities/views/compose.py index e9092f7..8e3c96d 100644 --- a/activities/views/compose.py +++ b/activities/views/compose.py @@ -216,6 +216,7 @@ class ImageUpload(FormView): name=form.cleaned_data.get("description"), state=PostAttachmentStates.fetched, ) + attachment.file.save( main_file.name, main_file, diff --git a/core/uploads.py b/core/uploads.py index 0160e2e..c83888c 100644 --- a/core/uploads.py +++ b/core/uploads.py @@ -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 diff --git a/runtime.txt b/runtime.txt index 335156c..4b44813 100644 --- a/runtime.txt +++ b/runtime.txt @@ -1 +1 @@ -python-3.11.0 +python-3.11.1 diff --git a/takahe/settings.py b/takahe/settings.py index a65367a..552dc76 100644 --- a/takahe/settings.py +++ b/takahe/settings.py @@ -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"