Skip to content

Commit 9036a13

Browse files
Replace shell commands with E2B SDK methods for file operations
1 parent be35b60 commit 9036a13

3 files changed

Lines changed: 36 additions & 122 deletions

File tree

app/api/sandbox/[sbxId]/files/content/route.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,13 @@ export async function GET(
5555
)
5656
}
5757

58-
const result = await sbx.commands.run(`cat "${normalizedPath}"`)
59-
60-
if (result.exitCode !== 0) {
61-
console.error('Error reading file:', result.stderr)
62-
return new Response(
63-
JSON.stringify({
64-
error: 'Failed to read file',
65-
details: result.stderr,
66-
path: normalizedPath
67-
}),
68-
{ status: 404, headers: { 'Content-Type': 'application/json' } }
69-
)
70-
}
58+
// Use E2B SDK's files.read() method for robust file reading
59+
const relativePath = normalizedPath.substring('/home/user/'.length)
60+
const content = await sbx.files.read(relativePath)
7161

7262
return new Response(
7363
JSON.stringify({
74-
content: result.stdout,
64+
content,
7565
path: filePath
7666
}),
7767
{ headers: { 'Content-Type': 'application/json' } }

app/api/sandbox/[sbxId]/files/route.ts

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,11 @@ export async function GET(
3333
// Connect to existing sandbox
3434
const sbx = await Sandbox.connect(sbxId)
3535

36-
// Get file tree from sandbox using shell command
37-
const result = await sbx.commands.run(
38-
'find /home/user -type f -o -type d | sort'
39-
)
40-
41-
if (result.exitCode !== 0) {
42-
console.error('Error listing files:', result.stderr)
43-
return new Response(
44-
JSON.stringify({ error: 'Failed to list files', details: result.stderr }),
45-
{ status: 500, headers: { 'Content-Type': 'application/json' } }
46-
)
47-
}
36+
// Use E2B SDK's files.list() method for robust file listing
37+
const filesList = await sbx.files.list('/home/user')
4838

49-
// Parse the file paths into a tree structure
50-
const files = parseFileTree(result.stdout)
39+
// Convert E2B file structure to our FileSystemNode format
40+
const files = convertE2BFilesToTree(filesList)
5141

5242
return new Response(
5343
JSON.stringify({ files }),
@@ -66,55 +56,21 @@ export async function GET(
6656
}
6757

6858
/**
69-
* Parse file paths from find command output into a tree structure
59+
* Convert E2B file list to our FileSystemNode structure
7060
*/
71-
function parseFileTree(output: string): FileSystemNode[] {
72-
const lines = output.trim().split('\n').filter(line => line.trim())
73-
const root: FileSystemNode[] = []
74-
const nodeMap = new Map<string, FileSystemNode>()
75-
76-
// Sort paths to ensure parents come before children
77-
const paths = lines
78-
.map(line => line.trim())
79-
.filter(path => path.startsWith('/home/user/'))
80-
.sort()
81-
82-
for (const fullPath of paths) {
83-
// Remove /home/user/ prefix
84-
const relativePath = fullPath.replace('/home/user/', '')
85-
if (!relativePath) continue
86-
87-
const parts = relativePath.split('/')
88-
const name = parts[parts.length - 1]
89-
const parentPath = parts.slice(0, -1).join('/')
90-
91-
// Determine if it's a directory (find includes both files and dirs)
92-
// We'll check if there are children later
61+
function convertE2BFilesToTree(e2bFiles: any[]): FileSystemNode[] {
62+
return e2bFiles.map(file => {
9363
const node: FileSystemNode = {
94-
name,
95-
isDirectory: false, // Will be updated if we find children
96-
path: `/${relativePath}`,
97-
children: []
64+
name: file.name,
65+
isDirectory: file.isDir,
66+
path: `/${file.path}`,
9867
}
9968

100-
nodeMap.set(relativePath, node)
101-
102-
if (parentPath === '') {
103-
// Root level file/directory
104-
root.push(node)
105-
} else {
106-
// Add to parent's children
107-
const parent = nodeMap.get(parentPath)
108-
if (parent) {
109-
if (!parent.children) {
110-
parent.children = []
111-
}
112-
parent.children.push(node)
113-
// Mark parent as directory
114-
parent.isDirectory = true
115-
}
69+
// Recursively convert children if it's a directory
70+
if (file.isDir && file.children) {
71+
node.children = convertE2BFilesToTree(file.children)
11672
}
117-
}
11873

119-
return root
74+
return node
75+
})
12076
}

app/api/sandbox/route.ts

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,32 @@ const sandboxTimeout = 10 * 60 * 1000
77

88
async function fetchSandboxFiles(sbx: Sandbox): Promise<FileSystemNode[]> {
99
try {
10-
const result = await sbx.commands.run(
11-
'find /home/user -type f -o -type d 2>/dev/null | grep -v "node_modules" | sort'
12-
)
13-
14-
if (result.exitCode !== 0) {
15-
console.error('Error listing files:', result.stderr)
16-
return []
17-
}
18-
19-
return parseFileTree(result.stdout)
10+
// Use E2B SDK's files.list() method for robust file listing
11+
const filesList = await sbx.files.list('/home/user')
12+
return convertE2BFilesToTree(filesList)
2013
} catch (error) {
2114
console.error('Error fetching sandbox files:', error)
2215
return []
2316
}
2417
}
2518

26-
function parseFileTree(output: string): FileSystemNode[] {
27-
const lines = output.trim().split('\n').filter(line => line.trim())
28-
const root: FileSystemNode[] = []
29-
const nodeMap = new Map<string, FileSystemNode>()
30-
31-
const paths = lines
32-
.map(line => line.trim())
33-
.filter(path => path.startsWith('/home/user/'))
34-
.sort()
35-
36-
for (const fullPath of paths) {
37-
const relativePath = fullPath.replace('/home/user/', '')
38-
if (!relativePath) continue
39-
40-
const parts = relativePath.split('/')
41-
const name = parts[parts.length - 1]
42-
const parentPath = parts.slice(0, -1).join('/')
43-
44-
const node: FileSystemNode = {
45-
name,
46-
isDirectory: false,
47-
path: `/${relativePath}`,
48-
children: []
49-
}
19+
function convertE2BFilesToTree(e2bFiles: any[]): FileSystemNode[] {
20+
return e2bFiles
21+
.filter(file => !file.name.includes('node_modules')) // Filter out node_modules
22+
.map(file => {
23+
const node: FileSystemNode = {
24+
name: file.name,
25+
isDirectory: file.isDir,
26+
path: `/${file.path}`,
27+
}
5028

51-
nodeMap.set(relativePath, node)
52-
53-
if (parentPath === '') {
54-
root.push(node)
55-
} else {
56-
const parent = nodeMap.get(parentPath)
57-
if (parent) {
58-
if (!parent.children) {
59-
parent.children = []
60-
}
61-
parent.children.push(node)
62-
parent.isDirectory = true
29+
// Recursively convert children if it's a directory
30+
if (file.isDir && file.children) {
31+
node.children = convertE2BFilesToTree(file.children)
6332
}
64-
}
65-
}
6633

67-
return root
34+
return node
35+
})
6836
}
6937

7038
export const maxDuration = 60

0 commit comments

Comments
 (0)