clove/cdn
web/src/routes/Preview.svelte
<script lang="ts">
// One file: what it is, links to the raw copy, and a preview where the
// browser can show it. Media loads straight from raw; text goes through
// /api/files so the bucket doesn't need CORS for this site.
import Crumbs from "../lib/Crumbs.svelte";
import Hero from "../lib/Hero.svelte";
import Icon from "../lib/Icon.svelte";
import { apiWrite, copyText, encodePath, fmtSize, type FileEntry } from "../lib/files";
import { navigate } from "../lib/router.svelte";
import { extensionOf, kindOf } from "../../../shared/kinds";
let {
file,
raw,
canEdit,
onchanged,
}: { file: FileEntry; raw: string; canEdit: boolean; onchanged: () => Promise<void> } = $props();
let deleting = $state(false);
let error = $state("");
/** Bigger text files only get the links; nobody wants to scroll that. */
const MAX_TEXT = 512 * 1024;
const name = $derived(file.path.slice(file.path.lastIndexOf("/") + 1));
const ext = $derived(extensionOf(file.path));
const kind = $derived(kindOf(file.path));
const url = $derived(`${raw}/${encodePath(file.path)}`);
// This preview page, as opposed to the raw file.
const page = $derived(`${location.origin}/${encodePath(file.path)}`);
const local = $derived(`/api/files/${encodePath(file.path)}`);
// Back to the folder afterwards; the worker keeps it even if it's now empty.
async function remove() {
if (!confirm(`Delete ${name}? Anything linking to it will break.`)) return;
deleting = true;
error = (await apiWrite("DELETE", local)) ?? "";
if (!error) {
await onchanged();
navigate(`/${encodePath(file.path.slice(0, Math.max(0, file.path.lastIndexOf("/"))))}`);
}
deleting = false;
}
const text = $derived(
kind === "text" && file.size <= MAX_TEXT
? fetch(local, { cache: "no-cache" }).then((r) => {
if (!r.ok) throw new Error(String(r.status));
return r.text();
})
: null,
);
</script>
<svelte:head><title>{name} · CDN</title></svelte:head>
<Hero eyebrow={kind ?? "File"} title={name}>
<p class="lede">{fmtSize(file.size)}{ext ? ` · .${ext}` : ""}</p>
</Hero>
<div class="content">
<Crumbs path={file.path} file />
<div class="actions">
<a class="btn" href={url} target="_blank" rel="noopener">Open raw</a>
<a class="btn" href={local} download={name}>Download</a>
<button class="btn" type="button" title={page} onclick={(e) => copyText(e.currentTarget, page)}>Copy URL</button>
<button class="btn" type="button" title={url} onclick={(e) => copyText(e.currentTarget, url)}>Copy raw</button>
{#if canEdit}
<button class="btn danger" type="button" disabled={deleting} onclick={remove}><Icon name="trash" />Delete</button>
{/if}
{#if error}<span class="status err">{error}</span>{/if}
</div>
<div class="preview">
{#if kind === "image"}
<img src={url} alt={name} />
{:else if kind === "video"}
<!-- svelte-ignore a11y_media_has_caption -->
<video src={url} controls preload="metadata"></video>
{:else if kind === "audio"}
<audio src={url} controls preload="metadata"></audio>
{:else if kind === "pdf"}
<iframe src={url} title={name}></iframe>
{:else if kind === "model"}
<!-- Same-origin URL: three.js fetches it, and raw has no CORS for this site -->
{#await import("../lib/ModelViewer.svelte")}
<p class="empty">Loading viewer…</p>
{:then { default: ModelViewer }}
<ModelViewer src={local} {name} />
{:catch}
<p class="empty">Couldn't load the 3D viewer.</p>
{/await}
{:else if text}
{#await text}
<p class="empty">Loading…</p>
{:then body}
<pre>{body}</pre>
{:catch}
<p class="empty">Couldn't load this file.</p>
{/await}
{:else if kind === "text"}
<p class="empty">Too big to preview here. Open the raw file instead.</p>
{:else}
<p class="empty">No preview for this kind of file.</p>
{/if}
</div>
</div>