pipeline-sdlc/wwwroot/api/workspace_upload.dspy

76 lines
2.3 KiB
Plaintext
Raw Permalink 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_upload.dspy - 上传文件到工作空间目录base64 JSON body
import os
import base64
from urllib.parse import unquote
def _decode_id(s):
for _ in range(4):
if '%' not in s:
break
s2 = unquote(s)
if s2 == s:
break
s = s2
return s
folder_id = _decode_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'
session_id = (params_kw or {}).get('session_id', '') or ''
pipeline_id = (params_kw or {}).get('pipeline_id', '') or ''
dbname = get_module_dbname('pipeline-sdlc')
async with DBPools().sqlorContext(dbname) as sor:
# 产线隔离:跨产线项目视为无项目(与弹窗入口一致,防绕过)
project_dir, _ = await get_project_dir_pl(sor, uid, session_id, pipeline_id)
space_dir, _ = await get_space_dir(sor, uid, session_id)
# 通用会话pipeline_id=_generic锁用户专属目录 _general/{uid}
# 不进入任何项目的工作空间2026-09-08 用户要求)
if pipeline_id == '_generic':
_gd = generic_workspace_dir(uid)
os.makedirs(_gd, exist_ok=True)
project_dir = _gd
space_dir = _gd
if not project_dir or not os.path.isdir(project_dir):
return {"status": "error", "message": "项目目录不可用"}
target_dir = resolve_workspace_path(project_dir, space_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}