| 3 | import path from "path"; |
| 4 | |
| 5 | export async function POST(request: NextRequest) { |
| 6 | let body: { path?: string; content?: string }; |
| 7 | try { |
| 8 | body = await request.json(); |
| 9 | } catch { |
| 10 | return NextResponse.json({ error: "invalid JSON body" }, { status: 400 }); |
| 11 | } |
| 12 | |
| 13 | const { path: filePath, content } = body; |
| 14 | if (!filePath || content === undefined) { |
| 15 | return NextResponse.json( |
| 16 | { error: "path and content are required" }, |
| 17 | { status: 400 } |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | const resolvedPath = path.resolve(filePath); |
| 22 | |
| 23 | try { |
| 24 | await fs.writeFile(resolvedPath, content, "utf-8"); |
| 25 | const stats = await fs.stat(resolvedPath); |
| 26 | return NextResponse.json({ success: true, size: stats.size }); |
| 27 | } catch (err) { |
| 28 | const message = err instanceof Error ? err.message : "Unknown error"; |
| 29 | return NextResponse.json({ error: message }, { status: 500 }); |
| 30 | } |
| 31 | } |