workspace: add upload/open/delete tools + drag-drop

This commit is contained in:
p 2026-08-13 15:56:21 +08:00
parent e0d095cb04
commit 5f7ddc197a
5 changed files with 291 additions and 12 deletions

View File

@ -0,0 +1,49 @@
# workspace_delete.dspy - 删除工作空间文件
import os
file_id = (params_kw or {}).get('id', '').strip()
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
if not file_id or file_id == '__root__':
return {"status": "error", "message": "不能删除根目录"}
full_path = ws_dir + '/' + file_id if ws_dir else file_id
# 防止路径穿越:确保最终路径在 ws_dir 内
real_ws = os.path.realpath(ws_dir)
real_full = os.path.realpath(full_path)
if not real_full.startswith(real_ws + os.sep):
return {"status": "error", "message": "非法路径"}
if not os.path.isfile(full_path):
return {"status": "error", "message": "文件不存在: " + file_id}
try:
os.remove(full_path)
except Exception as e:
return {"status": "error", "message": "删除失败: " + str(e)}
return {"status": "ok", "message": "已删除: " + os.path.basename(full_path)}

View File

@ -1,6 +1,7 @@
# workspace_files.dspy - 返回目录文件列表(无 id 或 id=__root__ 时返回根目录文件
# workspace_files.dspy - 目录文件列表(拖拽上传区 + 可选中文件行
import os
import json as _json
folder_id = (params_kw or {}).get('id', '').strip()
@ -27,7 +28,6 @@ async with DBPools().sqlorContext(dbname) as sor:
org_id = getattr(proj[0], 'org_id', '0') or '0'
ws_dir = workspace_base + '/' + org_id + '/' + pname
# 无 id 或 __root__ 都指向工作空间根目录
if not folder_id or folder_id == '__root__':
full_dir = ws_dir
else:
@ -36,6 +36,35 @@ else:
if not os.path.isdir(full_dir):
return {"widgettype": "Text", "options": {"text": "目录不可用: " + folder_id, "cfontsize": 0.9}}
upload_url = entire_url("/pipeline-sdlc/api/workspace_upload.dspy")
# 拖拽上传脚本:读取文件 → base64 → POST → 刷新浏览器
upload_script = (
"var fs=event.params.files;"
"if(!fs||!fs.length)return;"
"for(var i=0;i<fs.length;i++){"
"var f=fs[i].file;"
"var b64=await new Promise(function(res){var r=new FileReader();r.onload=function(){res(r.result);};r.readAsDataURL(f);});"
"var resp=await fetch(" + _json.dumps(upload_url) + ",{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:" + _json.dumps(folder_id) + ",filename:f.name,filedata:b64})});"
"var rj=await resp.json();"
"if(rj.status!='ok'){var m=new bricks.Message({title:'上传失败',message:rj.message||'未知错误'});m.open();}"
"}"
"var rb=bricks.getWidgetById('ws_rb',bricks.app);"
"if(rb){var cid=rb.current_id;rb.current_id=undefined;await rb.render_browser(cid);}"
)
widgets = []
# 拖拽上传区
widgets.append({
"widgettype": "Droppable",
"options": {"accepts": ["*"], "padding": "10px", "css": "filler"},
"subwidgets": [
{"widgettype": "Text", "options": {"text": "📥 拖拽文件到此处上传", "cfontsize": 0.85, "color": "#64748b", "halign": "middle"}}
],
"binds": [{"wid": "self", "event": "filedrop", "actiontype": "script", "target": "self", "script": upload_script}]
})
files = []
try:
entries = sorted(os.listdir(full_dir))
@ -49,24 +78,42 @@ for name in entries:
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 "📁"
is_text = ext in ['.md', '.py', '.js', '.html', '.css', '.json', '.txt', '.xml', '.yaml', '.yml',
'.toml', '.cfg', '.sh', '.sql', '.dspy', '.ui', '.csv', '.ts', '.tsx', '.jsx',
'.java', '.c', '.cpp', '.h', '.go', '.rs', '.rb', '.php', '.vue', '.scss', '.less']
icon = "📄" if is_text else "🎬" if ext in ['.png', '.jpg', '.jpeg', '.gif', '.mp4', '.mp3', '.wav'] else "📦"
if folder_id and folder_id != '__root__':
rel_path = folder_id + '/' + name
else:
rel_path = name
select_script = (
"if(bricks.app._ws_sel_el)bricks.app._ws_sel_el.classList.remove('selected');"
"this.dom_element.classList.add('selected');"
"bricks.app._ws_sel_el=this.dom_element;"
"bricks.app._ws_sel={id:" + _json.dumps(rel_path) + ",name:" + _json.dumps(name) + "};"
)
row = {
"widgettype": "HBox",
"options": {"padding": "6px 10px", "alignItems": "center", "gap": "8px"},
"options": {"padding": "6px 10px", "alignItems": "center", "gap": "8px", "cursor": "pointer"},
"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"}},
],
"binds": [{"wid": "self", "event": "click", "actiontype": "script", "target": "self", "script": select_script}]
}
files.append(row)
if not files:
return {"widgettype": "Text", "options": {"text": "(空目录)", "cfontsize": 0.85, "color": "#94a3b8", "padding": "8px"}}
widgets.append({"widgettype": "Text", "options": {"text": "(空目录)", "cfontsize": 0.85, "color": "#94a3b8", "padding": "8px"}})
else:
widgets.extend(files)
return {
"widgettype": "VScrollPanel",
"options": {"css": "filler", "padding": "8px", "gap": "4px"},
"subwidgets": files
"widgettype": "VBox",
"options": {"css": "filler", "padding": "8px", "gap": "6px", "height": "100%"},
"subwidgets": widgets
}

