# ⚙️ 后端逻辑/核心服务端.py (Hugging Face Spaces app.py) from fastapi import FastAPI, File, UploadFile, Form from fastapi.middleware.cors import CORSMiddleware import hashlib import 数据库连接 as db # 引入抽离出去的业务路由 from routers import router app = FastAPI(title="ComfyUI Ranking Community API") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 挂载业务路由 app.include_router(router) @app.get("/") def read_root(): return {"status": "ok", "message": "API is running and fully modularized!"} @app.post("/api/upload") async def upload_file(file: UploadFile = File(...), file_type: str = Form(...)): content = await file.read() file_hash = hashlib.md5(content).hexdigest()[:10] new_filename = f"{file_hash}_{file.filename}" dir_mapping = {"avatar": "avatars", "cover": "covers", "tool": "tools", "app": "apps"} target_dir = dir_mapping.get(file_type, "others") full_path_in_repo = f"{target_dir}/{new_filename}" db.save_file(full_path_in_repo, content) url = f"https://huggingface.co/datasets/{db.DATASET_REPO_ID}/resolve/main/{full_path_in_repo}" return {"status": "success", "url": url, "display_name": file.filename, "hashed_name": new_filename}