Files
2026-08-10 16:19:20 +03:00

29 lines
1.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { getAllowedFileRoots, isExistingFilePathAllowed, isFilePathAllowed, isWindowsAbsolutePath } from "@/lib/file-access";
import { getGitFileDiff } from "@/lib/git-changes";
export async function GET(request: NextRequest) {
try {
const cwd = request.nextUrl.searchParams.get("cwd")?.trim() ?? "";
const filePath = request.nextUrl.searchParams.get("path")?.trim() ?? "";
if (!cwd || (!cwd.startsWith("/") && !isWindowsAbsolutePath(cwd))) {
return NextResponse.json({ error: "cwd must be an absolute path", code: "cwd_must_be_absolute" }, { status: 400 });
}
if (!filePath || (!filePath.startsWith("/") && !isWindowsAbsolutePath(filePath))) {
return NextResponse.json({ error: "path must be an absolute path", code: "path_must_be_absolute" }, { status: 400 });
}
const allowedRoots = await getAllowedFileRoots();
if (!isFilePathAllowed(cwd, allowedRoots) || !isFilePathAllowed(filePath, allowedRoots)) {
return NextResponse.json({ error: "Access denied", code: "access_denied" }, { status: 403 });
}
if (!isExistingFilePathAllowed(cwd, allowedRoots) || !isExistingFilePathAllowed(filePath, allowedRoots)) {
return NextResponse.json({ error: "Access denied", code: "access_denied" }, { status: 403 });
}
return NextResponse.json(await getGitFileDiff(cwd, filePath));
} catch (error) {
return NextResponse.json({ error: error instanceof Error ? error.message : String(error) }, { status: 500 });
}
}