mirror of
https://github.com/codeaashu/claude-code.git
synced 2026-04-08 22:28:48 +03:00
claude-code
This commit is contained in:
55
web/app/api/files/read/route.ts
Normal file
55
web/app/api/files/read/route.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
const IMAGE_MIME: Record<string, string> = {
|
||||
png: "image/png",
|
||||
jpg: "image/jpeg",
|
||||
jpeg: "image/jpeg",
|
||||
gif: "image/gif",
|
||||
webp: "image/webp",
|
||||
bmp: "image/bmp",
|
||||
ico: "image/x-icon",
|
||||
};
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const filePath = request.nextUrl.searchParams.get("path");
|
||||
if (!filePath) {
|
||||
return NextResponse.json({ error: "path parameter required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const resolvedPath = path.resolve(filePath);
|
||||
|
||||
try {
|
||||
const stats = await fs.stat(resolvedPath);
|
||||
if (stats.isDirectory()) {
|
||||
return NextResponse.json({ error: "path is a directory" }, { status: 400 });
|
||||
}
|
||||
|
||||
const ext = resolvedPath.split(".").pop()?.toLowerCase() ?? "";
|
||||
|
||||
// Binary images: return base64 data URL
|
||||
if (ext in IMAGE_MIME) {
|
||||
const buffer = await fs.readFile(resolvedPath);
|
||||
const base64 = buffer.toString("base64");
|
||||
return NextResponse.json({
|
||||
content: `data:${IMAGE_MIME[ext]};base64,${base64}`,
|
||||
isImage: true,
|
||||
size: stats.size,
|
||||
modified: stats.mtime.toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
// Text (including SVG)
|
||||
const content = await fs.readFile(resolvedPath, "utf-8");
|
||||
return NextResponse.json({
|
||||
content,
|
||||
isImage: ext === "svg",
|
||||
size: stats.size,
|
||||
modified: stats.mtime.toISOString(),
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "Unknown error";
|
||||
return NextResponse.json({ error: message }, { status: 404 });
|
||||
}
|
||||
}
|
||||
31
web/app/api/files/write/route.ts
Normal file
31
web/app/api/files/write/route.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
let body: { path?: string; content?: string };
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "invalid JSON body" }, { status: 400 });
|
||||
}
|
||||
|
||||
const { path: filePath, content } = body;
|
||||
if (!filePath || content === undefined) {
|
||||
return NextResponse.json(
|
||||
{ error: "path and content are required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const resolvedPath = path.resolve(filePath);
|
||||
|
||||
try {
|
||||
await fs.writeFile(resolvedPath, content, "utf-8");
|
||||
const stats = await fs.stat(resolvedPath);
|
||||
return NextResponse.json({ success: true, size: stats.size });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "Unknown error";
|
||||
return NextResponse.json({ error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user