This commit is contained in:
lzcode
2026-10-16 14:01:02 +08:00
parent 8fc899d0a9
commit 24d63e6702
3 changed files with 104 additions and 2 deletions
Binary file not shown.
+42
View File
@@ -174,6 +174,48 @@ async def upload_video_file(request: Request, token: str = "",
})
@router.post("/upload/image")
async def upload_admin_image(request: Request,
admin: AdminIdentity = Depends(require_super)):
"""admin 通用图片上传:封面/头像/系列图等。存到 runtime/uploads/images/{YYYYMMDD}/,
返回 h5 域名可直接访问的完整 URL。"""
from fastapi import UploadFile, HTTPException
from datetime import datetime
from pathlib import Path
from uuid import uuid4
form = await request.form()
files = [v for v in form.values() if hasattr(v, "filename") and v.filename]
if not files:
raise HTTPException(400, "缺少 file 字段")
upload: UploadFile = files[0]
allowed = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
suffix = Path(upload.filename or "").suffix.lower()
if suffix not in allowed:
raise HTTPException(400, f"仅支持 {','.join(sorted(allowed))}")
PROJECT_ROOT = Path("/root/project/video")
day = datetime.now().strftime("%Y%m%d")
out_dir = PROJECT_ROOT / "runtime" / "uploads" / "images" / day
out_dir.mkdir(parents=True, exist_ok=True)
file_name = f"{uuid4().hex}{suffix}"
file_path = out_dir / file_name
size = 0
with open(file_path, "wb") as f:
while True:
chunk = await upload.read(1024 * 1024)
if not chunk:
break
size += len(chunk)
f.write(chunk)
await upload.close()
url = f"https://h5.256kb.top/uploads/images/{day}/{file_name}"
return ok({"url": url, "file_size": size})
# ---------- 用户 / 积分 ----------
@router.get("/users")