Add assistant-ui data agent frontend
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { readFile, stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { accountBaseRoot, getCurrentAccount } from "@/lib/claw-auth";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const account = await getCurrentAccount();
|
||||
if (!account)
|
||||
return Response.json({ error: "请先登录账号" }, { status: 401 });
|
||||
|
||||
const url = new URL(req.url);
|
||||
const requestedPath = url.searchParams.get("path");
|
||||
if (!requestedPath) {
|
||||
return Response.json({ error: "缺少文件路径" }, { status: 400 });
|
||||
}
|
||||
|
||||
const accountRoot = path.resolve(accountBaseRoot(account.id));
|
||||
const filePath = path.resolve(requestedPath);
|
||||
if (!filePath.startsWith(`${accountRoot}${path.sep}`)) {
|
||||
return Response.json({ error: "文件不在当前账号目录内" }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const fileStat = await stat(filePath);
|
||||
if (!fileStat.isFile()) {
|
||||
return Response.json({ error: "目标不是文件" }, { status: 400 });
|
||||
}
|
||||
const bytes = await readFile(filePath);
|
||||
return new Response(bytes, {
|
||||
headers: {
|
||||
"content-type": contentTypeFor(filePath),
|
||||
"content-disposition": `attachment; filename="${encodeURIComponent(path.basename(filePath))}"`,
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
return Response.json({ error: "文件不存在" }, { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
function contentTypeFor(filePath: string) {
|
||||
const ext = path.extname(filePath).toLowerCase();
|
||||
if (ext === ".json") return "application/json; charset=utf-8";
|
||||
if (ext === ".jsonl" || ext === ".txt" || ext === ".md" || ext === ".csv") {
|
||||
return "text/plain; charset=utf-8";
|
||||
}
|
||||
return "application/octet-stream";
|
||||
}
|
||||
Reference in New Issue
Block a user