feat: serve logo and profile images from Appwrite Storage
Bucket site-media (project homelab) holds public/images/{logo.png,
profile.jpg} under stable file ids equal to the filenames, read("any")
so browsers fetch them keyless. src/lib/media.ts builds the /view and
/preview URLs; the profile circle uses preview width=256 (Appwrite resize
44 KB -> 10 KB webp/jpeg, cached on the storage layer with a 45-day
expiry). Each img keeps onerror swap to the public/images/ copy, which
stays in git as upload source and outage fallback.
Importer now upserts media too (size-match skip, delete+recreate on
change). Server-side note: _APP_STORAGE_LIMIT was unset (0), which
made every upload fail 'File size not allowed' and stored 0 in new
buckets' maximumFileSize; set to 30 MiB in .env + recreate, and the
bucket updated to match. Console storage UI verified rendering on
self-hosted 2.7.4 (unlike documentsdb).
This commit is contained in:
1 parent
80577d6404
commit
e7cd4c2231
5 files changed
+93
-3
No files matched your search
@@ -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);
|
||||
@@ -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()
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||
<div class="flex flex-col items-center sm:items-start">
|
||||
<a href="/" class="flex gap-1 w-fit font-semibold text-ink transition-colors duration-300 ease-in-out hover:text-link">
|
||||
<img src="/images/logo.png" alt="Logo" class="size-6" />
|
||||
<img src={mediaUrl("logo.png")} alt="Logo" class="size-6" onerror={mediaFallback("/images/logo.png")} />
|
||||
{SITE.TITLE}
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@@ -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
|
||||
<div class="relative h-full w-full">
|
||||
<div class="absolute left-0 top-1/2 -translate-y-1/2 flex gap-1 font-semibold">
|
||||
<a href="/" class="flex gap-1 text-ink transition-colors duration-300 ease-in-out hover:text-link">
|
||||
<img src='/images/logo.png' alt="Logo" class="size-6" />
|
||||
<img src={mediaUrl("logo.png")} alt="Logo" class="size-6" onerror={mediaFallback("/images/logo.png")} />
|
||||
<div>
|
||||
{SITE.TITLE}
|
||||
</div>
|
||||
|
||||
@@ -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}'`
|
||||
}
|
||||
@@ -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 = [
|
||||
<div class="animate absolute h-full w-full flex items-center justify-center">
|
||||
<div class='relative w-full h-full flex items-center justify-center text-ink'>
|
||||
<div class='p-5 text-center'>
|
||||
<img src='/images/profile.jpg' alt="Quinn Marvin" class="rounded-full w-32 h-32 mx-auto mb-4 border-4 border-edge" />
|
||||
<img src={mediaUrl("profile.jpg", { width: 256 })} alt="Quinn Marvin" class="rounded-full w-32 h-32 mx-auto mb-4 border-4 border-edge" loading="lazy" width="128" height="128" onerror={mediaFallback("/images/profile.jpg")} />
|
||||
<p class='animated text-lg md:text-xl lg:text-2xl font-semibold opacity-75'>
|
||||
Hello, I am Quinn Marvin
|
||||
</p>
|
||||
|
||||
Reference in new issue
Block a user