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

Update storage/data_routers.py

Browse files
Files changed (1) hide show
  1. storage/data_routers.py +22 -4
storage/data_routers.py CHANGED
@@ -35,17 +35,35 @@ def get_data_tree(
35
  validate_token(token)
36
 
37
  def build_tree(path: Path):
 
 
 
 
 
 
 
 
 
38
  if path.is_file():
39
  return {
40
  "name": path.name,
41
  "type": "file"
42
  }
43
-
44
- # Directory case
45
  children = []
46
- for child in sorted(path.iterdir(), key=lambda p: p.name.lower()):
 
 
 
 
 
 
 
 
 
47
  children.append(build_tree(child))
48
-
49
  return {
50
  "name": path.name,
51
  "type": "directory",
 
35
  validate_token(token)
36
 
37
  def build_tree(path: Path):
38
+ """
39
+ Build a tree representation of directories and files.
40
+ Prevents errors by checking if the path is a directory before iterating.
41
+ """
42
+
43
+ if not path.exists():
44
+ return {"name": path.name, "type": "missing"}
45
+
46
+ # Si es un fichero → devolvemos un nodo simple
47
  if path.is_file():
48
  return {
49
  "name": path.name,
50
  "type": "file"
51
  }
52
+
53
+ # Si es un directorio → construimos sus hijos
54
  children = []
55
+ try:
56
+ entries = sorted(path.iterdir(), key=lambda p: p.name.lower())
57
+ except Exception:
58
+ # Si por cualquier razón no podemos listar, lo tratamos como file
59
+ return {
60
+ "name": path.name,
61
+ "type": "file"
62
+ }
63
+
64
+ for child in entries:
65
  children.append(build_tree(child))
66
+
67
  return {
68
  "name": path.name,
69
  "type": "directory",