refactor(assets_proxy): use decorators to simplify code
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .auth import router as auth_router
|
||||
from .fetcher import fetcher_router as fetcher_router
|
||||
from .file import file_router as file_router
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import re
|
||||
from typing import Annotated, Literal
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.dependencies.fetcher import Fetcher
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.dependencies.storage import StorageService as StorageServiceDep
|
||||
from app.storage import LocalStorageService
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
"""LIO (Legacy IO) router for osu-server-spectator compatibility."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.config import settings
|
||||
from app.database.notification import Notification, UserNotification
|
||||
from app.database.user import User
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Awaitable, Callable
|
||||
from math import ceil
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated, Any, Literal, Self
|
||||
|
||||
from app.database.chat import (
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.database import ChatMessageResp
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import Annotated, overload
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.config import settings
|
||||
|
||||
from . import admin, audio_proxy, avatar, beatmapset, cover, oauth, relationship, score, team, username # noqa: F401
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.database.auth import OAuthToken
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
提供从osu!官方获取beatmapset音频预览的代理服务
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.dependencies.database import get_redis, get_redis_binary
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from typing import Annotated
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.database.beatmap import Beatmap
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from typing import Annotated
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import secrets
|
||||
from typing import Annotated
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.database import Relationship
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.dependencies.rate_limit import LIMITERS
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.database.score import Score
|
||||
from app.dependencies.database import Database, Redis
|
||||
from app.dependencies.storage import StorageService
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from typing import Annotated
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.auth import (
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.auth import validate_username
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import urllib.parse
|
||||
|
||||
from app.config import settings
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from . import beatmap, public_user, replay, score, user # noqa: F401
|
||||
from .public_router import public_router as api_v1_public_router
|
||||
from .router import router as api_v1_router
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Annotated, Literal
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated, Literal
|
||||
|
||||
from app.database.statistics import UserStatistics
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
from datetime import date
|
||||
from typing import Annotated, Literal
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Annotated, Literal
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Annotated, Literal
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from . import ( # noqa: F401
|
||||
beatmap,
|
||||
beatmapset,
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from typing import Annotated, Literal
|
||||
from urllib.parse import parse_qs
|
||||
@@ -12,8 +10,8 @@ from app.dependencies.database import Database, Redis, with_db
|
||||
from app.dependencies.fetcher import Fetcher
|
||||
from app.dependencies.geoip import IPAddress, get_geoip_helper
|
||||
from app.dependencies.user import ClientUser, get_current_user
|
||||
from app.helpers.asset_proxy_helper import asset_proxy_response
|
||||
from app.models.beatmap import SearchQueryModel
|
||||
from app.service.asset_proxy_helper import process_response_assets
|
||||
from app.service.beatmapset_cache_service import generate_hash
|
||||
|
||||
from .router import router
|
||||
@@ -45,8 +43,9 @@ async def _save_to_db(sets: SearchBeatmapsetsResp):
|
||||
tags=["谱面集"],
|
||||
response_model=SearchBeatmapsetsResp,
|
||||
)
|
||||
@asset_proxy_response
|
||||
async def search_beatmapset(
|
||||
query: Annotated[SearchQueryModel, Query(...)],
|
||||
query: Annotated[SearchQueryModel, Query()],
|
||||
request: Request,
|
||||
background_tasks: BackgroundTasks,
|
||||
current_user: Annotated[User, Security(get_current_user, scopes=["public"])],
|
||||
@@ -102,9 +101,7 @@ async def search_beatmapset(
|
||||
cached_result = await cache_service.get_search_from_cache(query_hash, cursor_hash)
|
||||
if cached_result:
|
||||
sets = SearchBeatmapsetsResp(**cached_result)
|
||||
# 处理资源代理
|
||||
processed_sets = await process_response_assets(sets)
|
||||
return processed_sets
|
||||
return sets
|
||||
|
||||
try:
|
||||
sets = await fetcher.search_beatmapset(query, cursor, redis)
|
||||
@@ -112,10 +109,7 @@ async def search_beatmapset(
|
||||
|
||||
# 缓存搜索结果
|
||||
await cache_service.cache_search_result(query_hash, cursor_hash, sets.model_dump())
|
||||
|
||||
# 处理资源代理
|
||||
processed_sets = await process_response_assets(sets)
|
||||
return processed_sets
|
||||
return sets
|
||||
except HTTPError as e:
|
||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||
|
||||
@@ -127,6 +121,7 @@ async def search_beatmapset(
|
||||
response_model=BeatmapsetResp,
|
||||
description=("通过谱面 ID 查询所属谱面集。"),
|
||||
)
|
||||
@asset_proxy_response
|
||||
async def lookup_beatmapset(
|
||||
db: Database,
|
||||
request: Request,
|
||||
@@ -138,9 +133,7 @@ async def lookup_beatmapset(
|
||||
# 先尝试从缓存获取
|
||||
cached_resp = await cache_service.get_beatmap_lookup_from_cache(beatmap_id)
|
||||
if cached_resp:
|
||||
# 处理资源代理
|
||||
processed_resp = await process_response_assets(cached_resp)
|
||||
return processed_resp
|
||||
return cached_resp
|
||||
|
||||
try:
|
||||
beatmap = await Beatmap.get_or_fetch(db, fetcher, bid=beatmap_id)
|
||||
@@ -148,10 +141,7 @@ async def lookup_beatmapset(
|
||||
|
||||
# 缓存结果
|
||||
await cache_service.cache_beatmap_lookup(beatmap_id, resp)
|
||||
|
||||
# 处理资源代理
|
||||
processed_resp = await process_response_assets(resp)
|
||||
return processed_resp
|
||||
return resp
|
||||
except HTTPError as exc:
|
||||
raise HTTPException(status_code=404, detail="Beatmap not found") from exc
|
||||
|
||||
@@ -163,6 +153,7 @@ async def lookup_beatmapset(
|
||||
response_model=BeatmapsetResp,
|
||||
description="获取单个谱面集详情。",
|
||||
)
|
||||
@asset_proxy_response
|
||||
async def get_beatmapset(
|
||||
db: Database,
|
||||
request: Request,
|
||||
@@ -174,9 +165,7 @@ async def get_beatmapset(
|
||||
# 先尝试从缓存获取
|
||||
cached_resp = await cache_service.get_beatmapset_from_cache(beatmapset_id)
|
||||
if cached_resp:
|
||||
# 处理资源代理
|
||||
processed_resp = await process_response_assets(cached_resp)
|
||||
return processed_resp
|
||||
return cached_resp
|
||||
|
||||
try:
|
||||
beatmapset = await Beatmapset.get_or_fetch(db, fetcher, beatmapset_id)
|
||||
@@ -184,10 +173,7 @@ async def get_beatmapset(
|
||||
|
||||
# 缓存结果
|
||||
await cache_service.cache_beatmapset(resp)
|
||||
|
||||
# 处理资源代理
|
||||
processed_resp = await process_response_assets(resp)
|
||||
return processed_resp
|
||||
return resp
|
||||
except HTTPError as exc:
|
||||
raise HTTPException(status_code=404, detail="Beatmapset not found") from exc
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
提供缓存统计、清理和预热功能
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.dependencies.database import Redis
|
||||
from app.service.user_cache_service import get_user_cache_service
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.database import MeResp, User
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from app.config import settings
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated, Literal
|
||||
|
||||
from app.config import settings
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.database import Relationship, RelationshipResp, RelationshipType, User
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC
|
||||
from typing import Annotated, Literal
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from app.dependencies.rate_limit import LIMITERS
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, date
|
||||
import time
|
||||
from typing import Annotated
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
会话验证路由 - 实现类似 osu! 的邮件验证流程 (API v2)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated, Literal
|
||||
|
||||
from app.auth import check_totp_backup_code, verify_totp_key_with_replay_protection
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from app.database.beatmap import Beatmap
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import Annotated, Literal
|
||||
|
||||
@@ -19,10 +17,10 @@ from app.database.user import SEARCH_INCLUDED
|
||||
from app.dependencies.api_version import APIVersion
|
||||
from app.dependencies.database import Database, get_redis
|
||||
from app.dependencies.user import get_current_user
|
||||
from app.helpers.asset_proxy_helper import asset_proxy_response
|
||||
from app.log import log
|
||||
from app.models.score import GameMode
|
||||
from app.models.user import BeatmapsetType
|
||||
from app.service.asset_proxy_helper import process_response_assets
|
||||
from app.service.user_cache_service import get_user_cache_service
|
||||
from app.utils import utcnow
|
||||
|
||||
@@ -47,6 +45,7 @@ class BatchUserResponse(BaseModel):
|
||||
)
|
||||
@router.get("/users/lookup", response_model=BatchUserResponse, include_in_schema=False)
|
||||
@router.get("/users/lookup/", response_model=BatchUserResponse, include_in_schema=False)
|
||||
@asset_proxy_response
|
||||
async def get_users(
|
||||
session: Database,
|
||||
request: Request,
|
||||
@@ -89,28 +88,25 @@ async def get_users(
|
||||
# 异步缓存,不阻塞响应
|
||||
background_task.add_task(cache_service.cache_user, user_resp)
|
||||
|
||||
# 处理资源代理
|
||||
response = BatchUserResponse(users=cached_users)
|
||||
processed_response = await process_response_assets(response)
|
||||
return processed_response
|
||||
return response
|
||||
else:
|
||||
searched_users = (await session.exec(select(User).limit(50))).all()
|
||||
users = []
|
||||
for searched_user in searched_users:
|
||||
if searched_user.id != BANCHOBOT_ID:
|
||||
user_resp = await UserResp.from_db(
|
||||
searched_user,
|
||||
session,
|
||||
include=SEARCH_INCLUDED,
|
||||
)
|
||||
users.append(user_resp)
|
||||
# 异步缓存
|
||||
background_task.add_task(cache_service.cache_user, user_resp)
|
||||
if searched_user.id == BANCHOBOT_ID:
|
||||
continue
|
||||
user_resp = await UserResp.from_db(
|
||||
searched_user,
|
||||
session,
|
||||
include=SEARCH_INCLUDED,
|
||||
)
|
||||
users.append(user_resp)
|
||||
# 异步缓存
|
||||
background_task.add_task(cache_service.cache_user, user_resp)
|
||||
|
||||
# 处理资源代理
|
||||
response = BatchUserResponse(users=users)
|
||||
processed_response = await process_response_assets(response)
|
||||
return processed_response
|
||||
return response
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -176,6 +172,7 @@ async def get_user_kudosu(
|
||||
description="通过用户 ID 或用户名获取单个用户的详细信息,并指定特定 ruleset。",
|
||||
tags=["用户"],
|
||||
)
|
||||
@asset_proxy_response
|
||||
async def get_user_info_ruleset(
|
||||
session: Database,
|
||||
background_task: BackgroundTasks,
|
||||
@@ -224,6 +221,7 @@ async def get_user_info_ruleset(
|
||||
description="通过用户 ID 或用户名获取单个用户的详细信息。",
|
||||
tags=["用户"],
|
||||
)
|
||||
@asset_proxy_response
|
||||
async def get_user_info(
|
||||
background_task: BackgroundTasks,
|
||||
session: Database,
|
||||
@@ -239,9 +237,7 @@ async def get_user_info(
|
||||
user_id_int = int(user_id)
|
||||
cached_user = await cache_service.get_user_from_cache(user_id_int)
|
||||
if cached_user:
|
||||
# 处理资源代理
|
||||
processed_user = await process_response_assets(cached_user)
|
||||
return processed_user
|
||||
return cached_user
|
||||
|
||||
searched_user = (
|
||||
await session.exec(
|
||||
@@ -262,9 +258,7 @@ async def get_user_info(
|
||||
# 异步缓存结果
|
||||
background_task.add_task(cache_service.cache_user, user_resp)
|
||||
|
||||
# 处理资源代理
|
||||
processed_user = await process_response_assets(user_resp)
|
||||
return processed_user
|
||||
return user_resp
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -274,6 +268,7 @@ async def get_user_info(
|
||||
description="获取指定用户特定类型的谱面集列表,如最常游玩、收藏等。",
|
||||
tags=["用户"],
|
||||
)
|
||||
@asset_proxy_response
|
||||
async def get_user_beatmapsets(
|
||||
session: Database,
|
||||
background_task: BackgroundTasks,
|
||||
@@ -354,6 +349,7 @@ async def get_user_beatmapsets(
|
||||
),
|
||||
tags=["用户"],
|
||||
)
|
||||
@asset_proxy_response
|
||||
async def get_user_scores(
|
||||
request: Request,
|
||||
session: Database,
|
||||
@@ -381,8 +377,7 @@ async def get_user_scores(
|
||||
user_id, type, include_fails, mode, limit, offset, is_legacy_api
|
||||
)
|
||||
if cached_scores is not None:
|
||||
processed_scores = await process_response_assets(cached_scores)
|
||||
return processed_scores
|
||||
return cached_scores
|
||||
|
||||
db_user = await session.get(User, user_id)
|
||||
if not db_user or db_user.id == BANCHOBOT_ID:
|
||||
@@ -437,6 +432,4 @@ async def get_user_scores(
|
||||
is_legacy_api,
|
||||
)
|
||||
|
||||
# 处理资源代理
|
||||
processed_scores = await process_response_assets(score_responses)
|
||||
return processed_scores
|
||||
return score_responses
|
||||
|
||||
Reference in New Issue
Block a user