Add dashboard backup catalog and health tools
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { getCatalogBackup } from "@/app/lib/backupCatalog"
|
||||
|
||||
function contentDisposition(fileName: string) {
|
||||
const fallback = fileName.replace(/[^\w.-]/g, "_") || "sandbox-backup.tar.gz"
|
||||
return `attachment; filename="${fallback}"; filename*=UTF-8''${encodeURIComponent(fileName)}`
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ backupId: string }> },
|
||||
) {
|
||||
try {
|
||||
const { backupId } = await params
|
||||
const backup = await getCatalogBackup(backupId)
|
||||
return new NextResponse(new Uint8Array(backup.bytes), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"cache-control": "no-store",
|
||||
"content-disposition": contentDisposition(backup.metadata.fileName),
|
||||
"content-length": String(backup.bytes.byteLength),
|
||||
"content-type": "application/gzip",
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to download backup"
|
||||
return NextResponse.json({ ok: false, error: message }, { status: 404 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { restoreCatalogBackup } from "@/app/lib/backupCatalog"
|
||||
import { recordActivity } from "@/app/lib/activityLog"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ backupId: string }> },
|
||||
) {
|
||||
try {
|
||||
const { backupId } = await params
|
||||
const body = await request.json()
|
||||
const sandboxId = typeof body?.sandboxId === "string" ? body.sandboxId.trim() : ""
|
||||
const targetPath = typeof body?.targetPath === "string" && body.targetPath.trim() ? body.targetPath.trim() : "/sandbox"
|
||||
const replace = body?.replace === true
|
||||
if (!sandboxId) throw new Error("sandboxId is required")
|
||||
|
||||
const restored = await restoreCatalogBackup(backupId, sandboxId, targetPath, replace)
|
||||
await recordActivity({
|
||||
type: "backup.catalog.restore",
|
||||
status: "success",
|
||||
sandboxId,
|
||||
sandboxName: restored.sandboxName,
|
||||
message: `Restored catalog backup into ${restored.targetPath}.`,
|
||||
metadata: { backupId, targetPath: restored.targetPath, mode: restored.mode, bytes: restored.bytes },
|
||||
})
|
||||
|
||||
return NextResponse.json({ ok: true, restored, note: `Restored catalog backup into ${restored.targetPath} (${restored.mode}).` })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to restore catalog backup"
|
||||
await recordActivity({
|
||||
type: "backup.catalog.restore",
|
||||
status: "error",
|
||||
message,
|
||||
}).catch(() => undefined)
|
||||
return NextResponse.json({ ok: false, error: message }, { status: /required|path|large|unsafe|archive/.test(message) ? 400 : 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { deleteCatalogBackup } from "@/app/lib/backupCatalog"
|
||||
import { recordActivity } from "@/app/lib/activityLog"
|
||||
|
||||
export async function DELETE(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ backupId: string }> },
|
||||
) {
|
||||
try {
|
||||
const { backupId } = await params
|
||||
await deleteCatalogBackup(backupId)
|
||||
await recordActivity({
|
||||
type: "backup.catalog.delete",
|
||||
status: "success",
|
||||
message: `Deleted catalog backup ${backupId}.`,
|
||||
metadata: { backupId },
|
||||
})
|
||||
return NextResponse.json({ ok: true })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to delete backup"
|
||||
return NextResponse.json({ ok: false, error: message }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { createCatalogBackup, listBackupCatalog } from "@/app/lib/backupCatalog"
|
||||
import { recordActivity } from "@/app/lib/activityLog"
|
||||
|
||||
export async function GET() {
|
||||
const backups = await listBackupCatalog()
|
||||
return NextResponse.json({ ok: true, backups })
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const sandboxId = typeof body?.sandboxId === "string" ? body.sandboxId.trim() : ""
|
||||
const sourcePath = typeof body?.sourcePath === "string" && body.sourcePath.trim() ? body.sourcePath.trim() : "/sandbox"
|
||||
if (!sandboxId) throw new Error("sandboxId is required")
|
||||
|
||||
const backup = await createCatalogBackup(sandboxId, sourcePath)
|
||||
await recordActivity({
|
||||
type: "backup.catalog.create",
|
||||
status: "success",
|
||||
sandboxId,
|
||||
sandboxName: backup.sandboxName,
|
||||
message: `Saved backup ${backup.fileName} to catalog.`,
|
||||
metadata: { backupId: backup.id, sourcePath: backup.sourcePath, size: backup.size },
|
||||
})
|
||||
|
||||
return NextResponse.json({ ok: true, backup })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to save backup"
|
||||
await recordActivity({
|
||||
type: "backup.catalog.create",
|
||||
status: "error",
|
||||
message,
|
||||
}).catch(() => undefined)
|
||||
return NextResponse.json({ ok: false, error: message }, { status: /required|path|large|exist|directory/.test(message) ? 400 : 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user