Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.
TypeScript ai-agentai-resume-analyzerapplication-trackerapplication-tracking-systematscareerjob-applicationjob-automationjob-huntingjob-searchjob-seeker-toolsjob-trackerjob-trackingjobsearchjobseekersmcpollamaresumeresume-reviewself-hosted
feat: add backup preflight, import, snapshot list and rollback routes
Khuram Niazf518a9d4ce1a
diff --git a/src/app/api/backup/import/preflight/route.ts b/src/app/api/backup/import/preflight/route.tsnew file mode 100644index 0000000..d9538ce--- /dev/null+++ b/src/app/api/backup/import/preflight/route.ts@@ -0,0 +1,47 @@+import { auth } from "@/auth";+import { NextRequest, NextResponse } from "next/server";+import { APP_CONSTANTS } from "@/lib/constants";+import { BackupError, preflightBackup } from "@/lib/backup";+import { isOverUploadCap } from "@/lib/backup/upload";++export async function POST(req: NextRequest) {+ const session = await auth();+ if (!session?.user?.id) {+ return NextResponse.json({ error: "Not authenticated" }, { status: 401 });+ }++ if (isOverUploadCap(req)) {+ return NextResponse.json(+ { error: "That file is larger than the backup size limit." },+ { status: 413 },+ );+ }++ try {+ const formData = await req.formData();+ const file = formData.get("file");+ if (!(file instanceof File) || file.size === 0) {+ return NextResponse.json({ error: "No backup file uploaded" }, { status: 400 });+ }+ if (file.size > APP_CONSTANTS.BACKUP_MAX_UPLOAD_BYTES) {+ return NextResponse.json(+ { error: "That file is larger than the backup size limit." },+ { status: 413 },+ );+ }++ const bytes = Buffer.from(await file.arrayBuffer());+ const result = await preflightBackup(+ bytes,+ session.user.id,+ session.user.email ?? "",+ );+ return NextResponse.json(result, { status: 200 });+ } catch (error) {+ if (error instanceof BackupError) {+ return NextResponse.json({ error: error.userMessage }, { status: 400 });+ }+ console.error("[Backup] Preflight failed:", error);+ return NextResponse.json({ error: "Could not read that backup." }, { status: 500 });+ }+}diff --git a/src/app/api/backup/import/route.ts b/src/app/api/backup/import/route.tsnew file mode 100644index 0000000..b7c6418--- /dev/null+++ b/src/app/api/backup/import/route.ts@@ -0,0 +1,55 @@+import { auth } from "@/auth";+import { NextRequest, NextResponse } from "next/server";+import { APP_CONSTANTS } from "@/lib/constants";+import { BackupError, importBackup } from "@/lib/backup";+import { isOverUploadCap } from "@/lib/backup/upload";++// The app's first destructive route handler. Route handlers get none of the+// Origin/Host checking Next.js applies to Server Actions; what stands in for it+// is Auth.js's explicit sameSite: "lax" session cookie, so a cross-site POST+// never carries a session. Anything that later weakens that cookie has to add+// an Origin check here.+export async function POST(req: NextRequest) {+ const session = await auth();+ if (!session?.user?.id) {+ return NextResponse.json({ error: "Not authenticated" }, { status: 401 });+ }++ if (isOverUploadCap(req)) {+ return NextResponse.json(+ { error: "That file is larger than the backup size limit." },+ { status: 413 },+ );+ }++ try {+ const formData = await req.formData();+ const file = formData.get("file");+ const confirmWipe = formData.get("confirmWipe") === "true";++ if (!(file instanceof File) || file.size === 0) {+ return NextResponse.json({ error: "No backup file uploaded" }, { status: 400 });+ }+ if (file.size > APP_CONSTANTS.BACKUP_MAX_UPLOAD_BYTES) {+ return NextResponse.json(+ { error: "That file is larger than the backup size limit." },+ { status: 413 },+ );+ }++ const bytes = Buffer.from(await file.arrayBuffer());+ const result = await importBackup(+ bytes,+ session.user.id,+ session.user.email ?? "",+ confirmWipe,+ );+ return NextResponse.json(result, { status: 200 });+ } catch (error) {+ if (error instanceof BackupError) {+ return NextResponse.json({ error: error.userMessage }, { status: 400 });+ }+ console.error("[Backup] Import failed:", error);+ return NextResponse.json({ error: "Import failed." }, { status: 500 });+ }+}diff --git a/src/app/api/backup/rollback/route.ts b/src/app/api/backup/rollback/route.tsnew file mode 100644index 0000000..ae91472--- /dev/null+++ b/src/app/api/backup/rollback/route.ts@@ -0,0 +1,37 @@+import { auth } from "@/auth";+import { NextRequest, NextResponse } from "next/server";+import { BackupError, importBackup, readSnapshot } from "@/lib/backup";++// A rollback is an import whose bytes come off local disk instead of an+// upload, so it inherits every guard importBackup already has — including+// writing its own snapshot first, which is what makes a rollback undoable.+export async function POST(req: NextRequest) {+ const session = await auth();+ if (!session?.user?.id) {+ return NextResponse.json({ error: "Not authenticated" }, { status: 401 });+ }++ try {+ const { snapshotId } = (await req.json()) as { snapshotId?: unknown };+ if (typeof snapshotId !== "string") {+ return NextResponse.json({ error: "No snapshot selected" }, { status: 400 });+ }++ // readSnapshot validates the id against an anchored pattern before it+ // touches the filesystem; the per-user directory is the second layer.+ const bytes = await readSnapshot(session.user.id, snapshotId);+ const result = await importBackup(+ bytes,+ session.user.id,+ session.user.email ?? "",+ true,+ );+ return NextResponse.json(result, { status: 200 });+ } catch (error) {+ if (error instanceof BackupError) {+ return NextResponse.json({ error: error.userMessage }, { status: 400 });+ }+ console.error("[Backup] Rollback failed:", error);+ return NextResponse.json({ error: "Rollback failed." }, { status: 500 });+ }+}diff --git a/src/app/api/backup/snapshots/route.ts b/src/app/api/backup/snapshots/route.tsnew file mode 100644index 0000000..ade8c35--- /dev/null+++ b/src/app/api/backup/snapshots/route.ts@@ -0,0 +1,19 @@+import { auth } from "@/auth";+import { NextResponse } from "next/server";+import { listSnapshots } from "@/lib/backup";++// The user id comes from the session and is the only thing that selects the+// directory — there is no client-supplied path anywhere in this handler.+export async function GET() {+ const session = await auth();+ if (!session?.user?.id) {+ return NextResponse.json({ error: "Not authenticated" }, { status: 401 });+ }++ try {+ return NextResponse.json(await listSnapshots(session.user.id), { status: 200 });+ } catch (error) {+ console.error("[Backup] Listing snapshots failed:", error);+ return NextResponse.json({ error: "Could not list snapshots." }, { status: 500 });+ }+}diff --git a/src/lib/backup/upload.ts b/src/lib/backup/upload.tsnew file mode 100644index 0000000..2fd0631--- /dev/null+++ b/src/lib/backup/upload.ts@@ -0,0 +1,12 @@+import type { NextRequest } from "next/server";+import { APP_CONSTANTS } from "@/lib/constants";++// Checked before req.formData(), which buffers the entire body into memory.+// Checking file.size afterwards reports the problem but does not prevent it.+export function isOverUploadCap(req: NextRequest): boolean {+ const declared = Number(req.headers.get("content-length"));+ return (+ Number.isFinite(declared) &&+ declared > APP_CONSTANTS.BACKUP_MAX_UPLOAD_BYTES+ );+}