Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
# ⚙️ 后端逻辑/核心服务端.py (Hugging Face Spaces app.py)
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import Optional
|
| 6 |
import time
|
| 7 |
import uuid
|
|
|
|
| 8 |
|
| 9 |
# 导入数据库操作模块
|
| 10 |
import 数据库连接 as db
|
|
@@ -25,6 +26,40 @@ app.add_middleware(
|
|
| 25 |
def read_root():
|
| 26 |
return {"status": "ok", "message": "ComfyUI Ranking API is running!"}
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# --- 数据模型 ---
|
| 29 |
class UserRegister(BaseModel):
|
| 30 |
account: str # 唯一账号 (新增为主键)
|
|
@@ -51,17 +86,28 @@ class InteractionToggle(BaseModel):
|
|
| 51 |
|
| 52 |
class CommentCreate(BaseModel):
|
| 53 |
item_id: str
|
| 54 |
-
author: str # 存储用户的 account 或 name
|
| 55 |
content: str
|
| 56 |
reply_to_user: Optional[str] = None
|
| 57 |
parent_id: Optional[str] = None
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# --- 1. 用户系统 ---
|
| 60 |
@app.post("/api/users/register")
|
| 61 |
async def register_user(user: UserRegister):
|
| 62 |
users_db = db.load_data("users.json", default_data={})
|
| 63 |
|
| 64 |
-
#
|
| 65 |
if user.account in users_db:
|
| 66 |
raise HTTPException(status_code=400, detail="该账号已被注册,请尝试其他账号名")
|
| 67 |
|
|
@@ -134,6 +180,39 @@ async def get_items(type: str = "tool", sort: str = "time", limit: int = 20):
|
|
| 134 |
|
| 135 |
return {"status": "success", "data": filtered_items[:limit]}
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
# --- 3. 社交与互动 (点赞/收藏防抖与排重) ---
|
| 138 |
@app.post("/api/interactions/toggle")
|
| 139 |
async def toggle_interaction(interaction: InteractionToggle):
|
|
|
|
| 1 |
# ⚙️ 后端逻辑/核心服务端.py (Hugging Face Spaces app.py)
|
| 2 |
+
from fastapi import FastAPI, HTTPException, File, UploadFile, Form
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import Optional
|
| 6 |
import time
|
| 7 |
import uuid
|
| 8 |
+
import hashlib
|
| 9 |
|
| 10 |
# 导入数据库操作模块
|
| 11 |
import 数据库连接 as db
|
|
|
|
| 26 |
def read_root():
|
| 27 |
return {"status": "ok", "message": "ComfyUI Ranking API is running!"}
|
| 28 |
|
| 29 |
+
# --- 【新增】:通用文件上传与哈希分类接口 ---
|
| 30 |
+
@app.post("/api/upload")
|
| 31 |
+
async def upload_file(file: UploadFile = File(...), file_type: str = Form(...)):
|
| 32 |
+
content = await file.read()
|
| 33 |
+
|
| 34 |
+
# 计算文件内容的 MD5 哈希值,截取前10位保证唯一性
|
| 35 |
+
file_hash = hashlib.md5(content).hexdigest()[:10]
|
| 36 |
+
original_name = file.filename
|
| 37 |
+
# 将哈希值拼接到文件名前,防止同名文件覆盖 (后台物理名称)
|
| 38 |
+
new_filename = f"{file_hash}_{original_name}"
|
| 39 |
+
|
| 40 |
+
# 根据文件类型,物理隔离到不同的目录
|
| 41 |
+
dir_mapping = {
|
| 42 |
+
"avatar": "avatars",
|
| 43 |
+
"cover": "covers",
|
| 44 |
+
"tool": "tools",
|
| 45 |
+
"app": "apps"
|
| 46 |
+
}
|
| 47 |
+
target_dir = dir_mapping.get(file_type, "others")
|
| 48 |
+
full_path_in_repo = f"{target_dir}/{new_filename}"
|
| 49 |
+
|
| 50 |
+
# 将真实文件存入数据库(Dataset)
|
| 51 |
+
db.save_file(full_path_in_repo, content)
|
| 52 |
+
|
| 53 |
+
# 组装供前端直接下载或显示的原始直连 URL
|
| 54 |
+
url = f"https://huggingface.co/datasets/{db.DATASET_REPO_ID}/resolve/main/{full_path_in_repo}"
|
| 55 |
+
|
| 56 |
+
return {
|
| 57 |
+
"status": "success",
|
| 58 |
+
"url": url, # 前端 src 或 href 使用的真实链接
|
| 59 |
+
"display_name": original_name, # 供前端 UI 干净展示的文件名
|
| 60 |
+
"hashed_name": new_filename # 实际带哈希的文件名
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
# --- 数据模型 ---
|
| 64 |
class UserRegister(BaseModel):
|
| 65 |
account: str # 唯一账号 (新增为主键)
|
|
|
|
| 86 |
|
| 87 |
class CommentCreate(BaseModel):
|
| 88 |
item_id: str
|
| 89 |
+
author: str # 存储用户的 account 或 name
|
| 90 |
content: str
|
| 91 |
reply_to_user: Optional[str] = None
|
| 92 |
parent_id: Optional[str] = None
|
| 93 |
|
| 94 |
+
# 【核心修改】:新增发布项目的数据模型
|
| 95 |
+
class ItemCreate(BaseModel):
|
| 96 |
+
type: str
|
| 97 |
+
title: str
|
| 98 |
+
shortDesc: str
|
| 99 |
+
fullDesc: str
|
| 100 |
+
link: str
|
| 101 |
+
coverUrl: Optional[str] = None
|
| 102 |
+
author: str
|
| 103 |
+
price: float = 0.0 # 标价/积分字段
|
| 104 |
+
|
| 105 |
# --- 1. 用户系统 ---
|
| 106 |
@app.post("/api/users/register")
|
| 107 |
async def register_user(user: UserRegister):
|
| 108 |
users_db = db.load_data("users.json", default_data={})
|
| 109 |
|
| 110 |
+
# 通过 account 查重
|
| 111 |
if user.account in users_db:
|
| 112 |
raise HTTPException(status_code=400, detail="该账号已被注册,请尝试其他账号名")
|
| 113 |
|
|
|
|
| 180 |
|
| 181 |
return {"status": "success", "data": filtered_items[:limit]}
|
| 182 |
|
| 183 |
+
# 【核心修改】:处理前端发布的新插件/应用请求
|
| 184 |
+
@app.post("/api/items")
|
| 185 |
+
async def create_item(item: ItemCreate):
|
| 186 |
+
items_db = db.load_data("items.json", default_data=[])
|
| 187 |
+
|
| 188 |
+
# 生成独一无二的项目 ID (如: tool_169..._a1b2c3)
|
| 189 |
+
new_id = f"{item.type}_{int(time.time())}_{uuid.uuid4().hex[:6]}"
|
| 190 |
+
|
| 191 |
+
new_item = {
|
| 192 |
+
"id": new_id,
|
| 193 |
+
"type": item.type,
|
| 194 |
+
"title": item.title,
|
| 195 |
+
"author": item.author,
|
| 196 |
+
"shortDesc": item.shortDesc,
|
| 197 |
+
"fullDesc": item.fullDesc,
|
| 198 |
+
"link": item.link,
|
| 199 |
+
"coverUrl": item.coverUrl,
|
| 200 |
+
"price": item.price,
|
| 201 |
+
"likes": 0,
|
| 202 |
+
"favorites": 0,
|
| 203 |
+
"comments": 0,
|
| 204 |
+
"uses": 0,
|
| 205 |
+
"created_at": int(time.time()),
|
| 206 |
+
"liked_by": [],
|
| 207 |
+
"favorited_by": []
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
# 核心:将新项目插入到列表的【最前面】(第0位),保证 "最新发布" 排在第一
|
| 211 |
+
items_db.insert(0, new_item)
|
| 212 |
+
db.save_data("items.json", items_db)
|
| 213 |
+
|
| 214 |
+
return {"status": "success", "data": new_item}
|
| 215 |
+
|
| 216 |
# --- 3. 社交与互动 (点赞/收藏防抖与排重) ---
|
| 217 |
@app.post("/api/interactions/toggle")
|
| 218 |
async def toggle_interaction(interaction: InteractionToggle):
|