summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Godwin2022-12-14 21:21:18 -0700
committerAndrew Godwin2022-12-14 21:21:18 -0700
commit16005e14ef46c6721f4bf4e01a54e6c1bf0ecdf7 (patch)
tree60046a6b137e3b2602de6542070b6d8192bccb21
parent5d8b8212cc452a09b393d1024c832b464fe03740 (diff)
downloadtakahe-16005e14ef46c6721f4bf4e01a54e6c1bf0ecdf7.tar.gz
takahe-16005e14ef46c6721f4bf4e01a54e6c1bf0ecdf7.tar.bz2
takahe-16005e14ef46c6721f4bf4e01a54e6c1bf0ecdf7.zip
Make GCS backend handle webp right, and use gs://
Fixes #164
-rw-r--r--core/uploads.py18
-rw-r--r--takahe/settings.py13
2 files changed, 23 insertions, 8 deletions
diff --git a/core/uploads.py b/core/uploads.py
index c83888c..41b6e94 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.gcloud import GoogleCloudStorage
from storages.backends.s3boto3 import S3Boto3Storage
@@ -16,11 +17,26 @@ def upload_namer(prefix, instance, filename):
class TakaheS3Storage(S3Boto3Storage):
+ """
+ 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["ContentDisposition"] = "inline"
params["ContentType"] = "image/webp"
+ return params
+
+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"
return params
diff --git a/takahe/settings.py b/takahe/settings.py
index 552dc76..ee3854c 100644
--- a/takahe/settings.py
+++ b/takahe/settings.py
@@ -338,15 +338,14 @@ if SETUP.EMAIL_SERVER:
if SETUP.MEDIA_BACKEND:
parsed = urllib.parse.urlparse(SETUP.MEDIA_BACKEND)
query = urllib.parse.parse_qs(parsed.query)
- if parsed.scheme == "gcs":
- DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
- if parsed.path.lstrip("/"):
- GS_BUCKET_NAME = parsed.path.lstrip("/")
- else:
- GS_BUCKET_NAME = parsed.hostname
+ if parsed.scheme == "gs":
+ DEFAULT_FILE_STORAGE = "core.uploads.TakaheGoogleCloudStorage"
+ GS_BUCKET_NAME = parsed.path.lstrip("/")
GS_QUERYSTRING_AUTH = False
+ if parsed.hostname is not None:
+ port = parsed.port or 443
+ GS_CUSTOM_ENDPOINT = f"https://{parsed.hostname}:{port}"
elif parsed.scheme == "s3":
- # DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
DEFAULT_FILE_STORAGE = "core.uploads.TakaheS3Storage"
AWS_STORAGE_BUCKET_NAME = parsed.path.lstrip("/")
AWS_QUERYSTRING_AUTH = False