Files
hobbs-iac/tv app/Hobbs-Family-Organizer-Self-Hosted-v1.0.0/app/api/admin/photos/upload/route.ts
T
jeremy c2e5c1d9dc feat(hobbs-organizer): v1.2.0 photo frame (/photo) with Google Photos shared albums
- /photo full-screen slideshow (crossfade, blurred backdrop or fill, shuffle, date)
- admin Photos tab: add shared-album links (check, sync now, status/stale warnings),
  slideshow options, browser-resized uploads
- 6-hourly album sync scraping the share page (Library API no longer exposes
  user albums), caches 1920px + 64px copies in /data/photos
- /api/health/photos -> 503 when an album has not synced for 48h
- history/restore covers photo settings; min admin password length 6

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E9NnVEico3WvFxLyjy1iW3
2026-11-04 19:18:02 -06:00

45 lines
1.9 KiB
TypeScript

import { json, requireAdmin } from "../../../../_lib/auth";
import { addUpload, isJpeg, removeUpload } from "../../../../_lib/photos";
export const dynamic = "force-dynamic";
/**
* The admin page resizes photos in the browser (max 1920px JPEG plus a 64px thumbnail)
* before sending them, so the server only validates and stores.
*/
export async function POST(request: Request) {
const denied = await requireAdmin(request, { mutating: true });
if (denied) return denied;
let form: FormData;
try {
form = await request.formData();
} catch {
return json({ error: "Expected a photo upload." }, 400);
}
const photo = form.get("photo");
const thumb = form.get("thumb");
if (!(photo instanceof Blob) || !(thumb instanceof Blob)) return json({ error: "Missing photo." }, 400);
if (photo.size > 8 * 1024 * 1024 || thumb.size > 256 * 1024) return json({ error: "Photo is too large." }, 413);
const photoBytes = new Uint8Array(await photo.arrayBuffer());
const thumbBytes = new Uint8Array(await thumb.arrayBuffer());
if (!isJpeg(photoBytes) || !isJpeg(thumbBytes)) return json({ error: "Photos must be JPEG." }, 415);
const takenAt = Number(form.get("takenAt"));
try {
const result = await addUpload(photoBytes, thumbBytes, {
takenAt: Number.isFinite(takenAt) && takenAt > 0 ? takenAt : null,
width: Math.max(0, Math.round(Number(form.get("width")) || 0)),
height: Math.max(0, Math.round(Number(form.get("height")) || 0)),
});
return json({ ok: true, ...result });
} catch (error) {
return json({ error: error instanceof Error ? error.message : "Upload failed." }, 400);
}
}
export async function DELETE(request: Request) {
const denied = await requireAdmin(request, { mutating: true });
if (denied) return denied;
const key = new URL(request.url).searchParams.get("key") || "";
return (await removeUpload(key)) ? json({ ok: true }) : json({ error: "Not found." }, 404);
}