From 2731ea9aa9183e75d2ed05c131e1e87ed0483e66 Mon Sep 17 00:00:00 2001 From: Joachim Date: Mon, 7 Jun 2021 19:52:25 +0200 Subject: [PATCH] Add env var to enable/disable thumbnail generation --- .env.dev.example | 3 + .env.prod.example | 3 + bookwyrm/context_processors.py | 3 +- bookwyrm/models/book.py | 31 ++-- bookwyrm/settings.py | 1 + bookwyrm/templates/snippets/book_cover.html | 179 ++++++++++---------- 6 files changed, 117 insertions(+), 103 deletions(-) diff --git a/.env.dev.example b/.env.dev.example index 2f24378c6..e2f0999ac 100644 --- a/.env.dev.example +++ b/.env.dev.example @@ -50,5 +50,8 @@ EMAIL_HOST_PASSWORD=emailpassword123 EMAIL_USE_TLS=true EMAIL_USE_SSL=false +# Thumbnails Generation +ENABLE_THUMBNAIL_GENERATION=false + # Set this to true when initializing certbot for domain, false when not CERTBOT_INIT=false diff --git a/.env.prod.example b/.env.prod.example index a82499a32..8aa1b514a 100644 --- a/.env.prod.example +++ b/.env.prod.example @@ -50,5 +50,8 @@ EMAIL_HOST_PASSWORD=emailpassword123 EMAIL_USE_TLS=true EMAIL_USE_SSL=false +# Thumbnails Generation +ENABLE_THUMBNAIL_GENERATION=false + # Set this to true when initializing certbot for domain, false when not CERTBOT_INIT=false diff --git a/bookwyrm/context_processors.py b/bookwyrm/context_processors.py index b77c62b02..b95e1c236 100644 --- a/bookwyrm/context_processors.py +++ b/bookwyrm/context_processors.py @@ -1,5 +1,5 @@ """ customize the info available in context for rendering templates """ -from bookwyrm import models +from bookwyrm import models, settings def site_settings(request): # pylint: disable=unused-argument @@ -7,4 +7,5 @@ def site_settings(request): # pylint: disable=unused-argument return { "site": models.SiteSettings.objects.get(), "active_announcements": models.Announcement.active_announcements(), + "enable_thumbnail_generation": settings.ENABLE_THUMBNAIL_GENERATION, } diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 4437d6280..83884f333 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -6,7 +6,7 @@ from model_utils.managers import InheritanceManager from imagekit.models import ImageSpecField from bookwyrm import activitypub -from bookwyrm.settings import DOMAIN, DEFAULT_LANGUAGE +from bookwyrm.settings import DOMAIN, DEFAULT_LANGUAGE, ENABLE_THUMBNAIL_GENERATION from .activitypub_mixin import OrderedCollectionPageMixin, ObjectMixin from .base_model import BookWyrmModel @@ -88,20 +88,21 @@ class Book(BookDataModel): objects = InheritanceManager() - cover_bw_book_xsmall_webp = ImageSpecField(source="cover", id="bw:book:xsmall:webp") - cover_bw_book_xsmall_jpg = ImageSpecField(source="cover", id="bw:book:xsmall:jpg") - cover_bw_book_small_webp = ImageSpecField(source="cover", id="bw:book:small:webp") - cover_bw_book_small_jpg = ImageSpecField(source="cover", id="bw:book:small:jpg") - cover_bw_book_medium_webp = ImageSpecField(source="cover", id="bw:book:medium:webp") - cover_bw_book_medium_jpg = ImageSpecField(source="cover", id="bw:book:medium:jpg") - cover_bw_book_large_webp = ImageSpecField(source="cover", id="bw:book:large:webp") - cover_bw_book_large_jpg = ImageSpecField(source="cover", id="bw:book:large:jpg") - cover_bw_book_xlarge_webp = ImageSpecField(source="cover", id="bw:book:xlarge:webp") - cover_bw_book_xlarge_jpg = ImageSpecField(source="cover", id="bw:book:xlarge:jpg") - cover_bw_book_xxlarge_webp = ImageSpecField( - source="cover", id="bw:book:xxlarge:webp" - ) - cover_bw_book_xxlarge_jpg = ImageSpecField(source="cover", id="bw:book:xxlarge:jpg") + if ENABLE_THUMBNAIL_GENERATION: + cover_bw_book_xsmall_webp = ImageSpecField(source="cover", id="bw:book:xsmall:webp") + cover_bw_book_xsmall_jpg = ImageSpecField(source="cover", id="bw:book:xsmall:jpg") + cover_bw_book_small_webp = ImageSpecField(source="cover", id="bw:book:small:webp") + cover_bw_book_small_jpg = ImageSpecField(source="cover", id="bw:book:small:jpg") + cover_bw_book_medium_webp = ImageSpecField(source="cover", id="bw:book:medium:webp") + cover_bw_book_medium_jpg = ImageSpecField(source="cover", id="bw:book:medium:jpg") + cover_bw_book_large_webp = ImageSpecField(source="cover", id="bw:book:large:webp") + cover_bw_book_large_jpg = ImageSpecField(source="cover", id="bw:book:large:jpg") + cover_bw_book_xlarge_webp = ImageSpecField(source="cover", id="bw:book:xlarge:webp") + cover_bw_book_xlarge_jpg = ImageSpecField(source="cover", id="bw:book:xlarge:jpg") + cover_bw_book_xxlarge_webp = ImageSpecField( + source="cover", id="bw:book:xxlarge:webp" + ) + cover_bw_book_xxlarge_jpg = ImageSpecField(source="cover", id="bw:book:xxlarge:jpg") @property def author_text(self): diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index 58f1d2f78..76fb079f9 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -187,4 +187,5 @@ USER_AGENT = "%s (BookWyrm/%s; +https://%s/)" % ( ) # Imagekit generated thumbnails +ENABLE_THUMBNAIL_GENERATION = env.bool("ENABLE_THUMBNAIL_GENERATION", False) IMAGEKIT_CACHEFILE_DIR = "thumbnails" diff --git a/bookwyrm/templates/snippets/book_cover.html b/bookwyrm/templates/snippets/book_cover.html index cf133e598..1e2795805 100644 --- a/bookwyrm/templates/snippets/book_cover.html +++ b/bookwyrm/templates/snippets/book_cover.html @@ -1,6 +1,7 @@ {% spaceless %} {% load i18n %} +{% load static %} {% load imagekit %} {% if book.cover %} @@ -14,97 +15,101 @@ > {% else %} - {% if size_mobile == 'xsmall' %} - - - {% elif size_mobile == 'small' %} - - - {% elif size_mobile == 'medium' %} - - - {% elif size_mobile == 'large' %} - - - {% elif size_mobile == 'xlarge' %} - - - {% elif size_mobile == 'xxlarge' %} - - - {% endif %} + {% if enable_thumbnail_generation %} + + {% if size_mobile == 'xsmall' %} + + + {% elif size_mobile == 'small' %} + + + {% elif size_mobile == 'medium' %} + + + {% elif size_mobile == 'large' %} + + + {% elif size_mobile == 'xlarge' %} + + + {% elif size_mobile == 'xxlarge' %} + + + {% endif %} + + {% if size == 'xsmall' %} + + + {% elif size == 'small' %} + + + {% elif size == 'medium' %} + + + {% elif size == 'large' %} + + + {% elif size == 'xlarge' %} + + + {% elif size == 'xxlarge' %} + + + {% endif %} - {% if size == 'xsmall' %} - - - {% elif size == 'small' %} - - - {% elif size == 'medium' %} - - - {% elif size == 'large' %} - - - {% elif size == 'xlarge' %} - - - {% elif size == 'xxlarge' %} - - {% endif %} {{ book.alt_text|default:'' }} @@ -116,7 +121,7 @@
{% trans