Spaces:
Running
Running
Upload 2 files
Browse files- router_items.py +61 -1
- 云端_定时版本检测引擎.py +1 -1
router_items.py
CHANGED
|
@@ -355,7 +355,9 @@ async def create_item(item: ItemCreate):
|
|
| 355 |
await precache_github_zip(link, token, new_item["id"], latest_hash)
|
| 356 |
print(f"[缓存刷新] 首次发布预缓存完成: {new_item['id']}")
|
| 357 |
except Exception as e:
|
|
|
|
| 358 |
print(f"[缓存刷新] 首次发布预缓存失败(不影响发布): {e}")
|
|
|
|
| 359 |
|
| 360 |
asyncio.create_task(_first_publish_precache())
|
| 361 |
except Exception as e:
|
|
@@ -854,4 +856,62 @@ async def toggle_item_favorite(item_id: str, current_user: str = Depends(require
|
|
| 854 |
# 🗂️ 清除排序缓存(收藏数变化可能影响排序)
|
| 855 |
sort_cache.invalidate("items:")
|
| 856 |
|
| 857 |
-
return result_container[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
await precache_github_zip(link, token, new_item["id"], latest_hash)
|
| 356 |
print(f"[缓存刷新] 首次发布预缓存完成: {new_item['id']}")
|
| 357 |
except Exception as e:
|
| 358 |
+
import traceback
|
| 359 |
print(f"[缓存刷新] 首次发布预缓存失败(不影响发布): {e}")
|
| 360 |
+
print(f"[缓存刷新] 详细堆栈: {traceback.format_exc()}")
|
| 361 |
|
| 362 |
asyncio.create_task(_first_publish_precache())
|
| 363 |
except Exception as e:
|
|
|
|
| 856 |
# 🗂️ 清除排序缓存(收藏数变化可能影响排序)
|
| 857 |
sort_cache.invalidate("items:")
|
| 858 |
|
| 859 |
+
return result_container[0]
|
| 860 |
+
|
| 861 |
+
|
| 862 |
+
# ==========================================
|
| 863 |
+
# 🔄 手动触发预缓存刷新接口
|
| 864 |
+
# ==========================================
|
| 865 |
+
@router.post("/api/items/{item_id}/refresh_cache")
|
| 866 |
+
async def refresh_item_cache(item_id: str, current_user: str = Depends(require_auth)):
|
| 867 |
+
"""手动触发指定资源的 ZIP 预缓存刷新,仅限作者调用"""
|
| 868 |
+
items_db = db.load_data("items.json", default_data=[])
|
| 869 |
+
|
| 870 |
+
# 查找目标 item
|
| 871 |
+
target_item = None
|
| 872 |
+
for item in items_db:
|
| 873 |
+
if item["id"] == item_id:
|
| 874 |
+
target_item = item
|
| 875 |
+
break
|
| 876 |
+
|
| 877 |
+
if not target_item:
|
| 878 |
+
raise HTTPException(status_code=404, detail="资源不存在")
|
| 879 |
+
|
| 880 |
+
# 权限校验:仅作者可操作
|
| 881 |
+
if target_item.get("author") != current_user:
|
| 882 |
+
raise HTTPException(status_code=403, detail="仅作者可触发缓存刷新")
|
| 883 |
+
|
| 884 |
+
# 仅支持 GitHub 仓库类型
|
| 885 |
+
link = target_item.get("link", "")
|
| 886 |
+
if "github.com" not in link:
|
| 887 |
+
raise HTTPException(status_code=400, detail="仅支持 GitHub 仓库类型的资源")
|
| 888 |
+
|
| 889 |
+
try:
|
| 890 |
+
from 云端_定时版本检测引擎 import fetch_latest_github_hash, precache_github_zip
|
| 891 |
+
except ImportError:
|
| 892 |
+
raise HTTPException(status_code=500, detail="预缓存模块不可用")
|
| 893 |
+
|
| 894 |
+
# 获取 token
|
| 895 |
+
token = target_item.get("github_token") or os.environ.get("GITHUB_PAT", "")
|
| 896 |
+
|
| 897 |
+
# 获取最新 hash
|
| 898 |
+
latest_hash = await fetch_latest_github_hash(link, token)
|
| 899 |
+
if not latest_hash:
|
| 900 |
+
raise HTTPException(status_code=500, detail="获取最新版本号失败,请稍后重试")
|
| 901 |
+
|
| 902 |
+
# 写入 versions.json
|
| 903 |
+
versions_db = db.load_data("versions.json", default_data={})
|
| 904 |
+
entry = versions_db.get(item_id, {})
|
| 905 |
+
if isinstance(entry, str):
|
| 906 |
+
entry = {"hash": entry}
|
| 907 |
+
entry["hash"] = latest_hash
|
| 908 |
+
versions_db[item_id] = entry
|
| 909 |
+
db.save_data("versions.json", versions_db)
|
| 910 |
+
|
| 911 |
+
# 执行预缓存
|
| 912 |
+
success = await precache_github_zip(link, token, item_id, latest_hash)
|
| 913 |
+
|
| 914 |
+
if success:
|
| 915 |
+
return {"status": "success", "message": "缓存刷新完成"}
|
| 916 |
+
else:
|
| 917 |
+
return {"status": "error", "message": "缓存刷新失败,请查看云端日志获取详细信息"}
|
云端_定时版本检测引擎.py
CHANGED
|
@@ -200,7 +200,7 @@ async def precache_github_zip(repo_url: str, token: Optional[str], item_id: str,
|
|
| 200 |
"User-Agent": "ComfyUI-Hub"
|
| 201 |
}
|
| 202 |
if token:
|
| 203 |
-
headers["Authorization"] = f"
|
| 204 |
|
| 205 |
hf_token = os.environ.get("HF_TOKEN")
|
| 206 |
if not hf_token:
|
|
|
|
| 200 |
"User-Agent": "ComfyUI-Hub"
|
| 201 |
}
|
| 202 |
if token:
|
| 203 |
+
headers["Authorization"] = f"Bearer {token}"
|
| 204 |
|
| 205 |
hf_token = os.environ.get("HF_TOKEN")
|
| 206 |
if not hf_token:
|