From cd163b26a3f6c2751276475072406b397d8355c5 Mon Sep 17 00:00:00 2001 From: Zed Date: Mon, 9 Jan 2023 00:29:59 +0100 Subject: [PATCH] Use webp for thumbnails when available --- src/views/general.nim | 5 ++--- src/views/renderutils.nim | 8 ++++++++ src/views/tweet.nim | 14 +++----------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/views/general.nim b/src/views/general.nim index b18dae5..5e96d02 100644 --- a/src/views/general.nim +++ b/src/views/general.nim @@ -98,9 +98,8 @@ proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc=""; link(rel="preload", type="image/png", href=bannerUrl, `as`="image") for url in images: - let suffix = if "400x400" in url or url.endsWith("placeholder.png"): "" - else: "?name=small" - let preloadUrl = getPicUrl(url & suffix) + let preloadUrl = if "400x400" in url: getPicUrl(url) + else: getSmallPic(url) link(rel="preload", type="image/png", href=preloadUrl, `as`="image") let image = getUrlPrefix(cfg) & getPicUrl(url) diff --git a/src/views/renderutils.nim b/src/views/renderutils.nim index 8e8d530..cdfeb28 100644 --- a/src/views/renderutils.nim +++ b/src/views/renderutils.nim @@ -3,6 +3,14 @@ import strutils, strformat import karax/[karaxdsl, vdom, vstyles] import ".."/[types, utils] +const smallWebp* = "?name=small&format=webp" + +proc getSmallPic*(url: string): string = + result = url + if "?" notin url and not url.endsWith("placeholder.png"): + result &= smallWebp + result = getPicUrl(result) + proc icon*(icon: string; text=""; title=""; class=""; href=""): VNode = var c = "icon-" & icon if class.len > 0: c = &"{c} {class}" diff --git a/src/views/tweet.nim b/src/views/tweet.nim index daad61a..69335fa 100644 --- a/src/views/tweet.nim +++ b/src/views/tweet.nim @@ -7,14 +7,7 @@ import renderutils import ".."/[types, utils, formatters] import general -const - doctype = "\n" - -proc getSmallPic(url: string): string = - result = url - if "?" notin url and not url.endsWith("placeholder.png"): - result &= "?name=small" - result = getPicUrl(result) +const doctype = "\n" proc renderMiniAvatar(user: User; prefs: Prefs): VNode = let url = getPicUrl(user.getUserPic("_mini")) @@ -60,9 +53,8 @@ proc renderAlbum(tweet: Tweet): VNode = tdiv(class="attachment image"): let named = "name=" in photo - orig = photo - small = if named: photo else: photo & "?name=small" - a(href=getOrigPicUrl(orig), class="still-image", target="_blank"): + small = if named: photo else: photo & smallWebp + a(href=getOrigPicUrl(photo), class="still-image", target="_blank"): genImg(small) proc isPlaybackEnabled(prefs: Prefs; playbackType: VideoType): bool =