pipeline-sdlc/wwwroot/api/workspace_files.dspy

87 lines
4.1 KiB
Plaintext

# workspace_files.dspy - 返回目录下文件列表
import os
folder_id = (params_kw or {}).get('id', '').strip()
if not folder_id:
return {"widgettype": "Text", "options": {"text": "请从左侧选择目录", "cfontsize": 0.9}}
uid = await get_user()
if not uid:
uid = 'user-01'
dbname = get_module_dbname('pipeline-sdlc')
workspace_base = '/d/pipeline/workspaces'
async with DBPools().sqlorContext(dbname) as sor:
recs = await sor.sqlExe(
"SELECT current_project_id FROM pipeline_agent_settings WHERE user_id=${u}$", {"u": uid})
pid = getattr(recs[0], 'current_project_id', '') if recs else ''
ws_dir = ''
if pid:
proj = await sor.sqlExe("SELECT name, org_id, workspace_dir FROM sd_projects WHERE id=${p}$", {"p": pid})
if proj:
ws = getattr(proj[0], 'workspace_dir', '') or ''
if ws.startswith('/'):
ws_dir = ws
else:
pname = getattr(proj[0], 'name', '')
org_id = getattr(proj[0], 'org_id', '0') or '0'
ws_dir = workspace_base + '/' + org_id + '/' + pname
# __root__ means workspace root directory
if folder_id == '__root__':
full_dir = ws_dir
else:
full_dir = ws_dir + '/' + folder_id if ws_dir else folder_id
if not os.path.isdir(full_dir):
return {"widgettype": "Text", "options": {"text": "目录不可用: " + folder_id}}
files = []
try:
entries = sorted(os.listdir(full_dir))
except Exception as e:
return {"widgettype": "Text", "options": {"text": "读取错误: " + str(e)}}
for name in entries:
full = os.path.join(full_dir, name)
if name.startswith('.'):
continue
if os.path.isfile(full):
size = os.path.getsize(full)
ext = os.path.splitext(name)[1].lower()
is_text = ext in ['.md','.py','.js','.html','.css','.json','.txt','.xml','.yaml','.yml','.toml','.cfg','.sh','.sql','.dspy','.ui']
icon = "📄" if is_text else "🎬" if ext in ['.png','.jpg','.mp4'] else "📁"
fpath_escaped = full.replace("\\", "\\\\").replace("'", "\\'")
fname_escaped = name.replace("'", "\\'")
row = {
"widgettype": "HBox",
"options": {"padding": "6px 10px", "alignItems": "center", "gap": "8px", "borderRadius": "4px"},
"subwidgets": [
{"widgettype": "Text", "options": {"text": icon, "cfontsize": 1.2}},
{"widgettype": "Text", "options": {"text": name, "cfontsize": 0.9, "color": "#1e293b", "css": "filler"}},
{"widgettype": "Text", "options": {"text": f"{size/1024:.1f}KB", "cfontsize": 0.75, "color": "#94a3b8"}},
],
}
if is_text:
row["options"]["style"] = {"cursor": "pointer"}
row["binds"] = [{
"wid": "self",
"event": "click",
"actiontype": "script",
"target": "self",
"script": f"var pw=new bricks.PopupWindow({{title:'{fname_escaped}',cwidth:70,cheight:32,auto_open:true}});var wt=new bricks.Wterm({{css:'filler',padding:'0'}});pw.content_w.add_widget(wt);var sb=new bricks.Button({{label:'保存'}});pw.content_w.add_widget(sb);fetch('/pipeline-sdlc/api/workspace_file.dspy?action=read&path='+encodeURIComponent('{fpath_escaped}')).then(function(r){{return r.json()}}).then(function(d){{if(d.error){{wt.write('Error: '+d.error);return}};wt.write(d.content||'');sb.bind('click',function(){{var c=wt.get_value();fetch('/pipeline-sdlc/api/workspace_file.dspy',{{method:'POST',headers:{{'Content-Type':'application/x-www-form-urlencoded'}},body:'action=save&path='+encodeURIComponent('{fpath_escaped}')+'&content='+encodeURIComponent(c)}}).then(function(r){{return r.json()}}).then(function(d){{if(d.success){{sb.set_text('已保存');setTimeout(function(){{sb.set_text('保存')}},2000)}}}})}});}})"
}]
files.append(row)
if not files:
return {"widgettype": "Text", "options": {"text": "(空目录)", "cfontsize": 0.85, "color": "#94a3b8"}}
return {
"widgettype": "VScrollPanel",
"options": {"css": "filler", "padding": "8px", "gap": "4px"},
"subwidgets": files
}