import type { APIRoute } from 'astro'; import { storageStats, runRetention, purgeSources, mirrorAllToNas, rebuildThumbs } from '../../../lib/maintenance'; import { one } from '../../../lib/db'; export const prerender = false; const json = (b: unknown, s = 200) => new Response(JSON.stringify(b), { status: s, headers: { 'Content-Type': 'application/json' } }); export const GET: APIRoute = async () => json({ stats: await storageStats() }); // POST { action: 'retention' | 'purge_sources' } export const POST: APIRoute = async ({ request }) => { const b = await request.json().catch(() => ({})); if (b.action === 'purge_sources') return json(await purgeSources()); if (b.action === 'mirror_all') return json(await mirrorAllToNas()); if (b.action === 'rebuild_thumbs') return json(await rebuildThumbs()); if (b.action === 'retention') { const s = await one<{ retention_days: number | null }>('SELECT retention_days FROM settings WHERE id=1'); if (!s?.retention_days) return json({ error: 'Keine Aufbewahrungsfrist gesetzt.' }, 400); return json(await runRetention(s.retention_days)); } return json({ error: 'Unbekannte Aktion.' }, 400); };