feat: add support for local asset URLs in S3 storage backends
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -82,6 +82,7 @@ SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-#g((q@lvnkt(j6)2(gvtn0px)r
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = env_bool('DEBUG', True)
|
||||
S3_ENABLE = env_bool('S3_ENABLE', False)
|
||||
S3_USE_LOCAL_URLS = env_bool('S3_USE_LOCAL_URLS', False)
|
||||
|
||||
ALLOWED_HOSTS = env_list('ALLOWED_HOSTS', [
|
||||
'192.168.1.142',
|
||||
@@ -426,6 +427,4 @@ CELERY_RESULT_SERIALIZER = 'json'
|
||||
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
|
||||
|
||||
USE_X_FORWARDED_HOST = True
|
||||
SECURE_REFERER_POLICY = "strict-origin-when-cross-origin"
|
||||
|
||||
print(f"DEBUG: ALLOWED_HOSTS is {ALLOWED_HOSTS}")
|
||||
SECURE_REFERER_POLICY = "strict-origin-when-cross-origin"
|
||||
@@ -1,8 +1,19 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from django.utils.encoding import iri_to_uri
|
||||
from storages.backends.s3 import S3ManifestStaticStorage, S3Storage
|
||||
|
||||
|
||||
def _use_local_asset_urls() -> bool:
|
||||
return os.getenv('S3_USE_LOCAL_URLS', '').strip().lower() in {'1', 'true', 'yes', 'on'}
|
||||
|
||||
|
||||
def _local_asset_url(prefix: str, name: str) -> str:
|
||||
return iri_to_uri(f'/{prefix}/{name.lstrip("/")}')
|
||||
|
||||
|
||||
class StaticStorage(S3ManifestStaticStorage):
|
||||
location = 'static'
|
||||
default_acl = 'public-read'
|
||||
@@ -12,6 +23,11 @@ class StaticStorage(S3ManifestStaticStorage):
|
||||
'CacheControl': 'public, max-age=31536000, immutable',
|
||||
}
|
||||
|
||||
def url(self, name: str) -> str:
|
||||
if _use_local_asset_urls():
|
||||
return _local_asset_url('static', name)
|
||||
return super().url(name)
|
||||
|
||||
|
||||
class MediaStorage(S3Storage):
|
||||
location = 'media'
|
||||
@@ -20,4 +36,9 @@ class MediaStorage(S3Storage):
|
||||
file_overwrite = False
|
||||
object_parameters = {
|
||||
'CacheControl': 'public, max-age=604800',
|
||||
}
|
||||
}
|
||||
|
||||
def url(self, name: str) -> str:
|
||||
if _use_local_asset_urls():
|
||||
return _local_asset_url('media', name)
|
||||
return super().url(name)
|
||||
Reference in New Issue
Block a user