添加音频代理
This commit is contained in:
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
|
||||
from app.config import settings
|
||||
|
||||
from . import avatar, beatmapset_ratings, cover, oauth, relationship, team, username # noqa: F401
|
||||
from . import audio_proxy, avatar, beatmapset_ratings, cover, oauth, relationship, team, username # noqa: F401
|
||||
from .router import router as private_router
|
||||
|
||||
if settings.enable_totp_verification:
|
||||
|
||||
65
app/router/private/audio_proxy.py
Normal file
65
app/router/private/audio_proxy.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""
|
||||
音频代理接口
|
||||
提供从osu!官方获取beatmapset音频预览的代理服务
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.dependencies.database import get_redis
|
||||
from app.service.audio_proxy_service import AudioProxyService, get_audio_proxy_service
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path
|
||||
from fastapi.responses import Response
|
||||
from loguru import logger
|
||||
import redis.asyncio as redis
|
||||
|
||||
router = APIRouter(prefix="/audio", tags=["Audio Proxy"])
|
||||
|
||||
|
||||
async def get_audio_proxy_dependency(redis_client: Annotated[redis.Redis, Depends(get_redis)]) -> AudioProxyService:
|
||||
"""音频代理服务依赖注入"""
|
||||
return get_audio_proxy_service(redis_client)
|
||||
|
||||
|
||||
|
||||
|
||||
@router.get("/beatmapset/{beatmapset_id}")
|
||||
async def get_beatmapset_audio(
|
||||
beatmapset_id: Annotated[int, Path(description="谱面集ID", ge=1)],
|
||||
audio_service: Annotated[AudioProxyService, Depends(get_audio_proxy_dependency)],
|
||||
):
|
||||
"""
|
||||
获取谱面集的音频预览
|
||||
|
||||
根据谱面集ID获取osu!官方的音频预览文件。
|
||||
音频文件会被缓存7天以提高响应速度。
|
||||
|
||||
参数:
|
||||
- beatmapset_id: 谱面集ID
|
||||
|
||||
返回:
|
||||
- 音频文件的二进制数据,Content-Type为audio/mpeg
|
||||
"""
|
||||
try:
|
||||
# 获取谱面集音频数据
|
||||
audio_data, content_type = await audio_service.get_beatmapset_audio(beatmapset_id)
|
||||
|
||||
# 返回音频响应
|
||||
return Response(
|
||||
content=audio_data,
|
||||
media_type=content_type,
|
||||
headers={
|
||||
"Cache-Control": "public, max-age=604800", # 7天缓存
|
||||
"Content-Length": str(len(audio_data)),
|
||||
"Content-Disposition": f"inline; filename=\"{beatmapset_id}.mp3\"",
|
||||
},
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
# 重新抛出HTTP异常
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error getting beatmapset audio: {e}")
|
||||
raise HTTPException(status_code=500, detail="Internal server error") from e
|
||||
@@ -5,3 +5,8 @@ from app.dependencies.rate_limit import LIMITERS
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(prefix="/api/private", dependencies=LIMITERS)
|
||||
|
||||
# 导入并包含子路由
|
||||
from .audio_proxy import router as audio_proxy_router
|
||||
|
||||
router.include_router(audio_proxy_router)
|
||||
|
||||
Reference in New Issue
Block a user