56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { rawDb } from "@/lib/db/client";
|
|
import { assertLocalRequest } from "@/lib/api/localOnly";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const TABLES = [
|
|
"images",
|
|
"studios",
|
|
"labels",
|
|
"series",
|
|
"actresses",
|
|
"genres",
|
|
"tag_categories",
|
|
"tags",
|
|
"collections",
|
|
"image_actresses",
|
|
"image_genres",
|
|
"image_tags",
|
|
"collection_images",
|
|
"actress_categories",
|
|
"actress_categories_map",
|
|
"app_settings",
|
|
];
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const blocked = assertLocalRequest(req);
|
|
if (blocked) return blocked;
|
|
const data: Record<string, unknown[]> = {};
|
|
for (const t of TABLES) {
|
|
try {
|
|
data[t] = rawDb.prepare(`SELECT * FROM ${t}`).all();
|
|
} catch {
|
|
data[t] = [];
|
|
}
|
|
}
|
|
|
|
const payload = {
|
|
app: "Pinkudex",
|
|
version: 1,
|
|
exportedAt: new Date().toISOString(),
|
|
tables: data,
|
|
};
|
|
|
|
const json = JSON.stringify(payload, null, 2);
|
|
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
return new NextResponse(json, {
|
|
headers: {
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
"Content-Disposition": `attachment; filename="pinkudex-backup-${stamp}.json"`,
|
|
"Cache-Control": "no-store",
|
|
},
|
|
});
|
|
}
|