Harden MCP broker sandbox handoff
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { downloadSandboxFile } from "@/app/lib/sandboxFiles"
|
||||
import { downloadSandboxPath } from "@/app/lib/sandboxFiles"
|
||||
|
||||
function contentDisposition(fileName: string) {
|
||||
const fallback = fileName.replace(/[^\w.-]/g, "_") || "download.bin"
|
||||
@@ -14,7 +14,7 @@ export async function GET(
|
||||
const { sandboxId } = await params
|
||||
const requestUrl = new URL(request.url)
|
||||
const sourcePath = requestUrl.searchParams.get("path") || ""
|
||||
const downloaded = await downloadSandboxFile(sandboxId, sourcePath)
|
||||
const downloaded = await downloadSandboxPath(sandboxId, sourcePath)
|
||||
|
||||
return new NextResponse(new Uint8Array(downloaded.bytes), {
|
||||
status: 200,
|
||||
@@ -22,7 +22,7 @@ export async function GET(
|
||||
"cache-control": "no-store",
|
||||
"content-disposition": contentDisposition(downloaded.fileName),
|
||||
"content-length": String(downloaded.bytes.byteLength),
|
||||
"content-type": "application/octet-stream",
|
||||
"content-type": downloaded.archive ? "application/gzip" : "application/octet-stream",
|
||||
"x-sandbox-name": downloaded.sandboxName,
|
||||
"x-sandbox-path": downloaded.path,
|
||||
},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { assertRequestContentLength, uploadSandboxFile } from "@/app/lib/sandboxFiles"
|
||||
import { assertRequestContentLength, uploadSandboxFile, uploadSandboxFiles } from "@/app/lib/sandboxFiles"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
@@ -10,13 +10,37 @@ export async function POST(
|
||||
assertRequestContentLength(request)
|
||||
const form = await request.formData()
|
||||
const file = form.get("file")
|
||||
const files = form.getAll("files")
|
||||
const rawPath = form.get("path")
|
||||
if (!(file instanceof File)) throw new Error("file is required")
|
||||
const rawRelativePaths = form.get("relativePaths")
|
||||
const destinationPath = typeof rawPath === "string" && rawPath.trim()
|
||||
? rawPath.trim()
|
||||
: `/sandbox/${file.name}`
|
||||
: "/sandbox/"
|
||||
|
||||
if (files.length > 0) {
|
||||
const relativePaths = typeof rawRelativePaths === "string" ? JSON.parse(rawRelativePaths) : []
|
||||
if (!Array.isArray(relativePaths)) throw new Error("relative paths are required for directory upload")
|
||||
const payloads = await Promise.all(files.map(async (item, index) => {
|
||||
if (!(item instanceof File)) throw new Error("files must contain only file uploads")
|
||||
return {
|
||||
fileName: item.name,
|
||||
relativePath: typeof relativePaths[index] === "string" && relativePaths[index] ? relativePaths[index] : item.name,
|
||||
payload: Buffer.from(await item.arrayBuffer()),
|
||||
}
|
||||
}))
|
||||
const uploaded = await uploadSandboxFiles(sandboxId, destinationPath, payloads)
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
uploaded,
|
||||
note: `Uploaded ${uploaded.files.length} file${uploaded.files.length === 1 ? "" : "s"} to ${destinationPath}.`,
|
||||
})
|
||||
}
|
||||
|
||||
if (!(file instanceof File)) throw new Error("file is required")
|
||||
const targetPath = destinationPath.endsWith("/") ? destinationPath : rawPath ? destinationPath : `/sandbox/${file.name}`
|
||||
const payload = Buffer.from(await file.arrayBuffer())
|
||||
const uploaded = await uploadSandboxFile(sandboxId, destinationPath, file.name, payload)
|
||||
const uploaded = await uploadSandboxFile(sandboxId, targetPath, file.name, payload)
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
|
||||
Reference in New Issue
Block a user