|
1 | | -import { NextRequest, NextResponse } from 'next/server' |
| 1 | +import { NextResponse } from 'next/server' |
2 | 2 | import { Sandbox } from '@e2b/code-interpreter' |
3 | 3 |
|
4 | | -export async function GET(request: NextRequest) { |
5 | | - const searchParams = request.nextUrl.searchParams |
6 | | - const sandboxId = searchParams.get('sandboxId') |
7 | | - const path = searchParams.get('path') |
| 4 | +const E2B_API_KEY = process.env.E2B_API_KEY |
| 5 | +if (!E2B_API_KEY) { |
| 6 | + throw new Error('E2B_API_KEY environment variable not found') |
| 7 | +} |
8 | 8 |
|
9 | | - if (!sandboxId || !path) { |
10 | | - return NextResponse.json( |
11 | | - { error: 'sandboxId and path are required' }, |
12 | | - { status: 400 }, |
13 | | - ) |
14 | | - } |
| 9 | +const sandboxTimeout = 10 * 60 * 1000 |
15 | 10 |
|
16 | | - try { |
17 | | - const sandbox = await Sandbox.connect(sandboxId) |
18 | | - const content = await sandbox.files.read(path) |
19 | | - return new NextResponse(content, { |
20 | | - headers: { |
21 | | - 'Content-Type': 'text/plain', |
22 | | - }, |
23 | | - }) |
24 | | - } catch (error) { |
25 | | - console.error('Error fetching file content:', error) |
26 | | - return NextResponse.json( |
27 | | - { error: 'Failed to fetch file content' }, |
28 | | - { status: 500 }, |
29 | | - ) |
30 | | - } |
| 11 | +async function getSandbox(sessionID: string) { |
| 12 | + const sandbox = await Sandbox.create({ |
| 13 | + apiKey: E2B_API_KEY, |
| 14 | + metadata: { |
| 15 | + sessionID, |
| 16 | + }, |
| 17 | + timeoutMs: sandboxTimeout, |
| 18 | + }) |
| 19 | + return sandbox |
31 | 20 | } |
32 | 21 |
|
33 | | -export async function POST(request: NextRequest) { |
34 | | - const { |
35 | | - sandboxId, |
36 | | - path, |
37 | | - content, |
38 | | - }: { sandboxId: string; path: string; content: string } = await request.json() |
| 22 | +export async function GET(req: Request) { |
| 23 | + try { |
| 24 | + const { searchParams } = new URL(req.url) |
| 25 | + const sessionID = searchParams.get('sessionID') |
| 26 | + const path = searchParams.get('path') |
39 | 27 |
|
40 | | - if (!sandboxId || !path || content === undefined) { |
41 | | - return NextResponse.json( |
42 | | - { error: 'sandboxId, path, and content are required' }, |
43 | | - { status: 400 }, |
44 | | - ) |
45 | | - } |
| 28 | + if (!sessionID || !path) { |
| 29 | + return NextResponse.json( |
| 30 | + { error: 'sessionID and path are required' }, |
| 31 | + { status: 400 }, |
| 32 | + ) |
| 33 | + } |
46 | 34 |
|
47 | | - try { |
48 | | - const sandbox = await Sandbox.connect(sandboxId) |
49 | | - await sandbox.files.write(path, content) |
50 | | - return NextResponse.json({ success: true }) |
51 | | - } catch (error) { |
52 | | - console.error('Error saving file content:', error) |
| 35 | + const sandbox = await getSandbox(sessionID) |
| 36 | + const content = await sandbox.files.read(path) |
| 37 | + return NextResponse.json({ content }) |
| 38 | + } catch (error: any) { |
53 | 39 | return NextResponse.json( |
54 | | - { error: 'Failed to save file content' }, |
| 40 | + { error: error.message || 'An unexpected error occurred' }, |
55 | 41 | { status: 500 }, |
56 | 42 | ) |
57 | 43 | } |
58 | 44 | } |
59 | 45 |
|
60 | | -export async function DELETE(request: NextRequest) { |
61 | | - const searchParams = request.nextUrl.searchParams |
62 | | - const sandboxId = searchParams.get('sandboxId') |
63 | | - const path = searchParams.get('path') |
| 46 | +export async function POST(req: Request) { |
| 47 | + try { |
| 48 | + const { sessionID, path, content } = await req.json() |
64 | 49 |
|
65 | | - if (!sandboxId || !path) { |
66 | | - return NextResponse.json( |
67 | | - { error: 'sandboxId and path are required' }, |
68 | | - { status: 400 }, |
69 | | - ) |
70 | | - } |
| 50 | + if (!sessionID || !path || content === undefined) { |
| 51 | + return NextResponse.json( |
| 52 | + { error: 'sessionID, path and content are required' }, |
| 53 | + { status: 400 }, |
| 54 | + ) |
| 55 | + } |
71 | 56 |
|
72 | | - try { |
73 | | - const sandbox = await Sandbox.connect(sandboxId) |
74 | | - await sandbox.files.remove(path) |
| 57 | + const sandbox = await getSandbox(sessionID) |
| 58 | + await sandbox.files.write(path, content) |
75 | 59 | return NextResponse.json({ success: true }) |
76 | | - } catch (error) { |
77 | | - console.error('Error deleting file:', error) |
| 60 | + } catch (error: any) { |
78 | 61 | return NextResponse.json( |
79 | | - { error: 'Failed to delete file' }, |
| 62 | + { error: error.message || 'An unexpected error occurred' }, |
80 | 63 | { status: 500 }, |
81 | 64 | ) |
82 | 65 | } |
|
0 commit comments