This should now work

This commit is contained in:
Joachim 2021-06-06 21:12:21 +02:00
parent bbbae9fc9d
commit 1f2c296cfd
4 changed files with 158 additions and 120 deletions

View file

@ -3,6 +3,7 @@ import re
from django.db import models from django.db import models
from model_utils.managers import InheritanceManager from model_utils.managers import InheritanceManager
from imagekit.models import ImageSpecField
from bookwyrm import activitypub from bookwyrm import activitypub
from bookwyrm.settings import DOMAIN, DEFAULT_LANGUAGE from bookwyrm.settings import DOMAIN, DEFAULT_LANGUAGE
@ -87,6 +88,57 @@ class Book(BookDataModel):
objects = InheritanceManager() 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'
)
@property @property
def author_text(self): def author_text(self):
"""format a list of authors""" """format a list of authors"""

View file

@ -185,3 +185,6 @@ USER_AGENT = "%s (BookWyrm/%s; +https://%s/)" % (
VERSION, VERSION,
DOMAIN, DOMAIN,
) )
# Imagekit generated thumbnails
IMAGEKIT_CACHEFILE_DIR = "thumbnails"

View file

@ -5,127 +5,110 @@
{% if book.cover %} {% if book.cover %}
<picture class="cover-container {{ cover_class }}"> <picture class="cover-container {{ cover_class }}">
{% with external_path|yesno:',/images/' as image_path %} {% if external_path %}
<img
class="book-cover"
src="{{ book.cover }}"
itemprop="thumbnailUrl"
alt="{{ book.alt_text|default:'' }}"
>
{% else %}
{% if size_mobile == 'xsmall' %} {% if size_mobile == 'xsmall' %}
{% generateimage 'bw:book:xsmall:webp' source=book.cover as th_xsmall_webp %} <source
{% generateimage 'bw:book:xsmall:jpg' source=book.cover as th_xsmall_jpg %} media="(max-width: 768px)"
<source type="image/webp"
media="(max-width: 768px)" srcset="{{ book.cover_bw_book_xsmall_webp.url }}"
type="image/webp" />
srcset="{{ image_path }}{{ th_xsmall_webp }}" <source
/> media="(max-width: 768px)"
<source type="image/jpg"
media="(max-width: 768px)" srcset="{{ book.cover_bw_book_xsmall_jpg.url }}"
type="image/jpg" />
srcset="{{ image_path }}{{ th_xsmall_jpg }}" {% elif size_mobile == 'small' %}
/> <source
{% elif size_mobile == 'small' %} media="(max-width: 768px)"
{% generateimage 'bw:book:small:webp' source=book.cover as th_small_webp %} type="image/webp"
{% generateimage 'bw:book:small:jpg' source=book.cover as th_small_jpg %} srcset="{{ book.cover_bw_book_small_webp.url }}"
<source />
media="(max-width: 768px)" <source
type="image/webp" media="(max-width: 768px)"
srcset="{{ image_path }}{{ th_small_webp }}" type="image/jpg"
/> srcset="{{ book.cover_bw_book_small_jpg.url }}"
<source />
media="(max-width: 768px)" {% elif size_mobile == 'medium' %}
type="image/jpg" <source
srcset="{{ image_path }}{{ th_small_jpg }}" media="(max-width: 768px)"
/> type="image/webp"
{% elif size_mobile == 'medium' %} srcset="{{ book.cover_bw_book_medium_webp.url }}"
{% generateimage 'bw:book:medium:webp' source=book.cover as th_medium_webp %} />
{% generateimage 'bw:book:medium:jpg' source=book.cover as th_medium_jpg %} <source
<source media="(max-width: 768px)"
media="(max-width: 768px)" type="image/jpg"
type="image/webp" srcset="{{ book.cover_bw_book_medium_jpg.url }}"
srcset="{{ image_path }}{{ th_medium_webp }}" />
/> {% elif size_mobile == 'large' %}
<source <source
media="(max-width: 768px)" media="(max-width: 768px)"
type="image/jpg" type="image/webp"
srcset="{{ image_path }}{{ th_medium_jpg }}" srcset="{{ book.cover_bw_book_large_webp.url }}"
/> />
{% elif size_mobile == 'large' %} <source
{% generateimage 'bw:book:large:webp' source=book.cover as th_large_webp %} media="(max-width: 768px)"
{% generateimage 'bw:book:large:jpg' source=book.cover as th_large_jpg %} type="image/jpg"
<source srcset="{{ book.cover_bw_book_large_jpg.url }}"
media="(max-width: 768px)" />
type="image/webp" {% elif size_mobile == 'xlarge' %}
srcset="{{ image_path }}{{ th_large_webp }}" <source
/> media="(max-width: 768px)"
<source type="image/webp"
media="(max-width: 768px)" srcset="{{ book.cover_bw_book_xlarge_webp.url }}"
type="image/jpg" />
srcset="{{ image_path }}{{ th_large_jpg }}" <source
/> media="(max-width: 768px)"
{% elif size_mobile == 'xlarge' %} type="image/jpg"
{% generateimage 'bw:book:xlarge:webp' source=book.cover as th_xlarge_webp %} srcset="{{ book.cover_bw_book_xlarge_jpg.url }}"
{% generateimage 'bw:book:xlarge:jpg' source=book.cover as th_xlarge_jpg %} />
<source {% elif size_mobile == 'xxlarge' %}
media="(max-width: 768px)" <source
type="image/webp" media="(max-width: 768px)"
srcset="{{ image_path }}{{ th_xlarge_webp }}" type="image/webp"
/> srcset="{{ book.cover_bw_book_xxlarge_webp.url }}"
<source />
media="(max-width: 768px)" <source
type="image/jpg" media="(max-width: 768px)"
srcset="{{ image_path }}{{ th_xlarge_jpg }}" type="image/jpg"
/> srcset="{{ book.cover_bw_book_xxlarge_jpg.url }}"
{% elif size_mobile == 'xxlarge' %} />
{% generateimage 'bw:book:xxlarge:webp' source=book.cover as th_xxlarge_webp %} {% endif %}
{% generateimage 'bw:book:xxlarge:jpg' source=book.cover as th_xxlarge_jpg %}
<source {% if size == 'xsmall' %}
media="(max-width: 768px)" <source type="image/webp" srcset="{{ book.cover_bw_book_xsmall_webp.url }}"/>
type="image/webp" <source type="image/jpg" srcset="{{ book.cover_bw_book_xsmall_jpg.url }}"/>
srcset="{{ image_path }}{{ th_xxlarge_webp }}" {% elif size == 'small' %}
/> <source type="image/webp" srcset="{{ book.cover_bw_book_small_webp.url }}"/>
<source <source type="image/jpg" srcset="{{ book.cover_bw_book_small_jpg.url }}"/>
media="(max-width: 768px)" {% elif size == 'medium' %}
type="image/jpg" <source type="image/webp" srcset="{{ book.cover_bw_book_medium_webp.url }}"/>
srcset="{{ image_path }}{{ th_xxlarge_jpg }}" <source type="image/jpg" srcset="{{ book.cover_bw_book_medium_jpg.url }}"/>
/> {% elif size == 'large' %}
<source type="image/webp" srcset="{{ book.cover_bw_book_large_webp.url }}"/>
<source type="image/jpg" srcset="{{ book.cover_bw_book_large_jpg.url }}"/>
{% elif size == 'xlarge' %}
<source type="image/webp" srcset="{{ book.cover_bw_book_xlarge_webp.url }}"/>
<source type="image/jpg" srcset="{{ book.cover_bw_book_xlarge_jpg.url }}"/>
{% elif size == 'xxlarge' %}
<source type="image/webp" srcset="{{ book.cover_bw_book_xxlarge_webp.url }}"/>
<source type="image/jpg" srcset="{{ book.cover_bw_book_xxlarge_jpg.url }}"/>
{% endif %}
<img
class="book-cover"
src="/images/{{ book.cover }}"
itemprop="thumbnailUrl"
alt="{{ book.alt_text|default:'' }}"
>
{% endif %} {% endif %}
{% if size == 'xsmall' %}
{% generateimage 'bw:book:xsmall:webp' source=book.cover as th_xsmall_webp %}
{% generateimage 'bw:book:xsmall:jpg' source=book.cover as th_xsmall_jpg %}
<source type="image/webp" srcset="{{ image_path }}{{ th_xsmall_webp }}"/>
<source type="image/jpg" srcset="{{ image_path }}{{ th_xsmall_jpg }}"/>
{% elif size == 'small' %}
{% generateimage 'bw:book:small:webp' source=book.cover as th_small_webp %}
{% generateimage 'bw:book:small:jpg' source=book.cover as th_small_jpg %}
<source type="image/webp" srcset="{{ image_path }}{{ th_small_webp }}"/>
<source type="image/jpg" srcset="{{ image_path }}{{ th_small_jpg }}"/>
{% elif size == 'medium' %}
{% generateimage 'bw:book:medium:webp' source=book.cover as th_medium_webp %}
{% generateimage 'bw:book:medium:jpg' source=book.cover as th_medium_jpg %}
<source type="image/webp" srcset="{{ image_path }}{{ th_medium_webp }}"/>
<source type="image/jpg" srcset="{{ image_path }}{{ th_medium_jpg }}"/>
{% elif size == 'large' %}
{% generateimage 'bw:book:large:webp' source=book.cover as th_large_webp %}
{% generateimage 'bw:book:large:jpg' source=book.cover as th_large_jpg %}
<source type="image/webp" srcset="{{ image_path }}{{ th_large_webp }}"/>
<source type="image/jpg" srcset="{{ image_path }}{{ th_large_jpg }}"/>
{% elif size == 'xlarge' %}
{% generateimage 'bw:book:xlarge:webp' source=book.cover as th_xlarge_webp %}
{% generateimage 'bw:book:xlarge:jpg' source=book.cover as th_xlarge_jpg %}
<source type="image/webp" srcset="{{ image_path }}{{ th_xlarge_webp }}"/>
<source type="image/jpg" srcset="{{ image_path }}{{ th_xlarge_jpg }}"/>
{% elif size == 'xxlarge' %}
{% generateimage 'bw:book:xxlarge:webp' source=book.cover as th_xxlarge_webp %}
{% generateimage 'bw:book:xxlarge:jpg' source=book.cover as th_xxlarge_jpg %}
<source type="image/webp" srcset="{{ image_path }}{{ th_xxlarge_webp }}"/>
<source type="image/jpg" srcset="{{ image_path }}{{ th_xxlarge_jpg }}"/>
{% endif %}
<img
class="book-cover"
src="{{ image_path }}{{ book.cover }}"
itemprop="thumbnailUrl"
alt="{{ book.alt_text|default:'' }}"
>
{% endwith %}
</picture> </picture>
{% endif %} {% endif %}

View file

@ -5,7 +5,7 @@ django-model-utils==4.0.0
environs==7.2.0 environs==7.2.0
flower==0.9.4 flower==0.9.4
Markdown==3.3.3 Markdown==3.3.3
Pillow>=7.1.0 Pillow>=8.2.0
psycopg2==2.8.4 psycopg2==2.8.4
pycryptodome==3.9.4 pycryptodome==3.9.4
python-dateutil==2.8.1 python-dateutil==2.8.1