39 lines
1.7 KiB
TypeScript
39 lines
1.7 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireAdminSession } from "@/lib/admin/auth";
|
|
import { deleteMediaFile, listMediaFiles, saveMediaFile } from "@/lib/admin/media";
|
|
|
|
export async function GET() {
|
|
if (!await requireAdminSession()) return NextResponse.json({ message: "Nicht autorisiert." }, { status: 401 });
|
|
return NextResponse.json({ files: await listMediaFiles() });
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
if (!await requireAdminSession()) return NextResponse.json({ message: "Nicht autorisiert." }, { status: 401 });
|
|
const form = await request.formData();
|
|
const file = form.get("file");
|
|
|
|
if (!(file instanceof File)) {
|
|
return NextResponse.json({ message: "Bitte wählen Sie eine Datei aus." }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const result = await saveMediaFile(file);
|
|
if (result.error) return NextResponse.json({ message: result.error }, { status: 400 });
|
|
} catch {
|
|
return NextResponse.json({ message: "Die Datei konnte nicht gespeichert werden. Bitte Upload-Speicher und Rechte prüfen." }, { status: 500 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Datei erfolgreich hochgeladen.", files: await listMediaFiles() });
|
|
}
|
|
|
|
export async function DELETE(request: Request) {
|
|
if (!await requireAdminSession()) return NextResponse.json({ message: "Nicht autorisiert." }, { status: 401 });
|
|
|
|
const body = await request.json() as { name?: string };
|
|
if (!body.name) return NextResponse.json({ message: "Dateiname fehlt." }, { status: 400 });
|
|
|
|
const result = await deleteMediaFile(body.name);
|
|
if (result.error) return NextResponse.json({ message: result.error }, { status: 400 });
|
|
|
|
return NextResponse.json({ message: "Datei gelöscht.", files: await listMediaFiles() });
|
|
}
|