@@ -6,7 +6,70 @@ EditorRouter.get('/:filePath/:clerkID', ReadfileContents);
6
6
EditorRouter . post ( '/save-file' , SaveCurrentFile )
7
7
EditorRouter . post ( '/new-file' , CreateNewFile )
8
8
EditorRouter . post ( '/new-folder' , CreateNewFolder ) ;
9
- EditorRouter . post ( '/delete' , DeleteController ) ;
9
+ // Route to delete a folder
10
+ EditorRouter . post ( '/delete-folder' , async ( req , res ) => {
11
+ const { folderPath } = req . body ;
12
+
13
+ try {
14
+ // Verify folderPath is provided and is a string
15
+ if ( ! folderPath || typeof folderPath !== 'string' ) {
16
+ return res . status ( 400 ) . json ( { error : 'Invalid folder path' } ) ;
17
+ }
18
+
19
+ // Construct absolute path
20
+ const absolutePath = path . join ( __dirname , folderPath ) ;
21
+
22
+ // Check if the path exists
23
+ const pathExists = await fs . access ( absolutePath )
24
+ . then ( ( ) => true )
25
+ . catch ( ( ) => false ) ;
26
+
27
+ if ( ! pathExists ) {
28
+ return res . status ( 404 ) . json ( { error : 'Folder not found' } ) ;
29
+ }
30
+
31
+ // Delete folder recursively
32
+ await fs . rmdir ( absolutePath , { recursive : true } ) ;
33
+
34
+ res . json ( { message : 'Folder deleted successfully' } ) ;
35
+ } catch ( error ) {
36
+ console . error ( 'Error deleting folder:' , error ) ;
37
+ res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
38
+ }
39
+ } ) ;
40
+
41
+ // Route to delete a file
42
+ EditorRouter . post ( '/delete-file' , async ( req , res ) => {
43
+ const { filePath } = req . body ;
44
+
45
+ try {
46
+ // Verify filePath is provided and is a string
47
+ if ( ! filePath || typeof filePath !== 'string' ) {
48
+ return res . status ( 400 ) . json ( { error : 'Invalid file path' } ) ;
49
+ }
50
+
51
+ // Construct absolute path
52
+ const absolutePath = path . join ( __dirname , filePath ) ;
53
+
54
+ // Check if the file exists
55
+ const fileExists = await fs . access ( absolutePath )
56
+ . then ( ( ) => true )
57
+ . catch ( ( ) => false ) ;
58
+
59
+ if ( ! fileExists ) {
60
+ return res . status ( 404 ) . json ( { error : 'File not found' } ) ;
61
+ }
62
+
63
+ // Delete the file
64
+ await fs . unlink ( absolutePath ) ;
65
+
66
+ res . json ( { message : 'File deleted successfully' } ) ;
67
+ } catch ( error ) {
68
+ console . error ( 'Error deleting file:' , error ) ;
69
+ res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
70
+ }
71
+ } ) ;
72
+
10
73
EditorRouter . post ( '/rename' , RenameController ) ;
11
74
12
75
0 commit comments