37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
"""Public, signed image files for generated-HTML backgrounds."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from fastapi.responses import FileResponse
|
|
|
|
from deerflow.html_runtime.public_images import (
|
|
image_media_type,
|
|
resolve_html_image_path,
|
|
verify_html_image_token,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/public/html-images", tags=["html-images"])
|
|
|
|
_PUBLIC_HEADERS = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Cross-Origin-Resource-Policy": "cross-origin",
|
|
"X-Content-Type-Options": "nosniff",
|
|
"Cache-Control": "public, max-age=86400",
|
|
}
|
|
|
|
|
|
@router.get("/{token}")
|
|
async def get_html_image(token: str) -> FileResponse:
|
|
"""Serve one raster image referenced by a generated HTML page."""
|
|
try:
|
|
thread_id, kind, filename = verify_html_image_token(token)
|
|
path = resolve_html_image_path(thread_id, kind, filename)
|
|
except (ValueError, FileNotFoundError):
|
|
raise HTTPException(status_code=404, detail="HTML image not found") from None
|
|
return FileResponse(
|
|
path,
|
|
media_type=image_media_type(filename),
|
|
headers=_PUBLIC_HEADERS,
|
|
)
|