11import os
22import base64
3+ import shutil
34from datetime import datetime
45from typing import List , Dict
56
@@ -28,30 +29,6 @@ def create_directory(self, directory_path: str):
2829 except Exception as e :
2930 raise ValueError (f"Bad Request: Unable to create directory. { str (e )} " )
3031
31- def delete_directory (self , directory_path : str ):
32- """
33- Delete a directory.
34-
35- Args:
36- directory_path (str): Path of the directory to delete.
37-
38- Raises:
39- ValueError: If the directory does not exist or the path is invalid.
40- """
41- if not os .path .exists (directory_path ):
42- raise ValueError ("Not Found: Directory does not exist." )
43- if not os .path .isdir (directory_path ):
44- raise ValueError ("Bad Request: Path is not a directory." )
45- try :
46- for root , dirs , files in os .walk (directory_path , topdown = False ):
47- for file in files :
48- os .remove (os .path .join (root , file ))
49- for dir in dirs :
50- os .rmdir (os .path .join (root , dir ))
51- os .rmdir (directory_path )
52- except Exception as e :
53- raise ValueError (f"Bad Request: Unable to delete directory. { str (e )} " )
54-
5532 def list_directory (self , directory_path : str ) -> Dict [str , List [Dict [str , str ]]]:
5633 """
5734 List files and directories in a specified directory.
@@ -122,27 +99,39 @@ def upload_file(self, directory_path: str, file_name: str, file_content: str):
12299 except Exception as e :
123100 raise ValueError (f"Bad Request: Unable to upload file. { str (e )} " )
124101
125- def delete_files (self , file_paths : List [str ]):
102+ def delete_dirs_files (self , paths : List [str ]) -> Dict [ str , any ] :
126103 """
127- Delete specified files.
104+ Delete multiple files or directories .
128105
129106 Args:
130- file_paths (List[str]): List of file paths to delete.
107+ paths (List[str]): List of paths (files or directories) to delete.
131108
132- Raises :
133- ValueError: If a file does not exist or the path is invalid .
109+ Returns :
110+ Dict[str, any]: A dictionary containing the deletion status for each path.
134111 """
135- errors = []
136- for file_path in file_paths :
137- if not os .path .exists (file_path ):
138- errors .append (f"Not Found: { file_path } " )
139- continue
140- if not os .path .isfile (file_path ):
141- errors .append (f"Bad Request: { file_path } is not a file." )
142- continue
112+ results = {
113+ "hasFailure" : False ,
114+ "paths" : [],
115+ }
116+
117+ for path in paths :
143118 try :
144- os .remove (file_path )
119+ if not os .path .exists (path ):
120+ results ["paths" ].append ({"path" : path , "status" : "not found" })
121+ results ["hasFailure" ] = True
122+ continue
123+
124+ if os .path .isfile (path ):
125+ os .remove (path ) # Remove file
126+ results ["paths" ].append ({"path" : path , "status" : "file deleted" })
127+ elif os .path .isdir (path ):
128+ shutil .rmtree (path ) # Recursively delete directory
129+ results ["paths" ].append ({"path" : path , "status" : "directory deleted" })
130+ else :
131+ results ["paths" ].append ({"path" : path , "status" : "invalid path type" })
132+ results ["hasFailure" ] = True
145133 except Exception as e :
146- errors .append (f"Unable to delete { file_path } . { str (e )} " )
147- if errors :
148- raise ValueError ("; " .join (errors ))
134+ results ["paths" ].append ({"path" : path , "status" : "error" , "message" : str (e )})
135+ results ["hasFailure" ] = True
136+
137+ return results
0 commit comments