From 3103d3e0985d8f9feb531c23a543689283dfcbc5 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Wed, 14 Jun 2023 20:37:31 +0200 Subject: [PATCH] Fix #26: Scale images to the correct size and use more efficient image formats --- .gitignore | 1 + src/lib/components/AvatarComponent.svelte | 17 ++- src/lib/components/PostComponent.svelte | 79 ++++++++++++-- src/lib/mastodon/response.ts | 13 +++ src/lib/odesliResponse.ts | 3 + src/lib/server/db.ts | 109 ++++++++++++++++++- src/lib/server/timeline.ts | 122 ++++++++++++++++++++-- 7 files changed, 324 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index ed79676..c2c3b68 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ inventory.yml ansible.cfg avatars/* +thumbnails/* node_modules /build /.svelte-kit diff --git a/src/lib/components/AvatarComponent.svelte b/src/lib/components/AvatarComponent.svelte index c93d977..0575be1 100644 --- a/src/lib/components/AvatarComponent.svelte +++ b/src/lib/components/AvatarComponent.svelte @@ -6,8 +6,23 @@ let sourceSetHtml: string; $: avatarDescription = `Avatar for ${account.acct}`; $: { + // Sort thumbnails by file type. This is important, because the order of the srcset entries matter. + // We need the best format to be first + const formatPriority = new Map([ + ['avif', 0], + ['webp', 1], + ['jpg', 99], + ['jpeg', 99] + ]); + const resizedAvatars = (account.resizedAvatars ?? []).sort((a, b) => { + const extensionA = a.file.split('.').pop() ?? ''; + const extensionB = b.file.split('.').pop() ?? ''; + const prioA = formatPriority.get(extensionA) ?? 3; + const prioB = formatPriority.get(extensionB) ?? 3; + return prioA - prioB; + }); const m = new Map(); - for (const resizedAvatar of account.resizedAvatars ?? []) { + for (const resizedAvatar of resizedAvatars) { const extension = resizedAvatar.file.split('.').pop(); const mime = extension ? `image/${extension}` : 'application/octet-stream'; const sourceSetEntry = `${resizedAvatar.file} ${resizedAvatar.sizeDescriptor}`; diff --git a/src/lib/components/PostComponent.svelte b/src/lib/components/PostComponent.svelte index 2434733..a83578d 100644 --- a/src/lib/components/PostComponent.svelte +++ b/src/lib/components/PostComponent.svelte @@ -1,5 +1,6 @@