Spaces:
Running
Running
添加下载进度显示
Browse files- 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 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
try:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
except Exception as e:
|
| 98 |
yield b"GITHUB_DOWNLOAD_FAILED"
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 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 代理下载接口
|