Re-added webp fallback

This commit is contained in:
BordedDev 2025-05-17 13:55:46 +02:00
parent 1bed47fbf5
commit 53811ca9b2
No known key found for this signature in database
GPG Key ID: C5F495EAE56673BF
2 changed files with 5 additions and 4 deletions
src/snek

View File

@ -181,12 +181,12 @@ def enrich_image_rendering(text):
soup = BeautifulSoup(text, "html.parser")
for element in soup.find_all("img"):
if element.attrs["src"].startswith("/"):
element.attrs["src"] += "?width=240&height=240&format=webp"
element.attrs["src"] += "?width=240&height=240"
picture_template = f'''
<picture>
<source srcset="{element.attrs["src"]}" type="{mimetypes.guess_type(element.attrs["src"])[0]}" />
<source srcset="{element.attrs["src"]}" type="image/webp" />
<img src="{element.attrs["src"]}" title="{element.attrs["src"]}" alt="{element.attrs["src"]}" />
<source srcset="{element.attrs["src"]}&format=webp" type="image/webp" />
<img src="{element.attrs["src"]}&format=png" title="{element.attrs["src"]}" alt="{element.attrs["src"]}" />
</picture>'''
element.replace_with(BeautifulSoup(picture_template, "html.parser"))
return str(soup)

View File

@ -1,5 +1,6 @@
import asyncio
import mimetypes
from os.path import isfile
from PIL import Image
import pillow_heif.HeifImagePlugin
@ -17,7 +18,7 @@ class ChannelAttachmentView(BaseView):
relative_url=relative_path
)
if not channel_attachment:
if not channel_attachment or not isfile(channel_attachment["path"]):
return web.HTTPNotFound()
original_format = mimetypes.guess_type(channel_attachment["path"])[0]