mirror of
https://github.com/bitbaum/solon.git
synced 2026-12-04 05:07:04 +01:00
* feat(photos): serve photographs straight from R2, no image work on the box With NEXT_PUBLIC_MEDIA_BASE set at build time, the app box sends no photograph bytes and resizes nothing; unset, everything is as before. - src/lib/media: self-contained pipeline (no app imports, for a later move into @bitbaum/sitekit alongside Heidi's copy). media:render renders the bundled photographs at 640/960/1280/1920/2560 as AVIF and WebP with sharp and writes a committed manifest; media:upload (dry run by default, idempotent, immutable Cache-Control) sends them to the PHOTOS_S3_* bucket. A custom next/image loader, switched on only when the base is set, names the nearest variant; MediaImage draws AVIF with a WebP fallback and keeps the blur placeholder. ThemedPhoto, the menu feature and the ideas page use it. A test fails while the manifest lags an original. - Place photographs: with a base set, photoUrl points at the bucket object (place-photos/<file>@<width>) instead of /api/place-photo, which stays for the fallback and for PLACE_PHOTOS_VIA_APP=1 while the bucket fills. places:photos:to-r2 is now dry run by default; --apply copies what is only in place_photo_bytes and reads the rest of the shown widths from Commons straight into the bucket. Nothing is dropped. - places:photos:warm checks the bucket first and only falls back to the route for a miss. - .env.example and AGENTS.md document the variables and the order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T * feat(photos): the box's build fills the bucket and switches it on itself Nobody runs manual steps on the box, so `pnpm run build` now starts with scripts/media-ensure.ts. On the box's build (SELF_HOST=1, or MEDIA_ENSURE=1) with PHOTOS_S3_* and MEDIA_PUBLIC_BASE set, it HEADs every manifest variant, renders (sharp, concurrency 1, no cache) and uploads only the missing ones, reads one back through the public base, and only on a 200 with the image's content type writes NEXT_PUBLIC_MEDIA_BASE into .env.production.local (gitignored) for next build. Anything missing or failing is one log line, takes back a base an earlier build wrote, and exits 0: the build never fails because of media. An explicit NEXT_PUBLIC_MEDIA_BASE still overrides. Place photographs stay on /api/place-photo by default (it copies DB-only photographs into the bucket on first request); PLACE_PHOTOS_DIRECT=1 is the later, explicit switch to direct addresses. The decision logic (src/lib/media/ensure.ts) is pure and tested; sharp's settings are shared by media:render and the build. AGENTS.md and .env.example: an operator only gives the bucket a public domain and puts the five variables in /opt/fleet/env. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T --------- Co-authored-by: Claude <noreply@anthropic.com>
101 lines
3.5 KiB
TypeScript
101 lines
3.5 KiB
TypeScript
/**
|
|
* Puts the rendered photograph variants (`pnpm run media:render`) in the R2
|
|
* bucket the PHOTOS_S3_* settings name, under the keys the loader asks for:
|
|
*
|
|
* pnpm run media:upload dry run: lists what would be sent
|
|
* pnpm run media:upload --apply sends it
|
|
*
|
|
* Idempotent: a key already in the bucket is skipped (a changed original has
|
|
* a new hash, so a new key, and nothing is ever overwritten). Every object is
|
|
* sent with its content type and `Cache-Control: public, max-age=31536000,
|
|
* immutable`. Runs wherever the variants were rendered, never on the app box.
|
|
*/
|
|
import { existsSync, readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { ListObjectsV2Command, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
import {
|
|
MEDIA_CONTENT_TYPE,
|
|
MEDIA_FORMATS,
|
|
variantKey,
|
|
type MediaManifest,
|
|
} from "../src/lib/media/variants";
|
|
|
|
const ROOT = process.cwd();
|
|
const OUT = join(ROOT, ".media");
|
|
const MANIFEST = join(ROOT, "src/lib/media/manifest.json");
|
|
export const IMMUTABLE = "public, max-age=31536000, immutable";
|
|
|
|
async function keysUnder(client: S3Client, bucket: string, prefix: string): Promise<Set<string>> {
|
|
const keys = new Set<string>();
|
|
let token: string | undefined;
|
|
do {
|
|
const page = await client.send(
|
|
new ListObjectsV2Command({ Bucket: bucket, Prefix: prefix, ContinuationToken: token }),
|
|
);
|
|
for (const o of page.Contents ?? []) if (o.Key) keys.add(o.Key);
|
|
token = page.IsTruncated ? page.NextContinuationToken : undefined;
|
|
} while (token);
|
|
return keys;
|
|
}
|
|
|
|
async function main() {
|
|
const apply = process.argv.includes("--apply");
|
|
const manifest = JSON.parse(readFileSync(MANIFEST, "utf8")) as MediaManifest;
|
|
const wanted = Object.entries(manifest.photos).flatMap(([name, entry]) =>
|
|
entry.widths.flatMap((width) =>
|
|
MEDIA_FORMATS.map((format) => ({ key: variantKey(name, entry, width, format), format })),
|
|
),
|
|
);
|
|
const missingLocally = wanted.filter((v) => !existsSync(join(OUT, v.key)));
|
|
if (missingLocally.length > 0) {
|
|
console.error(`${missingLocally.length} variants are not rendered: run pnpm run media:render`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const env = process.env;
|
|
const { PHOTOS_S3_ENDPOINT: endpoint, PHOTOS_S3_BUCKET: bucket } = env;
|
|
const accessKeyId = env.PHOTOS_S3_ACCESS_KEY_ID;
|
|
const secretAccessKey = env.PHOTOS_S3_SECRET_ACCESS_KEY;
|
|
if (!endpoint || !bucket || !accessKeyId || !secretAccessKey) {
|
|
console.error(
|
|
"PHOTOS_S3_ENDPOINT, _BUCKET, _ACCESS_KEY_ID and _SECRET_ACCESS_KEY are required",
|
|
);
|
|
process.exit(1);
|
|
}
|
|
const client = new S3Client({
|
|
region: "auto",
|
|
endpoint,
|
|
credentials: { accessKeyId, secretAccessKey },
|
|
});
|
|
const held = await keysUnder(client, bucket, "photos/");
|
|
const todo = wanted.filter((v) => !held.has(v.key));
|
|
let bytes = 0;
|
|
for (const v of todo) {
|
|
const body = readFileSync(join(OUT, v.key));
|
|
bytes += body.length;
|
|
if (!apply) {
|
|
console.log(`would send ${v.key} (${(body.length / 1024).toFixed(0)} kB)`);
|
|
continue;
|
|
}
|
|
await client.send(
|
|
new PutObjectCommand({
|
|
Bucket: bucket,
|
|
Key: v.key,
|
|
Body: body,
|
|
ContentType: MEDIA_CONTENT_TYPE[v.format],
|
|
CacheControl: IMMUTABLE,
|
|
}),
|
|
);
|
|
console.log(`sent ${v.key}`);
|
|
}
|
|
const verb = apply ? "sent" : "would send (dry run; --apply to send)";
|
|
console.log(
|
|
`${wanted.length} variants: already there ${wanted.length - todo.length}, ${verb} ${todo.length}, ${(bytes / 1e6).toFixed(1)} MB`,
|
|
);
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|