2026-07-05 09:14:32 +08:00
|
|
|
"""Media signing route handlers."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
|
|
|
from backend.routes.common import handle_request
|
2026-07-11 21:50:34 +08:00
|
|
|
from backend.routes.params import int_or_default, optional_str
|
2026-07-06 03:18:33 +08:00
|
|
|
from backend.services.media_index_service import MediaIndexService
|
2026-07-05 09:14:32 +08:00
|
|
|
from backend.services.media_service import MediaService
|
2026-07-06 03:22:13 +08:00
|
|
|
from backend.services.orphan_media_service import OrphanMediaService
|
2026-07-05 09:14:32 +08:00
|
|
|
from core.identity import RequestContext
|
|
|
|
|
from core.security import require_authenticated
|
2026-07-06 03:22:13 +08:00
|
|
|
from database.db_helper import Database, load_database_settings
|
2026-07-05 09:14:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def sign_media(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
query: dict[str, Any],
|
|
|
|
|
service: MediaService | None = None,
|
|
|
|
|
) -> dict:
|
|
|
|
|
media_service = service or MediaService()
|
|
|
|
|
|
|
|
|
|
def action() -> dict[str, Any]:
|
|
|
|
|
require_authenticated(ctx)
|
|
|
|
|
uri = str(query.get("uri") or "").strip()
|
|
|
|
|
if not uri:
|
|
|
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "uri is required", {"field": "uri"})
|
|
|
|
|
return media_service.sign_media(uri)
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_media(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
query: dict[str, Any],
|
|
|
|
|
service: MediaService | None = None,
|
|
|
|
|
) -> dict:
|
|
|
|
|
media_service = service or MediaService()
|
|
|
|
|
|
|
|
|
|
def action() -> dict[str, Any]:
|
|
|
|
|
token = str(query.get("token") or "").strip()
|
|
|
|
|
if not token:
|
|
|
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "token is required", {"field": "token"})
|
|
|
|
|
return media_service.fetch_media(token)
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
2026-07-06 03:18:33 +08:00
|
|
|
|
|
|
|
|
|
2026-07-15 23:01:07 +08:00
|
|
|
def fetch_media_payload(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
query: dict[str, Any],
|
|
|
|
|
service: MediaService | None = None,
|
|
|
|
|
) -> dict:
|
|
|
|
|
"""Return signed media as JSON for authenticated plugin-page bridges.
|
|
|
|
|
|
|
|
|
|
AstrBot plugin pages run in a sandboxed iframe. Their API bridge carries
|
|
|
|
|
the dashboard login state, while a plain ``<img>`` request to ``/api/plug``
|
|
|
|
|
does not. Keeping this separate from ``fetch_media`` lets browser clients
|
|
|
|
|
create a local Blob URL without making the signed streaming endpoint less
|
|
|
|
|
strict at the AstrBot middleware layer.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
media_service = service or MediaService()
|
|
|
|
|
|
|
|
|
|
def action() -> dict[str, Any]:
|
|
|
|
|
require_authenticated(ctx)
|
|
|
|
|
token = str(query.get("token") or "").strip()
|
|
|
|
|
if not token:
|
|
|
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "token is required", {"field": "token"})
|
|
|
|
|
return media_service.fetch_media(token)
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
2026-07-06 03:18:33 +08:00
|
|
|
def index_media(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
query: dict[str, Any] | None = None,
|
|
|
|
|
service: MediaIndexService | None = None,
|
|
|
|
|
) -> dict:
|
|
|
|
|
params = query or {}
|
|
|
|
|
index_service = service or MediaIndexService(MediaService())
|
|
|
|
|
|
|
|
|
|
def action() -> dict[str, Any]:
|
|
|
|
|
require_authenticated(ctx)
|
|
|
|
|
return index_service.index_files(
|
2026-07-11 21:50:34 +08:00
|
|
|
root=optional_str(params.get("root")),
|
|
|
|
|
limit=int_or_default(params.get("limit"), 500),
|
2026-07-06 03:18:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
2026-07-06 03:22:13 +08:00
|
|
|
def scan_orphan_media(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
query: dict[str, Any] | None = None,
|
|
|
|
|
service: OrphanMediaService | None = None,
|
|
|
|
|
) -> dict:
|
|
|
|
|
params = query or {}
|
|
|
|
|
orphan_service = service or OrphanMediaService(
|
|
|
|
|
Database(load_database_settings()),
|
|
|
|
|
MediaIndexService(MediaService()),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def action() -> dict[str, Any]:
|
|
|
|
|
require_authenticated(ctx)
|
|
|
|
|
return orphan_service.scan(
|
2026-07-11 21:50:34 +08:00
|
|
|
root=optional_str(params.get("root")),
|
|
|
|
|
limit=int_or_default(params.get("limit"), 500),
|
2026-07-06 03:22:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|