VeuReu commited on
Commit
d214ed3
·
verified ·
1 Parent(s): aaeebe9

Update storage/data_routers.py

Browse files
Files changed (1) hide show
  1. storage/data_routers.py +56 -0
storage/data_routers.py CHANGED
@@ -135,4 +135,60 @@ def create_data_item(
135
  "status": "ok",
136
  "created": str(target),
137
  "type": item_type
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  }
 
135
  "status": "ok",
136
  "created": str(target),
137
  "type": item_type
138
+ }
139
+
140
+ @router.delete("/delete_path", tags=["Data Manager"])
141
+ def delete_path(
142
+ target: str = Query(..., description="Ruta absoluta dentro de /data a borrar"),
143
+ token: str = Query(..., description="Token requerido para autorización")
144
+ ):
145
+ """
146
+ Delete a file or directory recursively inside /data, except protected folders.
147
+
148
+ Behavior:
149
+ - Validate token.
150
+ - Validate path starts with /data/.
151
+ - Deny deletion of /data/db and /data/media (and anything inside them).
152
+ - If target is a file → delete file.
153
+ - If target is a directory → delete recursively.
154
+ """
155
+
156
+ validate_token(token)
157
+
158
+ # Normalizar ruta
159
+ path = Path(target).resolve()
160
+
161
+ # Debe estar dentro de /data/
162
+ if not str(path).startswith(str(DATA_ROOT)):
163
+ raise HTTPException(status_code=400, detail="Path must be inside /data/")
164
+
165
+ # Carpetas protegidas
166
+ protected = [
167
+ DATA_ROOT / "db",
168
+ DATA_ROOT / "media",
169
+ ]
170
+
171
+ for p in protected:
172
+ if path == p or str(path).startswith(str(p) + "/"):
173
+ raise HTTPException(
174
+ status_code=403,
175
+ detail=f"Deletion forbidden: {p} is protected"
176
+ )
177
+
178
+ # Verificar existencia
179
+ if not path.exists():
180
+ raise HTTPException(status_code=404, detail="Target path does not exist")
181
+
182
+ try:
183
+ if path.is_file():
184
+ path.unlink()
185
+ else:
186
+ shutil.rmtree(path)
187
+
188
+ except Exception as exc:
189
+ raise HTTPException(status_code=500, detail=f"Failed to delete: {exc}")
190
+
191
+ return {
192
+ "status": "ok",
193
+ "deleted": str(path)
194
  }