When saving gallery/settings via admin, the config cache was invalidated but the Immich album cache (which stores resolved title overrides and descriptions) was not cleared. This meant changes only appeared after the TTL expired or a manual 'Reload' click. Now both gallery and settings saves also call immich.invalidateAll() to flush the album cache, making changes visible on next page load.
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { isAdminAuthenticated, isAdminEnabled } from '@/lib/admin/auth';
|
|
import { readGalleryYaml, writeGalleryYaml } from '@/lib/admin/yaml-service';
|
|
import { invalidateConfigCache } from '@/lib/config';
|
|
import { immich } from '@/lib/immich';
|
|
import type { GalleryYaml } from '@/lib/config/schema';
|
|
|
|
/** GET: Read current gallery.yaml config. */
|
|
export async function GET() {
|
|
if (!isAdminEnabled()) {
|
|
return NextResponse.json({ error: 'Admin not enabled' }, { status: 403 });
|
|
}
|
|
if (!(await isAdminAuthenticated())) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const gallery = await readGalleryYaml();
|
|
return NextResponse.json({ gallery: gallery || { hero: [], albums: [], subpages: [] } });
|
|
}
|
|
|
|
/** PUT: Write gallery.yaml config. */
|
|
export async function PUT(request: Request) {
|
|
if (!isAdminEnabled()) {
|
|
return NextResponse.json({ error: 'Admin not enabled' }, { status: 403 });
|
|
}
|
|
if (!(await isAdminAuthenticated())) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
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 });
|
|
}
|
|
|
|
try {
|
|
await writeGalleryYaml(gallery);
|
|
invalidateConfigCache();
|
|
immich.invalidateAll();
|
|
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 });
|
|
}
|
|
}
|