tree: lazy-load children only
This commit is contained in:
parent
1dfaa7a573
commit
af7d7fc716
@ -1,7 +1,9 @@
|
|||||||
# workspace_tree.dspy - 返回完整目录树(非懒加载,所有节点一次性返回)
|
# workspace_tree.dspy - 懒加载:返回指定节点的直接子目录
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
node_id = (params_kw or {}).get('id', '').strip()
|
||||||
|
|
||||||
uid = await get_user()
|
uid = await get_user()
|
||||||
if not uid:
|
if not uid:
|
||||||
uid = 'user-01'
|
uid = 'user-01'
|
||||||
@ -27,29 +29,47 @@ async with DBPools().sqlorContext(dbname) as sor:
|
|||||||
ws_dir = ws if ws.startswith('/') else workspace_base + '/' + org_id + '/' + pname
|
ws_dir = ws if ws.startswith('/') else workspace_base + '/' + org_id + '/' + pname
|
||||||
|
|
||||||
if not ws_dir or not os.path.isdir(ws_dir):
|
if not ws_dir or not os.path.isdir(ws_dir):
|
||||||
return [{"id": "__root__", "parentid": "", "label": "📁 工作空间(不可用)", "path": ""}]
|
if not node_id:
|
||||||
|
return [{"id": "__root__", "parentid": "", "label": "📁 工作空间(不可用)", "path": ws_dir, "is_leaf": False}]
|
||||||
|
return []
|
||||||
|
|
||||||
items = [{"id": "__root__", "parentid": "", "label": "📁 " + os.path.basename(ws_dir), "path": ws_dir, "is_leaf": False}]
|
# 初始加载:无 id,返回根节点
|
||||||
|
if not node_id:
|
||||||
|
return [{
|
||||||
|
"id": "__root__",
|
||||||
|
"parentid": "",
|
||||||
|
"label": "📁 " + os.path.basename(ws_dir),
|
||||||
|
"path": ws_dir,
|
||||||
|
"is_leaf": False
|
||||||
|
}]
|
||||||
|
|
||||||
def scan_dir(path, parent_id, rel_prefix):
|
# 展开节点:id=__root__ → 扫描 ws_dir。否则拼完整路径
|
||||||
try:
|
if node_id == '__root__':
|
||||||
entries = sorted(os.listdir(path))
|
scan_path = ws_dir
|
||||||
except Exception:
|
else:
|
||||||
return
|
scan_path = ws_dir + '/' + node_id
|
||||||
for name in entries:
|
|
||||||
full = os.path.join(path, name)
|
if not os.path.isdir(scan_path):
|
||||||
if name.startswith('.') or name == '__pycache__':
|
return []
|
||||||
continue
|
|
||||||
if os.path.isdir(full):
|
items = []
|
||||||
rel_path = rel_prefix + '/' + name if rel_prefix else name
|
try:
|
||||||
items.append({
|
entries = sorted(os.listdir(scan_path))
|
||||||
"id": rel_path,
|
except Exception:
|
||||||
"parentid": parent_id,
|
return []
|
||||||
"label": "📁 " + name,
|
|
||||||
"path": full,
|
for name in entries:
|
||||||
"is_leaf": False
|
full = os.path.join(scan_path, name)
|
||||||
})
|
if name.startswith('.') or name == '__pycache__':
|
||||||
scan_dir(full, rel_path, rel_path)
|
continue
|
||||||
|
if os.path.isdir(full):
|
||||||
|
rel_path = node_id + '/' + name if node_id != '__root__' else name
|
||||||
|
items.append({
|
||||||
|
"id": rel_path,
|
||||||
|
"parentid": node_id,
|
||||||
|
"label": "📁 " + name,
|
||||||
|
"path": full,
|
||||||
|
"is_leaf": False
|
||||||
|
})
|
||||||
|
|
||||||
scan_dir(ws_dir, "__root__", "")
|
|
||||||
return items
|
return items
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user