pipeline-sdlc/wwwroot/api/workspace_tree.dspy
ymq 524abb23e6 feat(workspace): 工作空间端点按会话解析项目(session_id)
- workspace_popup/tree/files/view/open/delete/upload/file + office_read/save 读 session_id 传 get_workspace_dir
- 内部 URL 链路(查看/编辑/删除/上传/文件/vi编辑/univer-office)透传 session_id
- workspace_edit.xterm 手写项目解析收敛到 get_workspace_dir(session_id)
2026-08-24 16:40:49 +08:00

76 lines
2.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# workspace_tree.dspy - 初始只返回根节点无children靠点击展开
import os
node_id = (params_kw or {}).get('id', '').strip()
uid = await get_user()
if not uid:
uid = 'user-01'
session_id = (params_kw or {}).get('session_id', '') or ''
dbname = get_module_dbname('pipeline-sdlc')
async with DBPools().sqlorContext(dbname) as sor:
ws_dir, _ = await get_workspace_dir(sor, uid, session_id)
if not ws_dir or not os.path.isdir(ws_dir):
if not node_id:
return [{"id": "__root__", "parentid": "", "label": "📁 工作空间(不可用)", "path": ws_dir, "is_leaf": False}]
return []
# 初始加载:只返回根节点
if not node_id:
return [{
"id": "__root__",
"parentid": "",
"label": "📁 " + os.path.basename(ws_dir),
"path": ws_dir,
"is_leaf": False
}]
# 展开节点__root__ 特殊处理,其余用相对路径
if node_id == '__root__':
scan_path = ws_dir
id_prefix = ''
else:
scan_path = ws_dir + '/' + node_id
id_prefix = node_id + '/'
if not os.path.isdir(scan_path):
return []
items = []
try:
entries = sorted(os.listdir(scan_path))
except Exception:
return []
for name in entries:
full = os.path.join(scan_path, name)
if name.startswith('.') or name == '__pycache__':
continue
if os.path.isdir(full):
# 叶子判定:目录下是否还有「可见」子目录(排除隐藏目录和 __pycache__与下方扫描一致
# 无可见子目录 → 叶子(不可展开)
has_subdir = False
try:
for sub in os.listdir(full):
if sub.startswith('.') or sub == '__pycache__':
continue
if os.path.isdir(os.path.join(full, sub)):
has_subdir = True
break
except Exception:
pass
items.append({
"id": id_prefix + name,
"parentid": node_id,
"label": "📁 " + name,
"path": full,
"is_leaf": not has_subdir
})
return items