Spaces:
Running
Running
Upload router_proxy.py
Browse files- router_proxy.py +133 -137
router_proxy.py
CHANGED
|
@@ -1,137 +1,133 @@
|
|
| 1 |
-
# router_proxy.py
|
| 2 |
-
from fastapi import APIRouter, Depends, HTTPException
|
| 3 |
-
from fastapi.responses import StreamingResponse, JSONResponse, Response
|
| 4 |
-
from sqlalchemy.orm import Session
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
import httpx
|
| 7 |
-
import os
|
| 8 |
-
import urllib.request
|
| 9 |
-
import urllib.error
|
| 10 |
-
import 数据库连接 as json_db
|
| 11 |
-
from database_sql import get_db
|
| 12 |
-
from models_sql import Ownership
|
| 13 |
-
|
| 14 |
-
router = APIRouter()
|
| 15 |
-
|
| 16 |
-
class ProxyGithubZipRequest(BaseModel):
|
| 17 |
-
url: str
|
| 18 |
-
item_id: str
|
| 19 |
-
account: str
|
| 20 |
-
|
| 21 |
-
@router.post("/api/proxy_github_zip")
|
| 22 |
-
async def proxy_github_zip(req_data: ProxyGithubZipRequest, db: Session = Depends(get_db)):
|
| 23 |
-
"""云端代理:校验所有权后,拉取 GitHub 私有库 ZIP 流透传给本地"""
|
| 24 |
-
items_db = json_db.load_data("items.json", default_data=[])
|
| 25 |
-
item = next((i for i in items_db if i["id"] == req_data.item_id), None)
|
| 26 |
-
if not item: return JSONResponse(content={"error": "资源不存在"}, status_code=404)
|
| 27 |
-
|
| 28 |
-
# 1. 核心鉴权:验证用户是否购买过该工具
|
| 29 |
-
price = int(item.get("price", 0))
|
| 30 |
-
if price > 0 and req_data.account != item.get("author"):
|
| 31 |
-
owned = db.query(Ownership).filter(
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
except Exception as e:
|
| 136 |
-
print(f"[proxy_download] 未处理的异常: {e}")
|
| 137 |
-
return JSONResponse(content={"error": f"服务器内部错误: {str(e)}"}, status_code=500)
|
|
|
|
| 1 |
+
# router_proxy.py
|
| 2 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 3 |
+
from fastapi.responses import StreamingResponse, JSONResponse, Response
|
| 4 |
+
from sqlalchemy.orm import Session
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
import httpx
|
| 7 |
+
import os
|
| 8 |
+
import urllib.request
|
| 9 |
+
import urllib.error
|
| 10 |
+
import 数据库连接 as json_db
|
| 11 |
+
from database_sql import get_db
|
| 12 |
+
from models_sql import Ownership
|
| 13 |
+
|
| 14 |
+
router = APIRouter()
|
| 15 |
+
|
| 16 |
+
class ProxyGithubZipRequest(BaseModel):
|
| 17 |
+
url: str
|
| 18 |
+
item_id: str
|
| 19 |
+
account: str
|
| 20 |
+
|
| 21 |
+
@router.post("/api/proxy_github_zip")
|
| 22 |
+
async def proxy_github_zip(req_data: ProxyGithubZipRequest, db: Session = Depends(get_db)):
|
| 23 |
+
"""云端代理:校验所有权后,拉取 GitHub 私有库 ZIP 流透传给本地"""
|
| 24 |
+
items_db = json_db.load_data("items.json", default_data=[])
|
| 25 |
+
item = next((i for i in items_db if i["id"] == req_data.item_id), None)
|
| 26 |
+
if not item: return JSONResponse(content={"error": "资源不存在"}, status_code=404)
|
| 27 |
+
|
| 28 |
+
# 1. 核心鉴权:验证用户是否购买过该工具
|
| 29 |
+
price = int(item.get("price", 0))
|
| 30 |
+
if price > 0 and req_data.account != item.get("author"):
|
| 31 |
+
owned = db.query(Ownership).filter(Ownership.account == req_data.account, Ownership.item_id == req_data.item_id).first()
|
| 32 |
+
if not owned:
|
| 33 |
+
return JSONResponse(content={"error": "🚨 拒绝访问:未找到购买记录!"}, status_code=403)
|
| 34 |
+
|
| 35 |
+
# 2. 解析 GitHub 仓库信息
|
| 36 |
+
repo_url = item.get("link", "").rstrip("/")
|
| 37 |
+
if not repo_url.startswith("https://github.com/"):
|
| 38 |
+
return JSONResponse(content={"error": "无效的仓库地址,目前仅支持 GitHub 私有库代理"}, status_code=400)
|
| 39 |
+
|
| 40 |
+
repo_parts = repo_url.split("/")
|
| 41 |
+
if len(repo_parts) < 2: return JSONResponse(content={"error": "无效的仓库地址格式"}, status_code=400)
|
| 42 |
+
owner = repo_parts[-2]
|
| 43 |
+
repo = repo_parts[-1].replace(".git", "")
|
| 44 |
+
|
| 45 |
+
# GitHub 官方提供的打包下载 API
|
| 46 |
+
github_zip_api = f"https://api.github.com/repos/{owner}/{repo}/zipball"
|
| 47 |
+
|
| 48 |
+
# 优先读取该资源在数据库中绑定的专属创作者 Token
|
| 49 |
+
creator_token = item.get("github_token")
|
| 50 |
+
# 如果没填,尝试使用官方全局兜底的 PAT
|
| 51 |
+
fallback_token = os.environ.get("GITHUB_PAT")
|
| 52 |
+
|
| 53 |
+
active_token = creator_token if creator_token else fallback_token
|
| 54 |
+
|
| 55 |
+
headers = {
|
| 56 |
+
"Accept": "application/vnd.github.v3+json",
|
| 57 |
+
"User-Agent": "ComfyUI-Ranking-SaaS"
|
| 58 |
+
}
|
| 59 |
+
if active_token:
|
| 60 |
+
headers["Authorization"] = f"Bearer {active_token}"
|
| 61 |
+
|
| 62 |
+
# 3. 异步请求 GitHub API 并以流形式透传回客户端 (防内存打爆)
|
| 63 |
+
async def stream_generator():
|
| 64 |
+
async with httpx.AsyncClient(follow_redirects=True) as client:
|
| 65 |
+
try:
|
| 66 |
+
async with client.stream("GET", github_zip_api, headers=headers, timeout=120.0) as response:
|
| 67 |
+
if response.status_code != 200:
|
| 68 |
+
yield b"GITHUB_DOWNLOAD_FAILED"
|
| 69 |
+
return
|
| 70 |
+
async for chunk in response.aiter_bytes():
|
| 71 |
+
yield chunk
|
| 72 |
+
except Exception as e:
|
| 73 |
+
yield b"GITHUB_DOWNLOAD_FAILED"
|
| 74 |
+
|
| 75 |
+
return StreamingResponse(stream_generator(), media_type="application/zip")
|
| 76 |
+
|
| 77 |
+
# ==========================================
|
| 78 |
+
# 新增:工作流/应用 (App) JSON 代理下载接口
|
| 79 |
+
# ==========================================
|
| 80 |
+
class ProxyDownloadRequest(BaseModel):
|
| 81 |
+
url: str
|
| 82 |
+
item_id: str
|
| 83 |
+
account: str
|
| 84 |
+
|
| 85 |
+
@router.post("/api/proxy_download")
|
| 86 |
+
async def proxy_download(req_data: ProxyDownloadRequest, db: Session = Depends(get_db)):
|
| 87 |
+
"""云端代理:校验所有权后,拉取真实的工作流 JSON 文件并透传给本地"""
|
| 88 |
+
items_db = json_db.load_data("items.json", default_data=[])
|
| 89 |
+
item = next((i for i in items_db if i["id"] == req_data.item_id), None)
|
| 90 |
+
|
| 91 |
+
if not item:
|
| 92 |
+
return JSONResponse(content={"error": "云端记录中找不到该资源"}, status_code=404)
|
| 93 |
+
|
| 94 |
+
# 1. 核心鉴权:验证用户是否购买/获取过该工作流
|
| 95 |
+
price = int(item.get("price", 0))
|
| 96 |
+
if price > 0 and req_data.account != item.get("author"):
|
| 97 |
+
owned = db.query(Ownership).filter(Ownership.account == req_data.account, Ownership.item_id == req_data.item_id).first()
|
| 98 |
+
if not owned:
|
| 99 |
+
return JSONResponse(content={"error": "您尚未获取该工作流,请先在社区页面点击获取"}, status_code=403)
|
| 100 |
+
|
| 101 |
+
target_url = req_data.url
|
| 102 |
+
|
| 103 |
+
# 🚀 核心修复:从环境变量提取 Hugging Face Token,并组装 Authorization 请求头
|
| 104 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 105 |
+
|
| 106 |
+
headers = {"User-Agent": "ComfyUI-Ranking-SaaS"}
|
| 107 |
+
if hf_token and "huggingface.co" in target_url:
|
| 108 |
+
headers["Authorization"] = f"Bearer {hf_token}"
|
| 109 |
+
|
| 110 |
+
# 2. 使用 urllib 拉取真实 JSON 数据(保持原始中文文件名,不自动编码)
|
| 111 |
+
try:
|
| 112 |
+
req = urllib.request.Request(target_url, headers=headers)
|
| 113 |
+
with urllib.request.urlopen(req, timeout=120) as resp:
|
| 114 |
+
content = resp.read()
|
| 115 |
+
# 🚀 调试日志:打印响应大小
|
| 116 |
+
print(f"✅ 成功下载资源 [{req_data.item_id}], 大小:{len(content)} bytes")
|
| 117 |
+
# 🚀 核心修复:使用正确的 Response 参数名,并明确指定状态码
|
| 118 |
+
return Response(
|
| 119 |
+
content=content,
|
| 120 |
+
media_type="application/json",
|
| 121 |
+
status_code=200,
|
| 122 |
+
headers={"Content-Disposition": f"attachment; filename={req_data.item_id}.json"}
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
except urllib.error.HTTPError as e:
|
| 126 |
+
error_detail = e.read().decode('utf-8', errors='ignore') if hasattr(e, 'read') else str(e)
|
| 127 |
+
print(f"❌ 源文件拉取失败 [HTTP {e.code}]: {error_detail}")
|
| 128 |
+
return JSONResponse(content={"error": f"源文件拉取失败,HTTP 状态码:{e.code}"}, status_code=e.code)
|
| 129 |
+
except Exception as e:
|
| 130 |
+
import traceback
|
| 131 |
+
print(f"❌ 代理下载异常:{str(e)}")
|
| 132 |
+
print(traceback.format_exc())
|
| 133 |
+
return JSONResponse(content={"error": f"代理下载时发生网络异常:{str(e)}"}, status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|