polish sandbox workflows
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { backupSandboxArchive } from "@/app/lib/sandboxFiles"
|
||||
|
||||
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<{ sandboxId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { sandboxId } = await params
|
||||
const requestUrl = new URL(request.url)
|
||||
const sourcePath = requestUrl.searchParams.get("path") || "/sandbox"
|
||||
const backup = await backupSandboxArchive(sandboxId, sourcePath)
|
||||
|
||||
return new NextResponse(new Uint8Array(backup.bytes), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"cache-control": "no-store",
|
||||
"content-disposition": contentDisposition(backup.fileName),
|
||||
"content-length": String(backup.bytes.byteLength),
|
||||
"content-type": "application/gzip",
|
||||
"x-sandbox-name": backup.sandboxName,
|
||||
"x-sandbox-path": backup.sourcePath,
|
||||
"x-sandbox-backup-created-at": backup.createdAt,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to create sandbox backup"
|
||||
return NextResponse.json({ ok: false, error: message }, { status: /required|path|large|exist|directory/.test(message) ? 400 : 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { listSandboxFiles } from "@/app/lib/sandboxFiles"
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ sandboxId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { sandboxId } = await params
|
||||
const requestUrl = new URL(request.url)
|
||||
const path = requestUrl.searchParams.get("path") || "/sandbox"
|
||||
const listing = await listSandboxFiles(sandboxId, path)
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
listing,
|
||||
}, {
|
||||
headers: {
|
||||
"cache-control": "no-store",
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to list sandbox files"
|
||||
return NextResponse.json({ ok: false, error: message }, { status: /required|path|directory|readable/.test(message) ? 400 : 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { restoreSandboxArchive } from "@/app/lib/sandboxFiles"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ sandboxId: string }> }
|
||||
) {
|
||||
try {
|
||||
const { sandboxId } = await params
|
||||
const form = await request.formData()
|
||||
const file = form.get("archive")
|
||||
const rawTargetPath = form.get("targetPath")
|
||||
const rawReplace = form.get("replace")
|
||||
if (!(file instanceof File)) throw new Error("archive is required")
|
||||
|
||||
const targetPath = typeof rawTargetPath === "string" && rawTargetPath.trim()
|
||||
? rawTargetPath.trim()
|
||||
: "/sandbox"
|
||||
const replace = rawReplace === "true" || rawReplace === "1"
|
||||
const payload = Buffer.from(await file.arrayBuffer())
|
||||
const restored = await restoreSandboxArchive(sandboxId, targetPath, file.name, payload, replace)
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
restored,
|
||||
note: `Restored ${file.name} into ${restored.targetPath} (${restored.mode}).`,
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to restore sandbox backup"
|
||||
return NextResponse.json({ ok: false, error: message }, { status: /required|path|large|unsafe|archive/.test(message) ? 400 : 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user