Files
immich-folio/app/api/admin/gallery/route.ts
Ralksta a3a6319c29 release: v0.16.0
See CHANGELOG.md for the full v0.16.0 entry.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: ralksta <ralksta@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Ralf <rohlenbo79@gmail.com>
Co-authored-by: Paul Thies <110185070+ImScheinox@users.noreply.github.com>
Co-authored-by: lancetm714 <108419425+lancetm714@users.noreply.github.com>
2026-09-22 14:12:13 +02:00

62 lines
2.6 KiB
TypeScript

import { NextResponse } from 'next/server';
import { withAdmin } from '@/lib/admin/withAdmin';
import { revalidatePath } from 'next/cache';
import { readGalleryYaml, writeGalleryYaml } from '@/lib/admin/yaml-service';
import { invalidateConfigCache, deriveGallery } from '@/lib/config';
import { immich } from '@/lib/immich';
import type { GalleryYaml } from '@/lib/config/schema';
/** GET: Read current gallery.yaml config. */
export const GET = withAdmin(async () => {
const gallery = await readGalleryYaml();
return NextResponse.json({ gallery: gallery || { hero: [], albums: [], subpages: [] } });
});
/** PUT: Write gallery.yaml config. */
export const PUT = withAdmin(async (request: Request) => {
const body = await request.json().catch(() => null);
if (!body?.gallery) {
return NextResponse.json({ error: 'Missing gallery data' }, { status: 400 });
}
// Validate basic structure
const gallery = body.gallery as GalleryYaml;
if (!Array.isArray(gallery.hero) && typeof gallery.hero !== 'string' && gallery.hero != null) {
return NextResponse.json({ error: 'Invalid hero format' }, { status: 400 });
}
// Run the *real* derivation before writing. getConfig() throws on a gallery
// with no albums and no subpages, a subpage with no name, or a subpage with
// neither albums nor sections — all of which the page builder can produce.
// Writing one used to report "Saved successfully" and then take the public
// site down until someone noticed.
//
// Calling deriveGallery rather than re-checking those conditions here is
// deliberate: a second copy of the rules would drift from the first. So is
// validating before the write rather than rolling back after — a rollback
// leaves a window in which other requests read the broken config.
try {
deriveGallery(gallery);
} catch (err) {
const reason = err instanceof Error ? err.message : 'Invalid gallery structure';
console.warn('[Admin] Rejected an unloadable gallery.yaml:', reason);
return NextResponse.json({ error: reason }, { status: 400 });
}
try {
await writeGalleryYaml(gallery);
invalidateConfigCache();
immich.invalidateAll();
// Revalidate all pages so the homepage picks up new hero images immediately
revalidatePath('/', 'layout');
revalidatePath('/[...path]', 'page');
return NextResponse.json({
success: true,
message: 'Saved successfully. Backup of previous version created.',
});
} catch (err) {
console.error('[Admin] Failed to write gallery.yaml:', err);
return NextResponse.json({ error: 'Failed to save gallery config' }, { status: 500 });
}
});