76 lines
2.5 KiB
Plaintext
76 lines
2.5 KiB
Plaintext
# 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')
|
||
|
||
async with DBPools().sqlorContext(dbname) as sor:
|
||
workspace_base = '/d/pipeline/workspaces'
|
||
try:
|
||
_wbr = await sor.sqlExe("SELECT params_value FROM params WHERE params_name='workspace_base' LIMIT 1", {})
|
||
if _wbr and getattr(_wbr[0], 'params_value', ''):
|
||
workspace_base = getattr(_wbr[0], 'params_value', '')
|
||
except Exception:
|
||
pass
|
||
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}
|