@@ -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}
0 commit comments