From: Michael Tremer Date: Sat, 6 Jan 2024 18:29:42 +0000 (+0000) Subject: docs: Deliver images in WEBP format if the browser supports it X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ccfb1584722c29b2209b795e90bb105a1122542c;p=ipfire.org.git docs: Deliver images in WEBP format if the browser supports it Signed-off-by: Michael Tremer --- diff --git a/src/backend/wiki.py b/src/backend/wiki.py index 6bc2cf7a..349a94d0 100644 --- a/src/backend/wiki.py +++ b/src/backend/wiki.py @@ -685,13 +685,16 @@ class File(misc.Object): if res: return bytes(res.data) - async def get_thumbnail(self, size): + async def get_thumbnail(self, size, format=None): assert self.is_bitmap_image() - cache_key = "-".join(( + cache_key = ":".join(( + "wiki", + "thumbnail", self.path, util.normalize(self.filename), self.created_at.isoformat(), + format or "N/A", "%spx" % size, )) @@ -701,7 +704,7 @@ class File(misc.Object): return thumbnail # Generate the thumbnail - thumbnail = util.generate_thumbnail(self.blob, size) + thumbnail = util.generate_thumbnail(self.blob, size, format=format) # Put it into the cache for forever await self.backend.cache.set(cache_key, thumbnail) diff --git a/src/web/docs.py b/src/web/docs.py index 5f200b04..1570a3d4 100644 --- a/src/web/docs.py +++ b/src/web/docs.py @@ -134,11 +134,20 @@ class FileHandler(base.BaseHandler): self.render("docs/files/detail.html", page=page, file=file) return + # Get image size size = self.get_argument_int("s", None) # Check if image should be resized if size and file.is_bitmap_image(): - blob = await file.get_thumbnail(size) + # Send WEBP if the browser supports + if self.browser_accepts("image/webp"): + format = "WEBP" + + # Fall back to the native format + else: + format = None + + blob = await file.get_thumbnail(size, format=format) else: blob = file.blob