Merge pull request #1367 from Mai-with-u/dev

Dev
pull/1381/head
墨梓柒 2025-11-19 16:27:43 +08:00 committed by GitHub
commit 96365477f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 118 additions and 54 deletions

View File

@ -1,5 +1,6 @@
"""表情包管理 API 路由"""
from fastapi import APIRouter, HTTPException, Header, Query
from fastapi.responses import FileResponse
from pydantic import BaseModel
from typing import Optional, List
from src.common.logger import get_logger
@ -7,6 +8,7 @@ from src.common.database.database_model import Emoji
from .token_manager import get_token_manager
import json
import time
import os
logger = get_logger("webui.emoji")
@ -481,3 +483,65 @@ async def ban_emoji(
except Exception as e:
logger.exception(f"禁用表情包失败: {e}")
raise HTTPException(status_code=500, detail=f"禁用表情包失败: {str(e)}") from e
@router.get("/{emoji_id}/thumbnail")
async def get_emoji_thumbnail(
emoji_id: int,
token: Optional[str] = Query(None, description="访问令牌"),
authorization: Optional[str] = Header(None)
):
"""
获取表情包缩略图
Args:
emoji_id: 表情包ID
token: 访问令牌通过 query parameter
authorization: Authorization header
Returns:
表情包图片文件
"""
try:
# 优先使用 query parameter 中的 token用于 img 标签)
if token:
token_manager = get_token_manager()
if not token_manager.verify_token(token):
raise HTTPException(status_code=401, detail="Token 无效或已过期")
else:
# 如果没有 query token则验证 Authorization header
verify_auth_token(authorization)
emoji = Emoji.get_or_none(Emoji.id == emoji_id)
if not emoji:
raise HTTPException(status_code=404, detail=f"未找到 ID 为 {emoji_id} 的表情包")
# 检查文件是否存在
if not os.path.exists(emoji.full_path):
raise HTTPException(status_code=404, detail="表情包文件不存在")
# 根据格式设置 MIME 类型
mime_types = {
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'webp': 'image/webp',
'bmp': 'image/bmp'
}
media_type = mime_types.get(emoji.format.lower(), 'application/octet-stream')
return FileResponse(
path=emoji.full_path,
media_type=media_type,
filename=f"{emoji.emoji_hash}.{emoji.format}"
)
except HTTPException:
raise
except Exception as e:
logger.exception(f"获取表情包缩略图失败: {e}")
raise HTTPException(status_code=500, detail=f"获取表情包缩略图失败: {str(e)}") from e

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -5,8 +5,8 @@
<link rel="icon" type="image/x-icon" href="/maimai.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MaiBot Dashboard</title>
<script type="module" crossorigin src="/assets/index-Tw768L2V.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-BWXp50gY.css">
<script type="module" crossorigin src="/assets/index-Cq3PS1TX.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-CRqE9GGs.css">
</head>
<body>
<div id="root"></div>