项目改造
This commit is contained in:
@@ -24,7 +24,7 @@ from ..services.equipment_store import (
|
||||
list_stratagems_page,
|
||||
update_stratagem,
|
||||
)
|
||||
from ..utils.icon_utils import TYPE_COLORS, save_icon_png, svg_to_png
|
||||
from ..utils.icon_utils import save_icon_png, svg_to_png
|
||||
|
||||
basic_path = Path(__file__).resolve().parents[1]
|
||||
|
||||
@@ -39,10 +39,10 @@ def build_admin_app() -> FastAPI | None:
|
||||
auth = require_admin
|
||||
|
||||
@app.get("/stratagems")
|
||||
async def api_list(page: int = 1, page_size: int = 20, _: dict = auth):
|
||||
async def api_list(page: int = 1, page_size: int = 20, q: str = "", type: str = "", backpack: str = "", _: dict = auth):
|
||||
page = max(1, page)
|
||||
page_size = min(max(1, page_size), 100)
|
||||
items, total = list_stratagems_page(page, page_size)
|
||||
items, total = list_stratagems_page(page, page_size, keyword=q, type=type, backpack=backpack)
|
||||
return {"items": items, "total": total, "page": page, "page_size": page_size}
|
||||
|
||||
@app.post("/stratagems")
|
||||
@@ -86,8 +86,17 @@ def build_admin_app() -> FastAPI | None:
|
||||
|
||||
@app.get("/img/{path:path}")
|
||||
async def icon_file(path: str):
|
||||
# 兼容多种存量路径:res/img/...(插件根相对,新)、img/...(旧库,res/img 相对)、
|
||||
# 以及直接/相对 res/img 的路径,统一归一化到 res/img 下的相对路径再解析。
|
||||
rel = path.replace("\\", "/").lstrip("/")
|
||||
if rel.startswith("res/img/"):
|
||||
rel = rel[len("res/img/"):]
|
||||
elif rel.startswith("res/"):
|
||||
rel = rel[len("res/"):]
|
||||
elif rel.startswith("img/"):
|
||||
rel = rel[len("img/"):]
|
||||
base = (basic_path / "res" / "img").resolve()
|
||||
target = (basic_path / "res" / "img" / path).resolve()
|
||||
target = (basic_path / "res" / "img" / rel).resolve()
|
||||
if not target.is_relative_to(base) or not target.exists():
|
||||
raise HTTPException(status_code=404, detail="图片不存在")
|
||||
return FileResponse(target)
|
||||
@@ -96,13 +105,11 @@ def build_admin_app() -> FastAPI | None:
|
||||
async def api_upload_icon(seed: int, request: Request, _: dict = auth):
|
||||
body = await _get_body(request)
|
||||
svg = (body.get("svg") or "").strip()
|
||||
framed = bool(body.get("framed"))
|
||||
item = get_stratagem(seed)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="未找到该战备")
|
||||
frame_color = TYPE_COLORS.get(item.get("type"), "#6bb7ea")
|
||||
try:
|
||||
png = await svg_to_png(svg, framed=framed, frame_color=frame_color)
|
||||
png = await svg_to_png(svg)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user