(request: NextRequest)
| 198 | } |
| 199 | |
| 200 | export async function PUT(request: NextRequest) { |
| 201 | try { |
| 202 | const body = await request.json(); |
| 203 | const { workspace = "workspace", path: filePath, content } = body; |
| 204 | |
| 205 | if (!filePath || typeof content !== "string") { |
| 206 | return NextResponse.json( |
| 207 | { error: "Missing path or content" }, |
| 208 | { status: 400 } |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | const safePath = sanitizePath(filePath); |
| 213 | if (!safePath) { |
| 214 | return NextResponse.json( |
| 215 | { error: "Invalid file path" }, |
| 216 | { status: 400 } |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | const workspacePath = path.join(OPENCLAW_DIR, workspace); |
| 221 | |
| 222 | // Validate workspace exists |
| 223 | if (!(await fileExists(workspacePath))) { |
| 224 | return NextResponse.json( |
| 225 | { error: "Workspace not found" }, |
| 226 | { status: 404 } |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | const fullPath = path.join(workspacePath, safePath); |
| 231 | |
| 232 | // Create memory directory if needed |
| 233 | const dir = path.dirname(fullPath); |
| 234 | await fs.mkdir(dir, { recursive: true }); |
| 235 | |
| 236 | await fs.writeFile(fullPath, content, "utf-8"); |
| 237 | |
| 238 | return NextResponse.json({ success: true, path: safePath }); |
| 239 | } catch (error) { |
| 240 | console.error("Error saving file:", error); |
| 241 | return NextResponse.json( |
| 242 | { error: "Failed to save file" }, |
| 243 | { status: 500 } |
| 244 | ); |
| 245 | } |
| 246 | } |
nothing calls this directly
no test coverage detected