Spaces:
Running
Running
Upload 3 files
Browse files- app.py +12 -1
- models.py +7 -1
- router_users.py +11 -1
app.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
from fastapi import FastAPI, File, UploadFile, Form
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import hashlib
|
|
|
|
| 5 |
import 数据库连接 as db
|
| 6 |
|
| 7 |
# 引入拆分后的四大业务模块
|
|
@@ -34,11 +35,21 @@ def read_root():
|
|
| 34 |
async def upload_file(file: UploadFile = File(...), file_type: str = Form(...)):
|
| 35 |
content = await file.read()
|
| 36 |
file_hash = hashlib.md5(content).hexdigest()[:10]
|
|
|
|
| 37 |
new_filename = f"{file_hash}_{file.filename}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
dir_mapping = {"avatar": "avatars", "cover": "covers", "tool": "tools", "app": "apps"}
|
| 39 |
target_dir = dir_mapping.get(file_type, "others")
|
| 40 |
full_path_in_repo = f"{target_dir}/{new_filename}"
|
| 41 |
|
|
|
|
| 42 |
db.save_file(full_path_in_repo, content)
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
return {"status": "success", "url": url, "display_name": file.filename, "hashed_name": new_filename}
|
|
|
|
| 2 |
from fastapi import FastAPI, File, UploadFile, Form
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import hashlib
|
| 5 |
+
import urllib.parse # 【核心新增】:用于处理中文文件名的 URL 编码
|
| 6 |
import 数据库连接 as db
|
| 7 |
|
| 8 |
# 引入拆分后的四大业务模块
|
|
|
|
| 35 |
async def upload_file(file: UploadFile = File(...), file_type: str = Form(...)):
|
| 36 |
content = await file.read()
|
| 37 |
file_hash = hashlib.md5(content).hexdigest()[:10]
|
| 38 |
+
|
| 39 |
new_filename = f"{file_hash}_{file.filename}"
|
| 40 |
+
|
| 41 |
+
# 【核心修复】:由于标准的 HTTP URL 不支持直接包含中文,
|
| 42 |
+
# 我们对文件名进行 URL 安全转码 (如把 '测试' 转为 '%E6%B5%8B%E8%AF%95')
|
| 43 |
+
safe_filename = urllib.parse.quote(file.filename)
|
| 44 |
+
safe_url_filename = f"{file_hash}_{safe_filename}"
|
| 45 |
+
|
| 46 |
dir_mapping = {"avatar": "avatars", "cover": "covers", "tool": "tools", "app": "apps"}
|
| 47 |
target_dir = dir_mapping.get(file_type, "others")
|
| 48 |
full_path_in_repo = f"{target_dir}/{new_filename}"
|
| 49 |
|
| 50 |
+
# 保存文件时使用原始名称(文件系统支持中文)
|
| 51 |
db.save_file(full_path_in_repo, content)
|
| 52 |
+
|
| 53 |
+
# 返回给前端的下载链接,必须使用 URL Encode 编码后的名称,防止客户端 ascii 报错
|
| 54 |
+
url = f"https://huggingface.co/datasets/{db.DATASET_REPO_ID}/resolve/main/{target_dir}/{safe_url_filename}"
|
| 55 |
return {"status": "success", "url": url, "display_name": file.filename, "hashed_name": new_filename}
|
models.py
CHANGED
|
@@ -62,4 +62,10 @@ class FollowToggle(BaseModel):
|
|
| 62 |
class PrivateMessage(BaseModel):
|
| 63 |
sender: str
|
| 64 |
receiver: str
|
| 65 |
-
content: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
class PrivateMessage(BaseModel):
|
| 63 |
sender: str
|
| 64 |
receiver: str
|
| 65 |
+
content: str
|
| 66 |
+
|
| 67 |
+
class PrivacySettings(BaseModel):
|
| 68 |
+
follows: bool
|
| 69 |
+
likes: bool
|
| 70 |
+
favorites: bool
|
| 71 |
+
downloads: bool
|
router_users.py
CHANGED
|
@@ -4,7 +4,7 @@ import time
|
|
| 4 |
import re
|
| 5 |
import 数据库连接 as db
|
| 6 |
from notifications import add_notification
|
| 7 |
-
from models import UserRegister, UserLogin, UserUpdate, PasswordReset, FollowToggle
|
| 8 |
|
| 9 |
router = APIRouter()
|
| 10 |
|
|
@@ -85,4 +85,14 @@ async def toggle_follow(follow: FollowToggle):
|
|
| 85 |
if follow.user_id in target_followers: target_followers.remove(follow.user_id)
|
| 86 |
if follow.target_account in current_following: current_following.remove(follow.target_account)
|
| 87 |
db.save_data("users.json", users_db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
return {"status": "success"}
|
|
|
|
| 4 |
import re
|
| 5 |
import 数据库连接 as db
|
| 6 |
from notifications import add_notification
|
| 7 |
+
from models import UserRegister, UserLogin, UserUpdate, PasswordReset, FollowToggle, PrivacySettings
|
| 8 |
|
| 9 |
router = APIRouter()
|
| 10 |
|
|
|
|
| 85 |
if follow.user_id in target_followers: target_followers.remove(follow.user_id)
|
| 86 |
if follow.target_account in current_following: current_following.remove(follow.target_account)
|
| 87 |
db.save_data("users.json", users_db)
|
| 88 |
+
return {"status": "success"}
|
| 89 |
+
|
| 90 |
+
@router.put("/api/users/{account}/privacy")
|
| 91 |
+
async def update_privacy(account: str, privacy: PrivacySettings):
|
| 92 |
+
users_db = db.load_data("users.json", default_data={})
|
| 93 |
+
if account not in users_db:
|
| 94 |
+
raise HTTPException(status_code=404, detail="用户不存在")
|
| 95 |
+
|
| 96 |
+
users_db[account]["privacy"] = privacy.dict()
|
| 97 |
+
db.save_data("users.json", users_db)
|
| 98 |
return {"status": "success"}
|