Proxy local ZXDB/WoS mirror downloads through application API
- Created `src/app/api/zxdb/download/route.ts` to serve local files. - Updated `resolveLocalLink` in `src/server/repo/zxdb.ts` to return API-relative URLs with `source` and `path` parameters. - Encodes the relative subpath to ensure correct URL construction. - Includes security checks in the API route to prevent path traversal. - Updated `docs/ZXDB.md` to reflect the proxy mechanism. Signed-off: junie@lucy.xalior.com
This commit is contained in:
53
src/app/api/zxdb/download/route.ts
Normal file
53
src/app/api/zxdb/download/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { env } from "@/env";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const source = searchParams.get("source");
|
||||
const filePath = searchParams.get("path");
|
||||
|
||||
if (!source || !filePath) {
|
||||
return new NextResponse("Missing source or path", { status: 400 });
|
||||
}
|
||||
|
||||
let baseDir: string | undefined;
|
||||
if (source === "zxdb") {
|
||||
baseDir = env.ZXDB_LOCAL_FILEPATH;
|
||||
} else if (source === "wos") {
|
||||
baseDir = env.WOS_LOCAL_FILEPATH;
|
||||
}
|
||||
|
||||
if (!baseDir) {
|
||||
return new NextResponse("Invalid source or mirroring not enabled", { status: 400 });
|
||||
}
|
||||
|
||||
// Security: Ensure path doesn't escape baseDir
|
||||
const absolutePath = path.normalize(path.join(baseDir, filePath));
|
||||
if (!absolutePath.startsWith(path.normalize(baseDir))) {
|
||||
return new NextResponse("Forbidden", { status: 403 });
|
||||
}
|
||||
|
||||
if (!fs.existsSync(absolutePath)) {
|
||||
return new NextResponse("File not found", { status: 404 });
|
||||
}
|
||||
|
||||
const stat = fs.statSync(absolutePath);
|
||||
if (!stat.isFile()) {
|
||||
return new NextResponse("Not a file", { status: 400 });
|
||||
}
|
||||
|
||||
const fileBuffer = fs.readFileSync(absolutePath);
|
||||
const fileName = path.basename(absolutePath);
|
||||
|
||||
return new NextResponse(fileBuffer, {
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Content-Disposition": `attachment; filename="${fileName}"`,
|
||||
"Content-Length": stat.size.toString(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export const runtime = "nodejs";
|
||||
Reference in New Issue
Block a user