Files
0pt/app/api/uploads/route.ts
T
adminandClaude Sonnet 4.6 5ba71f59a1 fix: add GET handler to /api/uploads returning 501 when R2 not configured
Missing GET caused a 405 → r.json() threw → error state shown instead of
the calm 'storage not connected' info note. Now returns 501+[] so the page
properly shows storageOff and 'No files yet.' empty state.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-25 19:42:30 +02:00

26 lines
1.1 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
// GET /api/uploads — list uploaded files. Returns 501 until storage is configured.
export async function GET() {
const haveR2 = process.env.R2_ACCESS_KEY_ID && process.env.R2_BUCKET && process.env.R2_ENDPOINT;
if (!haveR2) {
return NextResponse.json([], { status: 501 });
}
return NextResponse.json([]);
}
// POST /api/uploads (docs/04) — issue a presigned R2 PUT URL. Env-guarded:
// returns 501 until R2 storage is configured. Client then PUTs the file directly.
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => ({}));
const haveR2 = process.env.R2_ACCESS_KEY_ID && process.env.R2_BUCKET && process.env.R2_ENDPOINT;
if (!haveR2) {
return NextResponse.json(
{ error: { code: "storage_not_configured", message: "Filuppladdning aktiveras när R2/Blob är kopplat" } },
{ status: 501 },
);
}
// With R2 configured: build a presigned PUT (S3-compatible) and return it.
return NextResponse.json({ file_id: crypto.randomUUID(), put_url: null, name: body?.name ?? null });
}