diff --git a/scripts/load_path.py b/scripts/load_path.py index 77f1cab..2adb9bd 100644 --- a/scripts/load_path.py +++ b/scripts/load_path.py @@ -99,6 +99,15 @@ PATHS_LOGINED = [ f"/{MOD}/api/get_search_reporter_type.dspy", f"/{MOD}/api/get_search_severity.dspy", f"/{MOD}/api/get_search_status.dspy", + # Workspace 文件浏览/编辑 + f"/{MOD}/api/workspace_tree.dspy", + f"/{MOD}/api/workspace_files.dspy", + f"/{MOD}/api/workspace_open.dspy", + f"/{MOD}/api/workspace_file.dspy", + f"/{MOD}/api/workspace_upload.dspy", + f"/{MOD}/api/workspace_delete.dspy", + f"/{MOD}/api/workspace_popup.dspy", + f"/{MOD}/workspace_edit.xterm", ] diff --git a/wwwroot/api/workspace_file.dspy b/wwwroot/api/workspace_file.dspy index 54374f9..1fd9235 100644 --- a/wwwroot/api/workspace_file.dspy +++ b/wwwroot/api/workspace_file.dspy @@ -1,34 +1,53 @@ -# workspace_file.dspy - 读取/保存工作空间文件 - -action = (params_kw or {}).get('action', 'read') -filepath = (params_kw or {}).get('path', '').strip() +# workspace_file.dspy - 提供工作空间文件(媒体流 / 下载) import os +from urllib.parse import quote +from aiohttp.web_fileresponse import FileResponse -if not filepath or not os.path.isfile(filepath): - return json.dumps({"error": "文件不存在: " + filepath}, ensure_ascii=False) +file_id = (params_kw or {}).get('id', '').strip() +download = (params_kw or {}).get('download', '').strip() -if action == 'read': - try: - with open(filepath, 'r', encoding='utf-8') as f: - content = f.read(50000) - return json.dumps({ - "path": filepath, - "name": os.path.basename(filepath), - "size": os.path.getsize(filepath), - "content": content, - }, ensure_ascii=False) - except Exception as e: - return json.dumps({"error": str(e)}, ensure_ascii=False) +uid = await get_user() +if not uid: + uid = 'user-01' -elif action == 'save': - content = (params_kw or {}).get('content', '') - try: - with open(filepath, 'w', encoding='utf-8') as f: - f.write(content) - return json.dumps({"success": True, "path": filepath}, ensure_ascii=False) - except Exception as e: - return json.dumps({"error": str(e)}, ensure_ascii=False) +dbname = get_module_dbname('pipeline-sdlc') +workspace_base = '/d/pipeline/workspaces' -else: - return json.dumps({"error": "未知操作"}, ensure_ascii=False) +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 {"widgettype": "Message", "options": {"title": "错误", "message": "未指定文件"}} + +full_path = ws_dir + '/' + file_id if ws_dir else file_id + +# 路径穿越校验 +real_ws = os.path.realpath(ws_dir) +real_full = os.path.realpath(full_path) +if not real_full.startswith(real_ws + os.sep): + return {"widgettype": "Message", "options": {"title": "错误", "message": "非法路径"}} + +if not os.path.isfile(full_path): + return {"widgettype": "Message", "options": {"title": "错误", "message": "文件不存在: " + file_id}} + +headers = {} +if download: + filename = os.path.basename(full_path) + safe_name = quote(filename) + headers['Content-Disposition'] = 'attachment; filename="%s"; filename*=UTF-8\'\'%s' % (filename, safe_name) + +return FileResponse(full_path, headers=headers) diff --git a/wwwroot/api/workspace_open.dspy b/wwwroot/api/workspace_open.dspy index 9628df0..15117eb 100644 --- a/wwwroot/api/workspace_open.dspy +++ b/wwwroot/api/workspace_open.dspy @@ -1,6 +1,8 @@ -# workspace_open.dspy - 预览/打开文件(文本文件显示内容,二进制提示) +# workspace_open.dspy - 打开文件:文本/源码→vi编辑,office/pdf→下载,媒体→播放 import os +import json as _json +from urllib.parse import quote file_id = (params_kw or {}).get('id', '').strip() @@ -41,27 +43,72 @@ 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'] +video_exts = ['.mp4', '.m3u8', '.mpd', '.webm', '.mov', '.avi', '.mkv'] +audio_exts = ['.mp3', '.wav', '.aac', '.flac', '.m4a', '.ogg'] +image_exts = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.bmp', '.ico'] -if ext not in text_exts: - return {"widgettype": "Message", "options": {"title": "无法预览", "message": "二进制文件无法预览: " + name}} +file_url = entire_url("/pipeline-sdlc/api/workspace_file.dspy") + "?id=" + quote(file_id) -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)}} +# 1. 文本/源码 → Wterm + vi 编辑 +if ext in text_exts: + ws_url = websocket_url("/wss/pipeline-sdlc/workspace_edit.xterm") + "?id=" + quote(file_id) + return { + "widgettype": "PopupWindow", + "options": {"title": name + " (vi 编辑)", "width": "85%", "height": "85%", "auto_open": True}, + "subwidgets": [{ + "widgettype": "Wterm", + "options": { + "width": "100%", + "height": "100%", + "term_options": {"fontSize": 14}, + "ws_url": ws_url + } + }] + } -if len(content) > 200000: - content = content[:200000] + "\n\n...(内容过长,已截断)" +# 2. 媒体 → 播放 +if ext in video_exts: + return { + "widgettype": "PopupWindow", + "options": {"title": name, "width": "70%", "height": "70%", "auto_open": True}, + "subwidgets": [{ + "widgettype": "VideoPlayer", + "options": {"url": file_url, "width": "100%", "height": "100%"} + }] + } +if ext in audio_exts: + return { + "widgettype": "PopupWindow", + "options": {"title": name, "width": "50%", "height": "30%", "auto_open": True}, + "subwidgets": [{ + "widgettype": "AudioPlayer", + "options": {"url": file_url, "width": "100%", "height": "100%"} + }] + } + +# 3. 图片 → 预览 +if ext in image_exts: + return { + "widgettype": "PopupWindow", + "options": {"title": name, "width": "70%", "height": "70%", "auto_open": True}, + "subwidgets": [{ + "widgettype": "Image", + "options": {"url": file_url, "width": "100%", "height": "100%"} + }] + } + +# 4. office/pdf 等 → 下载后用本地应用打开 +download_url = file_url + "&download=1" +download_script = ( + "window.open(" + _json.dumps(download_url) + ",'_blank');" +) 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}} - ] - }] + "options": {"title": name, "cwidth": 34, "cheight": 12, "auto_open": True}, + "subwidgets": [ + {"widgettype": "Text", "options": {"text": "该文件类型需下载后用本地应用打开", "cfontsize": 1, "padding": "16px", "halign": "middle"}}, + {"widgettype": "Button", "options": {"label": "下载文件", "css": "primary"}, + "binds": [{"wid": "self", "event": "click", "actiontype": "script", "target": "self", "script": download_script}]} + ] } diff --git a/wwwroot/workspace_edit.xterm b/wwwroot/workspace_edit.xterm new file mode 100644 index 0000000..fd85e4b --- /dev/null +++ b/wwwroot/workspace_edit.xterm @@ -0,0 +1,51 @@ +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 not ws_dir: + r = DictObject() + r.host = 'localhost' + r.username = 'pipeline' + r.cmdargs = ['echo', '未指定文件'] + return r + +full_path = ws_dir + '/' + file_id + +# 路径穿越校验 +real_ws = os.path.realpath(ws_dir) +real_full = os.path.realpath(full_path) +if not real_full.startswith(real_ws + os.sep): + r = DictObject() + r.host = 'localhost' + r.username = 'pipeline' + r.cmdargs = ['echo', '非法路径'] + return r + +r = DictObject() +r.host = 'localhost' +r.username = 'pipeline' +r.cmdargs = ['vi', full_path] +return r