pipeline-sdlc/wwwroot/api/workspace_delete.dspy

36 lines
1.0 KiB
Plaintext

# 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')
async with DBPools().sqlorContext(dbname) as sor:
ws_dir, _ = await get_workspace_dir(sor, uid)
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)}