diff --git a/scripts/appwrite-migrate.mjs b/scripts/appwrite-migrate.mjs index b761290..5576183 100644 --- a/scripts/appwrite-migrate.mjs +++ b/scripts/appwrite-migrate.mjs @@ -113,5 +113,43 @@ for (const coll of COLLECTIONS) { } } } + +// --- media (public/images/*.{png,jpg,jpeg,webp}) -------------------------- +// Files are upserted under stable IDs equal to the filename, so a re-import +// replaces bytes rather than piling up versions. The bucket (`site-media`) +// must exist with read("any") and a non-zero maximumFileSize; create it once +// from the console or the API. +if (!only) { + const BUCKET = process.env.APPWRITE_BUCKET_ID ?? "site-media"; + const imgDir = path.resolve(import.meta.dirname, "..", "public", "images"); + const images = (await readdir(imgDir)).filter((f) => /\.(png|jpe?g|webp)$/i.test(f)).sort(); + for (const file of images) { + const buf = await readFile(path.join(imgDir, file)); + const existing = await api("GET", `${ENDPOINT}/storage/buckets/${BUCKET}/files/${encodeURIComponent(file)}`); + if (existing.status === 200 && existing.json.sizeOriginal === buf.length) { + console.log(`media/${file}: unchanged`); + continue; + } + if (existing.status === 200) { + // No content-replace endpoint: delete, then re-create with the same id. + await api("DELETE", `${ENDPOINT}/storage/buckets/${BUCKET}/files/${encodeURIComponent(file)}`); + } + const form = new FormData(); + form.append("fileId", file); + form.append("fileUpload", new Blob([buf]), file); + const res = await fetch(`${ENDPOINT}/storage/buckets/${BUCKET}/files`, { + method: "POST", + headers: { "x-appwrite-key": KEY, "x-appwrite-project": PROJECT }, + body: form, + }); + if (res.status === 201) { + console.log(`media/${file}: uploaded (${buf.length} bytes)`); + } else { + failed++; + console.error(`media/${file}: ${res.status} ${(await res.text()).slice(0, 120)}`); + } + } +} + console.log(`\ncreated=${created} updated=${updated} failed=${failed}`); process.exit(failed ? 1 : 0); diff --git a/src/components/Footer.astro b/src/components/Footer.astro index 429147c..8b94d02 100644 --- a/src/components/Footer.astro +++ b/src/components/Footer.astro @@ -2,6 +2,7 @@ import { SITE, SOCIALS } from "@consts" import Container from "@components/Container.astro" import SystemStatus from "@components/SystemStatus.astro" +import { mediaUrl, mediaFallback } from "@lib/media" import { getSystemStatus } from "@lib/status" // Rendered server-side so the light is correct in the first paint (and with JS @@ -32,7 +33,7 @@ const statusSnapshot = await getSystemStatus()
- Logo + Logo {SITE.TITLE}
diff --git a/src/components/Header.astro b/src/components/Header.astro index bc2657b..ac17382 100644 --- a/src/components/Header.astro +++ b/src/components/Header.astro @@ -1,6 +1,7 @@ --- import { SITE, LINKS, RESUME } from "@consts" import { cn, ACTIVE_NAV_COLORS, isNavActive } from "@lib/utils" +import { mediaUrl, mediaFallback } from "@lib/media" import Container from "@components/Container.astro" const { pathname } = Astro.url --- @@ -14,7 +15,7 @@ const { pathname } = Astro.url
- Logo + Logo
{SITE.TITLE}
diff --git a/src/lib/media.ts b/src/lib/media.ts new file mode 100644 index 0000000..fb446e3 --- /dev/null +++ b/src/lib/media.ts @@ -0,0 +1,49 @@ +/** + * Browser-facing URLs for images stored in Appwrite Storage + * (bucket `site-media`, project `homelab`, files with stable IDs). + * + * The files themselves are uploaded by scripts/appwrite-migrate.mjs and are + * readable by anyone (bucket grants read("any")), so the only thing a page + * needs is the URL. Because the URLs are absolute and public, they must point + * at the hostnames a *browser* can reach — not at the internal endpoint the + * server SDK uses (inside Appwrite Sites that is http://appwrite). Override + * with APPWRITE_MEDIA_URL if the instance is ever renamed. + * + * /view serves the original bytes (Appwrite re-encodes png to webp for free); + * /preview resizes/re-encodes on demand and caches the result on disk — used + * for the profile circle, which renders at 128 CSS px (256 for retina) and + * would otherwise ship a 44 KB original for a 10 KB job. + * + * Server-rendered .astro components only: reads process.env, so importing + * this from a client island would bake in nothing. + */ + +const MEDIA_BASE = + process.env.APPWRITE_MEDIA_URL ?? "https://appwrite.spaghet.io/v1/storage/buckets/site-media/files" +const PROJECT = process.env.APPWRITE_PROJECT_ID ?? "homelab" + +export type MediaOptions = { + /** Target width in CSS px; doubles for retina use, e.g. 256 for a 128px circle. */ + width?: number + /** Preview quality 0-100 (default 80). Ignored without width. */ + quality?: number +} + +export function mediaUrl(file: string, opts: MediaOptions = {}): string { + const params = new URLSearchParams({ project: PROJECT }) + if (opts.width) { + params.set("width", String(opts.width)) + params.set("quality", String(opts.quality ?? 80)) + return `${MEDIA_BASE}/${encodeURIComponent(file)}/preview?${params}` + } + return `${MEDIA_BASE}/${encodeURIComponent(file)}/view?${params}` +} + +/** + * Inline onerror handler that swaps to the repo copy under public/images/ if + * Appwrite storage is ever unreachable. The local files stay in git as the + * backup and the upload source; this keeps them as the visible fallback too. + */ +export function mediaFallback(localPath: string): string { + return `this.onerror=null;this.src='${localPath}'` +} diff --git a/src/pages/index.astro b/src/pages/index.astro index bd041da..85f1641 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -5,6 +5,7 @@ import StackCard from "@components/StackCard.astro" import { SITE, SOCIALS } from "@consts" import { toSearchEntry } from "@lib/content" import { listSlimContent } from "@lib/content-source" +import { mediaUrl, mediaFallback } from "@lib/media" import ParmesanFlecks from "@components/ParmesanFlecks.astro" import MeatballShower from "@components/MeatballShower.astro" @@ -85,7 +86,7 @@ const stack = [
- Quinn Marvin + Quinn Marvin

Hello, I am Quinn Marvin