ZHIWEI666 commited on
Commit
21b27b1
·
verified ·
1 Parent(s): 599e756

添加下载进度显示

Browse files
Files changed (1) hide show
  1. router_proxy.py +27 -10
router_proxy.py CHANGED
@@ -85,19 +85,36 @@ async def proxy_github_zip(req_data: ProxyGithubZipRequest, db: Session = Depend
85
  headers["Authorization"] = f"Bearer {active_token}"
86
 
87
  # 3. 异步请求 GitHub API 并以流形式透传回客户端 (防内存打爆)
88
- async def stream_generator():
89
- async with httpx.AsyncClient(follow_redirects=True) as client:
 
 
 
 
 
 
 
 
 
 
 
 
90
  try:
91
- async with client.stream("GET", github_zip_api, headers=headers, timeout=120.0) as response:
92
- if response.status_code != 200:
93
- yield b"GITHUB_DOWNLOAD_FAILED"
94
- return
95
- async for chunk in response.aiter_bytes():
96
- yield chunk
97
- except Exception as e:
98
  yield b"GITHUB_DOWNLOAD_FAILED"
 
 
 
99
 
100
- return StreamingResponse(stream_generator(), media_type="application/zip")
 
 
 
101
 
102
  # ==========================================
103
  # 新增:工作流/应用 (App) JSON 代理下载接口
 
85
  headers["Authorization"] = f"Bearer {active_token}"
86
 
87
  # 3. 异步请求 GitHub API 并以流形式透传回客户端 (防内存打爆)
88
+ client = httpx.AsyncClient(follow_redirects=True)
89
+ try:
90
+ response = await client.send(
91
+ client.build_request("GET", github_zip_api, headers=headers),
92
+ stream=True
93
+ )
94
+
95
+ # 获取 Content-Length 并透传给客户端,使本地端能计算下载进度
96
+ content_length = response.headers.get('content-length', '')
97
+ resp_headers = {}
98
+ if content_length:
99
+ resp_headers["Content-Length"] = content_length
100
+
101
+ async def stream_generator():
102
  try:
103
+ if response.status_code != 200:
104
+ yield b"GITHUB_DOWNLOAD_FAILED"
105
+ return
106
+ async for chunk in response.aiter_bytes():
107
+ yield chunk
108
+ except Exception:
 
109
  yield b"GITHUB_DOWNLOAD_FAILED"
110
+ finally:
111
+ await response.aclose()
112
+ await client.aclose()
113
 
114
+ return StreamingResponse(stream_generator(), media_type="application/zip", headers=resp_headers)
115
+ except Exception as e:
116
+ await client.aclose()
117
+ return JSONResponse(content={"error": f"代理下载时发生网络异常:{str(e)}"}, status_code=500)
118
 
119
  # ==========================================
120
  # 新增:工作流/应用 (App) JSON 代理下载接口