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);
|
||||
Reference in new issue
Block a user