Make GCS backend handle webp right, and use gs://

Fixes #164
This commit is contained in:
Andrew Godwin 2022-12-14 21:21:18 -07:00
parent 5d8b8212cc
commit 16005e14ef
2 changed files with 24 additions and 9 deletions

View file

@ -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

View file

@ -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