From b616e2ea337069c68271472bfc75cb6b4d18faea Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Wed, 25 Feb 2026 14:52:05 -0800 Subject: [PATCH] Blobify images so users can open them easier --- .../components/ContentLinkBlockImage.svelte | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/app/components/ContentLinkBlockImage.svelte b/src/app/components/ContentLinkBlockImage.svelte index ef184cc3..d0cac62c 100644 --- a/src/app/components/ContentLinkBlockImage.svelte +++ b/src/app/components/ContentLinkBlockImage.svelte @@ -26,6 +26,22 @@ const key = getTagValue("decryption-key", meta) const nonce = getTagValue("decryption-nonce", meta) const algorithm = getTagValue("encryption-algorithm", meta) + const mime = getTagValue("m", meta) + const fileName = + getTagValue("filename", meta) || + getTagValue("name", meta) || + decodeURIComponent(new URL(url).pathname.split("/").filter(Boolean).at(-1) || "image") + + const revokeSrc = () => { + if (src.startsWith("blob:")) { + URL.revokeObjectURL(src) + } + } + + const setBlobSrc = (data: Blob | Uint8Array, type?: string) => { + revokeSrc() + src = URL.createObjectURL(new File([data], fileName, type ? {type} : undefined)) + } const onError = once(async () => { // If the image failed to load, try authenticating @@ -36,7 +52,8 @@ const res = await getBlob(server, hash, {authEvent}) if (res.status === 200) { - src = URL.createObjectURL(await res.blob()) + const blob = await res.blob() + setBlobSrc(blob, blob.type || undefined) } else { hasError = true } @@ -57,7 +74,7 @@ const ciphertext = new Uint8Array(await response.arrayBuffer()) const decryptedData = await decryptFile({ciphertext, key, nonce, algorithm}) - src = URL.createObjectURL(new Blob([new Uint8Array(decryptedData)])) + setBlobSrc(new Uint8Array(decryptedData), mime) } } else { src = url @@ -65,7 +82,7 @@ }) onDestroy(() => { - URL.revokeObjectURL(src) + revokeSrc() })