From 4c539bc7eb326718bbf41900972eb9b3ed2cb2a1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 5 Oct 2021 08:00:13 -0700 Subject: [PATCH] Fixes image paths in endpoints --- bookwyrm/settings.py | 6 ++++-- bookwyrm/views/wellknown.py | 17 +++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index 39469a7a..b2f90487 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -210,12 +210,13 @@ if USE_S3: AWS_S3_OBJECT_PARAMETERS = {"CacheControl": "max-age=86400"} # S3 Static settings STATIC_LOCATION = "static" - STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/{STATIC_LOCATION}/" + STATIC_URL = f"{PROTOCOL}://{AWS_S3_CUSTOM_DOMAIN}/{STATIC_LOCATION}/" STATICFILES_STORAGE = "bookwyrm.storage_backends.StaticStorage" # S3 Media settings MEDIA_LOCATION = "images" - MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/" + MEDIA_URL = f"{PROTOCOL}://{AWS_S3_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/" MEDIA_FULL_URL = MEDIA_URL + STATIC_FULL_URL = STATIC_URL DEFAULT_FILE_STORAGE = "bookwyrm.storage_backends.ImagesStorage" # I don't know if it's used, but the site crashes without it STATIC_ROOT = os.path.join(BASE_DIR, env("STATIC_ROOT", "static")) @@ -225,4 +226,5 @@ else: STATIC_ROOT = os.path.join(BASE_DIR, env("STATIC_ROOT", "static")) MEDIA_URL = "/images/" MEDIA_FULL_URL = f"{PROTOCOL}://{DOMAIN}{MEDIA_URL}" + STATIC_FULL_URL = f"{PROTOCOL}://{DOMAIN}{STATIC_URL}" MEDIA_ROOT = os.path.join(BASE_DIR, env("MEDIA_ROOT", "images")) diff --git a/bookwyrm/views/wellknown.py b/bookwyrm/views/wellknown.py index 0f860441..de4f7e62 100644 --- a/bookwyrm/views/wellknown.py +++ b/bookwyrm/views/wellknown.py @@ -9,7 +9,7 @@ from django.utils import timezone from django.views.decorators.http import require_GET from bookwyrm import models -from bookwyrm.settings import DOMAIN, VERSION, MEDIA_FULL_URL +from bookwyrm.settings import DOMAIN, VERSION, MEDIA_FULL_URL, STATIC_FULL_URL @require_GET @@ -93,8 +93,7 @@ def instance_info(_): status_count = models.Status.objects.filter(user__local=True, deleted=False).count() site = models.SiteSettings.get() - logo_path = site.logo or "images/logo.png" - logo = f"{MEDIA_FULL_URL}{logo_path}" + logo = get_image_url(site.logo, "logo.png") return JsonResponse( { "uri": DOMAIN, @@ -134,8 +133,14 @@ def host_meta(request): def opensearch(request): """Open Search xml spec""" site = models.SiteSettings.get() - logo_path = site.favicon or "images/favicon.png" - logo = f"{MEDIA_FULL_URL}{logo_path}" + image = get_image_url(site.favicon, "favicon.png") return TemplateResponse( - request, "opensearch.xml", {"image": logo, "DOMAIN": DOMAIN} + request, "opensearch.xml", {"image": image, "DOMAIN": DOMAIN} ) + + +def get_image_url(obj, fallback): + """helper for loading the full path to an image""" + if obj: + return f"{MEDIA_FULL_URL}{obj}" + return f"{STATIC_FULL_URL}images/{fallback}"