Files
sofa/app/api/backup/[filename]/route.ts
T
jake 73b07f5ff6 Refactor auth session handling and server actions across app
- Overhaul `lib/auth/server.ts` and `lib/auth/session.ts`; update all API
  routes and server actions to use the revised session pattern
- Refactor server actions (settings, titles, watchlist, setup) for
  consistency with new auth layer
- Extract `SetupForm` into its own client component with `useActionState`,
  animated steps, and copyable env snippets
- Move landing page redirect logic into `app/page.tsx`; slim down
  `LandingPage` component
- Add `proxy.ts` for local dev proxying
- Minor cleanup to `NavBar`, `MobileTabBar`, and `TitleCard`
2026-03-06 17:05:49 -05:00

43 lines
1.3 KiB
TypeScript

import path from "node:path";
import { type NextRequest, NextResponse } from "next/server";
import { getSession } from "@/lib/auth/session";
import { getBackupPath } from "@/lib/services/backup";
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ filename: string }> },
) {
const session = await getSession();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
if (session.user.role !== "admin") {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
const { filename } = await params;
// Sanitize to prevent path traversal
const safe = path.basename(filename);
if (!safe || safe !== filename || safe.includes("..")) {
return NextResponse.json({ error: "Invalid filename" }, { status: 400 });
}
const backupPath = await getBackupPath(safe);
if (!backupPath) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
const file = Bun.file(backupPath);
const buffer = await file.arrayBuffer();
return new NextResponse(new Uint8Array(buffer), {
status: 200,
headers: {
"Content-Type": "application/x-sqlite3",
"Content-Disposition": `attachment; filename="${safe}"`,
"Content-Length": String(file.size),
},
});
}