Spaces:
Running
Running
Upload 2 files
Browse files- github_zip_service.py +163 -155
- router_proxy.py +1 -1
github_zip_service.py
CHANGED
|
@@ -1,155 +1,163 @@
|
|
| 1 |
-
"""授权后的GitHub ZIP交付服务;认证/实时所有权仍由路由负责。"""
|
| 2 |
-
import asyncio
|
| 3 |
-
import os
|
| 4 |
-
from github_archive import cached_archive
|
| 5 |
-
from github_source import github_item_source
|
| 6 |
-
from skill_archive_source import source_header, installation_revision, source_key
|
| 7 |
-
from proxy_resources import ProxyResources
|
| 8 |
-
from workflow_stream_response import OwnedProxyResponse
|
| 9 |
-
from zip_stream import zip_response_length, stream_zip_bytes
|
| 10 |
-
from owned_thread import run_owned_thread
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
async def prepare_github_zip(item, item_id, *, store, token_selector, resolve_commit,
|
| 14 |
-
send, client_factory, transport_factory, error_response,
|
| 15 |
-
cache_max_bytes, direct_max_bytes, signing_key=None):
|
| 16 |
-
# 按服务端资源解析仓库,普通作者只使用本人绑定凭据或匿名GitHub。
|
| 17 |
-
try:
|
| 18 |
-
source = github_item_source(item)
|
| 19 |
-
repo_url = item.get("link", "")
|
| 20 |
-
owner, repo = source["owner"], source["repo"]
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
if
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
cache_headers[
|
| 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 |
-
if
|
| 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 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""授权后的GitHub ZIP交付服务;认证/实时所有权仍由路由负责。"""
|
| 2 |
+
import asyncio
|
| 3 |
+
import os
|
| 4 |
+
from github_archive import cached_archive
|
| 5 |
+
from github_source import github_item_source, repository_parts
|
| 6 |
+
from skill_archive_source import source_header, installation_revision, source_key
|
| 7 |
+
from proxy_resources import ProxyResources
|
| 8 |
+
from workflow_stream_response import OwnedProxyResponse
|
| 9 |
+
from zip_stream import zip_response_length, stream_zip_bytes
|
| 10 |
+
from owned_thread import run_owned_thread
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
async def prepare_github_zip(item, item_id, *, expected_repository, store, token_selector, resolve_commit,
|
| 14 |
+
send, client_factory, transport_factory, error_response,
|
| 15 |
+
cache_max_bytes, direct_max_bytes, signing_key=None):
|
| 16 |
+
# 按服务端资源解析仓库,普通作者只使用本人绑定凭据或匿名GitHub。
|
| 17 |
+
try:
|
| 18 |
+
source = github_item_source(item)
|
| 19 |
+
repo_url = item.get("link", "")
|
| 20 |
+
owner, repo = source["owner"], source["repo"]
|
| 21 |
+
try:
|
| 22 |
+
expected_owner, expected_repo = repository_parts(expected_repository)
|
| 23 |
+
except ValueError:
|
| 24 |
+
expected_owner = expected_repo = None
|
| 25 |
+
if (expected_owner is None or expected_owner.casefold() != owner.casefold()
|
| 26 |
+
or expected_repo.casefold() != repo.casefold()):
|
| 27 |
+
return error_response(content={"error": "资源来源已变更,请刷新资源后重新安装。",
|
| 28 |
+
"error_code": "source_changed"}, status_code=409)
|
| 29 |
+
active_token = await run_owned_thread(token_selector, item)
|
| 30 |
+
skill_revision = installation_revision(item, source_key(source), active_token, signing_key=signing_key) if source['skill'] else None
|
| 31 |
+
except ValueError:
|
| 32 |
+
return error_response(content={"error": "资源仓库或凭据暂时无法核实"}, status_code=503)
|
| 33 |
+
|
| 34 |
+
# ===== ZIP 缓存优先机制(鉴权后、GitHub请求前) =====
|
| 35 |
+
cache_resources = None
|
| 36 |
+
cache_handed_off = False
|
| 37 |
+
try:
|
| 38 |
+
versions_db = await run_owned_thread(store.load_data, "versions.json", default_data={})
|
| 39 |
+
item_version = versions_db.get(item_id)
|
| 40 |
+
archive = cached_archive(item, active_token, item_version, repo_id=store.DATASET_REPO_ID)
|
| 41 |
+
hf_cache_token = os.environ.get("HF_TOKEN")
|
| 42 |
+
if archive and hf_cache_token:
|
| 43 |
+
hf_headers = {"Authorization": f"Bearer {hf_cache_token}"}
|
| 44 |
+
cache_filename = archive["path"]
|
| 45 |
+
cache_url = f"https://huggingface.co/datasets/{archive['repo_id']}/resolve/{archive['hf_commit']}/{cache_filename}"
|
| 46 |
+
cache_client = client_factory(
|
| 47 |
+
follow_redirects=False, trust_env=False, transport=transport_factory(),
|
| 48 |
+
)
|
| 49 |
+
cache_resources = ProxyResources(cache_client)
|
| 50 |
+
head_resp = cache_resources.track(await send(
|
| 51 |
+
cache_client, "HEAD", cache_url, headers=hf_headers, timeout=10))
|
| 52 |
+
if head_resp.status_code == 200:
|
| 53 |
+
await cache_resources.close_response(head_resp)
|
| 54 |
+
stream_resp = cache_resources.track(await send(
|
| 55 |
+
cache_client, "GET", cache_url, headers=hf_headers, timeout=120))
|
| 56 |
+
if stream_resp.status_code == 200:
|
| 57 |
+
# identity响应应与已上传归档大小一致;编码响应仍保持原始透传。
|
| 58 |
+
encoding = (stream_resp.headers.get('content-encoding') or 'identity').strip().lower()
|
| 59 |
+
expected_size = archive['size'] if encoding == 'identity' else None
|
| 60 |
+
cache_length = zip_response_length(stream_resp.headers, max_bytes=cache_max_bytes,
|
| 61 |
+
expected_size=expected_size)
|
| 62 |
+
cache_headers = {}
|
| 63 |
+
if source["skill"]:
|
| 64 |
+
cache_headers["X-Ranking-Skill-Source"] = source_header(source, ())
|
| 65 |
+
cache_headers['X-Ranking-Skill-Revision'] = skill_revision
|
| 66 |
+
content_length = stream_resp.headers.get('content-length')
|
| 67 |
+
if content_length:
|
| 68 |
+
cache_headers["Content-Length"] = content_length
|
| 69 |
+
content_encoding = stream_resp.headers.get('content-encoding')
|
| 70 |
+
if content_encoding:
|
| 71 |
+
cache_headers["Content-Encoding"] = content_encoding
|
| 72 |
+
if item_version.get('hash'):
|
| 73 |
+
cache_headers["X-Installed-Version"] = archive["commit"]
|
| 74 |
+
|
| 75 |
+
async def cache_stream_generator():
|
| 76 |
+
try:
|
| 77 |
+
async for chunk in stream_zip_bytes(stream_resp, max_bytes=cache_max_bytes,
|
| 78 |
+
length=cache_length, expected_size=expected_size):
|
| 79 |
+
yield chunk
|
| 80 |
+
except Exception as error:
|
| 81 |
+
print(f"[ZIP缓存] 响应流中断: error={type(error).__name__}")
|
| 82 |
+
raise
|
| 83 |
+
finally:
|
| 84 |
+
await cache_resources.close()
|
| 85 |
+
|
| 86 |
+
prepared = OwnedProxyResponse(cache_stream_generator(), close_callback=cache_resources.close,
|
| 87 |
+
media_type="application/zip", headers=cache_headers)
|
| 88 |
+
cache_handed_off = True
|
| 89 |
+
return prepared
|
| 90 |
+
print(f"[ZIP缓存] GET失败,回退GitHub: status={stream_resp.status_code}")
|
| 91 |
+
except asyncio.CancelledError:
|
| 92 |
+
raise
|
| 93 |
+
except Exception as error:
|
| 94 |
+
print(f"[ZIP缓存] 查询失败,回退GitHub: error={type(error).__name__}")
|
| 95 |
+
finally:
|
| 96 |
+
if cache_resources is not None and not cache_handed_off:
|
| 97 |
+
await cache_resources.close()
|
| 98 |
+
|
| 99 |
+
# 先取得完整commit,再按它下载;不在下载后另取“最新版本”冒充实际内容。
|
| 100 |
+
live_commit = await resolve_commit(repo_url, active_token)
|
| 101 |
+
if not live_commit:
|
| 102 |
+
return error_response(content={"error": "资源版本暂时无法核实,请稍后重试"}, status_code=502)
|
| 103 |
+
github_zip_api = f"https://api.github.com/repos/{owner}/{repo}/zipball/{live_commit}"
|
| 104 |
+
|
| 105 |
+
headers = {
|
| 106 |
+
"Accept": "application/vnd.github.v3+json",
|
| 107 |
+
"User-Agent": "ComfyUI-Ranking-SaaS"
|
| 108 |
+
}
|
| 109 |
+
if active_token:
|
| 110 |
+
headers["Authorization"] = f"Bearer {active_token}"
|
| 111 |
+
|
| 112 |
+
# 异步请求固定GitHub commit,原始块保持长度与Content-Encoding契约。
|
| 113 |
+
client = client_factory(
|
| 114 |
+
follow_redirects=False, trust_env=False, transport=transport_factory(),
|
| 115 |
+
)
|
| 116 |
+
resources = ProxyResources(client)
|
| 117 |
+
handed_off = False
|
| 118 |
+
try:
|
| 119 |
+
try:
|
| 120 |
+
response = resources.track(await send(
|
| 121 |
+
client, "GET", github_zip_api, headers=headers, timeout=120))
|
| 122 |
+
except Exception as error:
|
| 123 |
+
print(f"[proxy_github_zip] 请求失败: error={type(error).__name__}")
|
| 124 |
+
return error_response(content={"error": "代理下载时发生网络异常,请稍后重试"}, status_code=502)
|
| 125 |
+
if response.status_code != 200:
|
| 126 |
+
status_map = {404: "仓库不存在或无权访问", 401: "Token 无效或已过期", 403: "访问被拒绝(可能触发速率限制)"}
|
| 127 |
+
err_msg = status_map.get(response.status_code, f"GitHub API 返回 HTTP {response.status_code}")
|
| 128 |
+
return error_response(content={"error": f"GITHUB_DOWNLOAD_FAILED: {err_msg}"}, status_code=502)
|
| 129 |
+
declared_length = zip_response_length(response.headers, max_bytes=direct_max_bytes)
|
| 130 |
+
resp_headers = {}
|
| 131 |
+
if source["skill"]:
|
| 132 |
+
resp_headers["X-Ranking-Skill-Source"] = source_header(source, ())
|
| 133 |
+
resp_headers['X-Ranking-Skill-Revision'] = skill_revision
|
| 134 |
+
content_length = response.headers.get('content-length', '')
|
| 135 |
+
if content_length:
|
| 136 |
+
resp_headers["Content-Length"] = content_length
|
| 137 |
+
content_encoding = response.headers.get('content-encoding')
|
| 138 |
+
if content_encoding:
|
| 139 |
+
resp_headers["Content-Encoding"] = content_encoding
|
| 140 |
+
resp_headers["X-Installed-Version"] = live_commit
|
| 141 |
+
|
| 142 |
+
async def stream_generator():
|
| 143 |
+
try:
|
| 144 |
+
async for chunk in stream_zip_bytes(response, max_bytes=direct_max_bytes, length=declared_length):
|
| 145 |
+
yield chunk
|
| 146 |
+
except Exception as error:
|
| 147 |
+
print(f"[proxy_github_zip] 响应流中断: error={type(error).__name__}")
|
| 148 |
+
raise
|
| 149 |
+
finally:
|
| 150 |
+
await resources.close()
|
| 151 |
+
|
| 152 |
+
prepared = OwnedProxyResponse(stream_generator(), close_callback=resources.close,
|
| 153 |
+
media_type="application/zip", headers=resp_headers)
|
| 154 |
+
handed_off = True
|
| 155 |
+
return prepared
|
| 156 |
+
except asyncio.CancelledError:
|
| 157 |
+
raise
|
| 158 |
+
except Exception as error:
|
| 159 |
+
print(f"[proxy_github_zip] 响应准备失败: error={type(error).__name__}")
|
| 160 |
+
return error_response(content={"error": "代理下载时发生网络异常,请稍后重试"}, status_code=502)
|
| 161 |
+
finally:
|
| 162 |
+
if not handed_off:
|
| 163 |
+
await resources.close()
|
router_proxy.py
CHANGED
|
@@ -146,7 +146,7 @@ async def proxy_github_zip(request: Request, req_data: ProxyGithubZipRequest, ac
|
|
| 146 |
from 云端_定时版本检测引擎 import fetch_github_commit
|
| 147 |
return await fetch_github_commit(repo_url, token)
|
| 148 |
|
| 149 |
-
return await prepare_github_zip(item, req_data.item_id, store=json_db,
|
| 150 |
signing_key=JWT_SECRET,
|
| 151 |
token_selector=select_github_token, resolve_commit=resolve_commit,
|
| 152 |
send=_send_with_validated_redirects, client_factory=httpx.AsyncClient,
|
|
|
|
| 146 |
from 云端_定时版本检测引擎 import fetch_github_commit
|
| 147 |
return await fetch_github_commit(repo_url, token)
|
| 148 |
|
| 149 |
+
return await prepare_github_zip(item, req_data.item_id, expected_repository=req_data.url, store=json_db,
|
| 150 |
signing_key=JWT_SECRET,
|
| 151 |
token_selector=select_github_token, resolve_commit=resolve_commit,
|
| 152 |
send=_send_with_validated_redirects, client_factory=httpx.AsyncClient,
|