973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | <script lang="ts"> |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 2 | import { onDestroy } from 'svelte'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 3 | import type { MediaAttachment } from './types'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | import { messageMediaUrl } from './api'; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 5 | import { lightbox } from './lightbox'; |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 6 | import Emoji from './Emoji.svelte'; |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 7 | import EmojiText from './EmojiText.svelte'; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | export let attachment: MediaAttachment; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | const PREVIEWABLE = new Set(['Image', 'Sticker']); |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 12 | const RETRY_DELAYS = [1000, 3000, 10000]; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | // Encrypted media often has no fetchable thumbnail, so request the bounded |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | // full bytes directly instead of flashing a broken thumbnail first. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | $: fullUrl = messageMediaUrl(attachment.id, 'Full'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | $: thumbUrl = messageMediaUrl(attachment.id, 'Thumbnail'); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | $: previewable = PREVIEWABLE.has(attachment.kind); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | let fullFailed = false; |
87336a53e733 | atagen | 2026-10-24 10:37 UTC | 21 | let loaded = false; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 22 | let mediaId: string | null = null; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 23 | let retryCount = 0; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 24 | let retryToken = ''; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 25 | let retryTimer: ReturnType<typeof setTimeout> | null = null; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 27 | $: previewUrl = retryToken ? `${fullUrl}&retry=${retryToken}` : fullUrl; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 28 | $: if (attachment.id !== mediaId) resetPreview(attachment.id); |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 29 | |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 30 | function clearRetry(): void { |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 31 | if (retryTimer !== null) clearTimeout(retryTimer); |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 32 | retryTimer = null; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 33 | } |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 34 | |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 35 | function resetPreview(id: string): void { |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 36 | clearRetry(); |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 37 | mediaId = id; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 38 | retryCount = 0; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 39 | retryToken = ''; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 40 | fullFailed = false; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 41 | loaded = false; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 42 | } |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 43 | |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 44 | function retryPreview(resetBudget = false): void { |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 45 | clearRetry(); |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 46 | if (resetBudget) retryCount = 0; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 47 | retryCount += 1; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 48 | retryToken = `${Date.now()}-${retryCount}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | fullFailed = false; |
87336a53e733 | atagen | 2026-10-24 10:37 UTC | 50 | loaded = false; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 53 | function previewFailed(): void { |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 54 | fullFailed = true; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 55 | loaded = false; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 56 | if (retryTimer !== null || retryCount >= RETRY_DELAYS.length) return; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 57 | retryTimer = setTimeout(() => retryPreview(), RETRY_DELAYS[retryCount]); |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 58 | } |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 59 | |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 60 | onDestroy(clearRetry); |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 61 | |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 62 | function openLightbox(ev: MouseEvent): void { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 63 | if (ev.button !== 0 || ev.ctrlKey || ev.metaKey || ev.shiftKey || ev.altKey) return; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 64 | ev.preventDefault(); |
7cca1d5628d1 | atagen | 2026-10-24 07:36 UTC | 65 | lightbox.set({ src: fullUrl, alt: attachment.caption || attachment.filename, filename: attachment.filename, mxc: attachment.mxc }); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 66 | } |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 67 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | function sizeLabel(): string { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | if (typeof attachment.size_bytes !== 'number') return ''; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | const bytes = attachment.size_bytes; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | if (bytes < 1024) return `${bytes} B`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | function durationLabel(): string { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | if (typeof attachment.duration_ms !== 'number') return ''; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | const totalSeconds = Math.round(attachment.duration_ms / 1000); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | const minutes = Math.floor(totalSeconds / 60); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | const seconds = totalSeconds % 60; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | return `${minutes}:${seconds.toString().padStart(2, '0')}`; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | </script> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 85 | <svelte:window on:online={() => { if (fullFailed) retryPreview(true); }} /> |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 86 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | {#if previewable && !fullFailed} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | <figure class="media-figure"> |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 89 | <a href={fullUrl} target="_blank" rel="noopener noreferrer" title={attachment.filename} on:click={openLightbox}> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | <img |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | class="media-image" |
87336a53e733 | atagen | 2026-10-24 10:37 UTC | 92 | class:loaded |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 93 | src={previewUrl} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | alt={attachment.caption || attachment.filename} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | loading="lazy" |
87336a53e733 | atagen | 2026-10-24 10:37 UTC | 96 | on:load={() => (loaded = true)} |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 97 | on:error={previewFailed} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 98 | /> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 99 | </a> |
dfd1feaae855 | atagen | 2026-10-24 10:54 UTC | 100 | {#if attachment.kind !== 'Sticker'} |
dfd1feaae855 | atagen | 2026-10-24 10:54 UTC | 101 | <figcaption class="media-caption"> |
dfd1feaae855 | atagen | 2026-10-24 10:54 UTC | 102 | {#if attachment.caption && attachment.caption !== attachment.filename} |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 103 | <span><EmojiText text={attachment.caption} /></span> |
dfd1feaae855 | atagen | 2026-10-24 10:54 UTC | 104 | {:else} |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 105 | <span><EmojiText text={attachment.filename} /></span> |
dfd1feaae855 | atagen | 2026-10-24 10:54 UTC | 106 | {/if} |
dfd1feaae855 | atagen | 2026-10-24 10:54 UTC | 107 | {#if sizeLabel()}<span class="media-size"> · {sizeLabel()}</span>{/if} |
dfd1feaae855 | atagen | 2026-10-24 10:54 UTC | 108 | </figcaption> |
dfd1feaae855 | atagen | 2026-10-24 10:54 UTC | 109 | {/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 110 | </figure> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 111 | {:else if previewable && fullFailed} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 112 | <div class="media-file"> |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 113 | <span class="file-icon" aria-hidden="true"><Emoji emoji="🖼️" size={18} /></span> |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 114 | <a href={fullUrl} target="_blank" rel="noopener noreferrer"><EmojiText text={attachment.caption || attachment.filename} /></a> |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 115 | <span class="media-size">{retryTimer !== null ? 'Retrying preview…' : 'Preview unavailable'}</span> |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 116 | <button type="button" class="ghost-btn" on:click={() => retryPreview(true)}>Retry</button> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 117 | </div> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 118 | {:else if attachment.kind === 'Video'} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 119 | <div class="media-block"> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 120 | <!-- svelte-ignore a11y-media-has-caption --> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 121 | <video class="media-video" controls preload="metadata" poster={attachment.has_thumbnail ? thumbUrl : undefined}> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 122 | <source src={fullUrl} type={attachment.mimetype ?? undefined} /> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 123 | Your browser cannot play this video. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 124 | </video> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 125 | <div class="media-caption"> |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 126 | <a href={fullUrl} target="_blank" rel="noopener noreferrer"><EmojiText text={attachment.caption || attachment.filename} /></a> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 127 | {#if durationLabel()}<span class="media-size"> · {durationLabel()}</span>{/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 128 | {#if sizeLabel()}<span class="media-size"> · {sizeLabel()}</span>{/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 129 | </div> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 130 | </div> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 131 | {:else if attachment.kind === 'Audio'} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 132 | <div class="media-block"> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 133 | <!-- svelte-ignore a11y-media-has-caption --> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 134 | <audio class="media-audio" controls preload="metadata" src={fullUrl}></audio> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 135 | <div class="media-caption"> |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 136 | <span><EmojiText text={attachment.caption || attachment.filename} /></span> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 137 | {#if durationLabel()}<span class="media-size"> · {durationLabel()}</span>{/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 138 | {#if sizeLabel()}<span class="media-size"> · {sizeLabel()}</span>{/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 139 | </div> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 140 | </div> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 141 | {:else} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 142 | <div class="media-file"> |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 143 | <span class="file-icon" aria-hidden="true"><Emoji emoji="📎" size={18} /></span> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 144 | <a href={fullUrl} target="_blank" rel="noopener noreferrer" download={attachment.filename}> |
445bf199a9cf | atagen | 2026-10-25 02:56 UTC | 145 | <EmojiText text={attachment.caption || attachment.filename} /> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 146 | </a> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 147 | {#if sizeLabel()}<span class="media-size">{sizeLabel()}</span>{/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 148 | </div> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 149 | {/if} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 150 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 151 | <style> |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 152 | .media-figure { margin: 6px 0 2px; max-width: 420px; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 153 | .media-image { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 154 | display: block; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 155 | max-width: 100%; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 156 | max-height: 320px; |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 157 | border-radius: var(--r-md); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 158 | border: 1px solid var(--border); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 159 | background: var(--bg-soft); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 160 | } |
87336a53e733 | atagen | 2026-10-24 10:37 UTC | 161 | .media-image.loaded { border-color: transparent; background: none; } |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 162 | .media-video { display: block; max-width: 100%; max-height: 320px; border-radius: var(--r-md); background: #000; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 163 | .media-audio { display: block; width: 100%; max-width: 420px; margin-top: 6px; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 164 | .media-block { margin: 6px 0 2px; max-width: 420px; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 165 | .media-caption { font-size: 12px; color: var(--muted); margin-top: 4px; display: flex; gap: 4px; flex-wrap: wrap; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 166 | .media-caption a { color: var(--text); } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 167 | .media-size { opacity: 0.85; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 168 | .media-file { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 169 | display: inline-flex; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 170 | align-items: center; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 171 | gap: 8px; |
e2b04d2f1db0 | atagen | 2026-10-24 23:55 UTC | 172 | flex-wrap: wrap; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 173 | margin: 6px 0 2px; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 174 | padding: 6px 12px; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 175 | border: 1px solid var(--border); |
05a48e9e7942 | atagen | 2026-10-24 09:32 UTC | 176 | border-radius: var(--r-md); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 177 | background: var(--bg-soft); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 178 | max-width: 100%; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 179 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 180 | .media-file a { color: var(--text); overflow-wrap: anywhere; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 181 | .file-icon { font-size: 15px; } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 182 | </style> |