View File

@ -0,0 +1,67 @@
# workspace_open.dspy - 预览/打开文件(文本文件显示内容,二进制提示)
import os
file_id = (params_kw or {}).get('id', '').strip()
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
if not file_id:
return {"widgettype": "Message", "options": {"title": "打开失败", "message": "未指定文件"}}
full_path = ws_dir + '/' + file_id if ws_dir else file_id
if not os.path.isfile(full_path):
return {"widgettype": "Message", "options": {"title": "打开失败", "message": "文件不存在: " + file_id}}
name = os.path.basename(full_path)
ext = os.path.splitext(name)[1].lower()
text_exts = ['.md', '.py', '.js', '.html', '.css', '.json', '.txt', '.xml', '.yaml', '.yml',
'.toml', '.cfg', '.sh', '.sql', '.dspy', '.ui', '.csv', '.ts', '.tsx', '.jsx',
'.java', '.c', '.cpp', '.h', '.go', '.rs', '.rb', '.php', '.vue', '.scss', '.less', '.ini']
if ext not in text_exts:
return {"widgettype": "Message", "options": {"title": "无法预览", "message": "二进制文件无法预览: " + name}}
try:
with open(full_path, 'r', encoding='utf-8', errors='replace') as f:
content = f.read()
except Exception as e:
return {"widgettype": "Message", "options": {"title": "打开失败", "message": str(e)}}
if len(content) > 200000:
content = content[:200000] + "\n\n...(内容过长,已截断)"
return {
"widgettype": "PopupWindow",
"options": {"title": name, "width": "80%", "height": "80%", "auto_open": True},
"subwidgets": [{
"widgettype": "VScrollPanel",
"options": {"css": "filler", "padding": "12px"},
"subwidgets": [
{"widgettype": "Text", "options": {"text": content, "cfontsize": 0.85, "wrap": True}}
]
}]
}

View File

