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