ZHIWEI666 commited on
Commit
3ff2ccf
·
verified ·
1 Parent(s): 4d85da6

Upload router_proxy.py

Browse files
Files changed (1) hide show
  1. router_proxy.py +43 -1
router_proxy.py CHANGED
@@ -67,4 +67,46 @@ async def proxy_github_zip(req_data: ProxyGithubZipRequest, db: Session = Depend
67
  async for chunk in response.aiter_bytes():
68
  yield chunk
69
 
70
- return StreamingResponse(stream_generator(), media_type="application/zip")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  async for chunk in response.aiter_bytes():
68
  yield chunk
69
 
70
+ return StreamingResponse(stream_generator(), media_type="application/zip")
71
+
72
+ # ==========================================
73
+ # 新增:工作流/应用 (App) JSON 代理下载接口
74
+ # ==========================================
75
+ class ProxyDownloadRequest(BaseModel):
76
+ url: str
77
+ item_id: str
78
+ account: str
79
+
80
+ @router.post("/api/proxy_download")
81
+ async def proxy_download(req_data: ProxyDownloadRequest, db: Session = Depends(get_db)):
82
+ """云端代理:校验所有权后,拉取真实的工作流 JSON 文件并透传给本地"""
83
+ items_db = json_db.load_data("items.json", default_data=[])
84
+ item = next((i for i in items_db if i["id"] == req_data.item_id), None)
85
+
86
+ if not item:
87
+ return JSONResponse(content={"error": "云端记录中找不到该资源"}, status_code=404)
88
+
89
+ # 1. 核心鉴权:验证用户是否购买/获取过该工作流
90
+ price = int(item.get("price", 0))
91
+ if price > 0 and req_data.account != item.get("author"):
92
+ owned = db.query(Ownership).filter(Ownership.account == req_data.account, Ownership.item_id == req_data.item_id).first()
93
+ if not owned:
94
+ return JSONResponse(content={"error": "您尚未获取该工作流,请先在社区页面点击获取"}, status_code=403)
95
+
96
+ target_url = req_data.url
97
+
98
+ # 2. 异步拉取真实 JSON 数据并透传回客户端
99
+ try:
100
+ # 开启 follow_redirects=True 防止源地址有 302 重定向跳转
101
+ async with httpx.AsyncClient(follow_redirects=True) as client:
102
+ resp = await client.get(target_url, timeout=30.0)
103
+
104
+ if resp.status_code != 200:
105
+ return JSONResponse(content={"error": f"源文件拉取失败,HTTP状态码: {resp.status_code}"}, status_code=resp.status_code)
106
+
107
+ # 成功则直接将 JSON 二进制流原样返回给本地 ComfyUI 引擎
108
+ from fastapi.responses import Response
109
+ return Response(content=resp.content, media_type="application/json")
110
+
111
+ except Exception as e:
112
+ return JSONResponse(content={"error": f"代理下载时发生网络异常: {str(e)}"}, status_code=500)