@ -1,4 +1,6 @@
# workspace_popup.dspy - 返回 PopupWindow内含 ResourceBrowser 控件
# workspace_popup.dspy - 返回 PopupWindow内含 ResourceBrowser含打开/删除 tools
import json as _json
uid = await get_user()
if not uid:
@ -28,6 +30,29 @@ async with DBPools().sqlorContext(dbname) as sor:
}]
}
else:
open_url = entire_url("/pipeline-sdlc/api/workspace_open.dspy")
delete_url = entire_url("/pipeline-sdlc/api/workspace_delete.dspy")
# 打开:预览选中文件
open_script = (
"if(!bricks.app._ws_sel)return;"
"var r=await fetch(" + _json.dumps(open_url) + "+'?id='+encodeURIComponent(bricks.app._ws_sel.id));"
"var d=await r.json();"
"bricks.widgetBuild(d,bricks.app);"
)
# 删除:删除选中文件并刷新
delete_script = (
"if(!bricks.app._ws_sel)return;"
"var resp=await fetch(" + _json.dumps(delete_url) + ",{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:bricks.app._ws_sel.id})});"
"var rj=await resp.json();"
"if(rj.status=='ok'){"
"bricks.app._ws_sel=null;bricks.app._ws_sel_el=null;"
"var rb=bricks.getWidgetById('ws_rb',bricks.app);"
"if(rb){var cid=rb.current_id;rb.current_id=undefined;await rb.render_browser(cid);}"
"}else{var m=new bricks.Message({title:'删除失败',message:rj.message||'未知错误'});m.open();}"
)
resp = {
"widgettype": "PopupWindow",
"options": {
@ -38,6 +63,7 @@ async with DBPools().sqlorContext(dbname) as sor:
},
"subwidgets": [{
"widgettype": "ResourceBrowser",
"id": "ws_rb",
"options": {
"tree_options": {
"widgettype": "Tree",
@ -52,11 +78,32 @@ async with DBPools().sqlorContext(dbname) as sor:
"widgettype": "urlwidget",
"options": {
"url": entire_url("/pipeline-sdlc/api/workspace_files.dspy")
}
},
"tools": [
{
"widgettype": "Button",
"options": {"label": "打开", "css": "small"},
"binds": [{"wid": "self", "event": "click", "actiontype": "script", "target": "self", "script": open_script}]
},
{
"widgettype": "Button",
"options": {"label": "删除", "css": "small"},
"binds": [{
"wid": "self", "event": "click", "actiontype": "script", "target": "self",
"conform": {
"title": "删除确认",
"message": "确认删除选中的文件?",
"conform": {"label": "删除"},
"discard": {"label": "取消"}
},
"script": delete_script
}]
}
]
},
"tree_width": "280px"
}
}]
}
return json.dumps(resp, ensure_ascii=False)
return _json.dumps(resp, ensure_ascii=False)

View File

@ -0,0 +1,69 @@
# workspace_upload.dspy - 上传文件到工作空间目录base64 JSON body
import os
import base64
folder_id = (params_kw or {}).get('id', '').strip()
filename = (params_kw or {}).get('filename', '').strip()
filedata = (params_kw or {}).get('filedata', '').strip()
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
if not ws_dir or not os.path.isdir(ws_dir):
return {"status": "error", "message": "工作空间目录不可用"}
if not folder_id or folder_id == '__root__':
target_dir = ws_dir
else:
target_dir = ws_dir + '/' + folder_id
if not os.path.isdir(target_dir):
return {"status": "error", "message": "目标目录不存在: " + folder_id}
# 防止路径穿越
filename = os.path.basename(filename)
if not filename:
return {"status": "error", "message": "文件名无效"}
if not filedata:
return {"status": "error", "message": "文件内容为空"}
# 解码 base64
try:
if filedata.startswith('data:'):
_, b64 = filedata.split(',', 1)
else:
b64 = filedata
data = base64.b64decode(b64)
except Exception as e:
return {"status": "error", "message": "文件解码失败: " + str(e)}
target_path = os.path.join(target_dir, filename)
try:
with open(target_path, 'wb') as f:
f.write(data)
except Exception as e:
return {"status": "error", "message": "写入失败: " + str(e)}
return {"status": "ok", "message": "上传成功: " + filename}