Spaces:
Running
Running
Upload router_proxy.py
Browse files- router_proxy.py +19 -18
router_proxy.py
CHANGED
|
@@ -107,22 +107,24 @@ async def proxy_download(req_data: ProxyDownloadRequest, db: Session = Depends(g
|
|
| 107 |
if hf_token and "huggingface.co" in target_url:
|
| 108 |
headers["Authorization"] = f"Bearer {hf_token}"
|
| 109 |
|
| 110 |
-
#
|
| 111 |
try:
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
print(f"✅ 成功下载资源 [{req_data.item_id}], 大小:{len(content)} bytes")
|
| 125 |
-
|
| 126 |
return Response(
|
| 127 |
content=content,
|
| 128 |
media_type="application/json",
|
|
@@ -130,10 +132,9 @@ async def proxy_download(req_data: ProxyDownloadRequest, db: Session = Depends(g
|
|
| 130 |
headers={"Content-Disposition": f"attachment; filename={req_data.item_id}.json"}
|
| 131 |
)
|
| 132 |
|
| 133 |
-
except
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
return JSONResponse(content={"error": f"源文件拉取失败,HTTP 状态码:{e.code}"}, status_code=e.code)
|
| 137 |
except Exception as e:
|
| 138 |
import traceback
|
| 139 |
print(f"❌ 代理下载异常:{str(e)}")
|
|
|
|
| 107 |
if hf_token and "huggingface.co" in target_url:
|
| 108 |
headers["Authorization"] = f"Bearer {hf_token}"
|
| 109 |
|
| 110 |
+
# 🚀 核心修复:使用异步 httpx 替代同步 urllib,避免阻塞事件循环
|
| 111 |
try:
|
| 112 |
+
async with httpx.AsyncClient(follow_redirects=True, verify=False, timeout=120.0) as client:
|
| 113 |
+
print(f"🔍 开始下载资源 [{req_data.item_id}]")
|
| 114 |
+
print(f"🔗 目标地址:{target_url[:80]}...")
|
| 115 |
+
|
| 116 |
+
response = await client.get(target_url, headers=headers)
|
| 117 |
+
|
| 118 |
+
if response.status_code != 200:
|
| 119 |
+
print(f"❌ 源文件拉取失败 [HTTP {response.status_code}]")
|
| 120 |
+
return JSONResponse(
|
| 121 |
+
content={"error": f"源文件拉取失败,HTTP 状态码:{response.status_code}"},
|
| 122 |
+
status_code=response.status_code
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
content = response.content
|
| 126 |
print(f"✅ 成功下载资源 [{req_data.item_id}], 大小:{len(content)} bytes")
|
| 127 |
+
|
| 128 |
return Response(
|
| 129 |
content=content,
|
| 130 |
media_type="application/json",
|
|
|
|
| 132 |
headers={"Content-Disposition": f"attachment; filename={req_data.item_id}.json"}
|
| 133 |
)
|
| 134 |
|
| 135 |
+
except httpx.TimeoutException as e:
|
| 136 |
+
print(f"❌ 下载超时:{str(e)}")
|
| 137 |
+
return JSONResponse(content={"error": "下载超时,请稍后重试"}, status_code=504)
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
import traceback
|
| 140 |
print(f"❌ 代理下载异常:{str(e)}")
|