4c77eab84b
Quart's send_file uses cache_timeout= not max_age=. The TypeError on every /api/images/<id> request caused a 500, which the browser rendered as alt text. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
"""Serve locally-cached images.
|
|
|
|
No authentication required — image IDs are opaque and unguessable (based
|
|
on the SHA-256 of the original URL). Images are served with a 1-day
|
|
Cache-Control header so the browser doesn't re-request on every page load.
|
|
"""
|
|
|
|
import logging
|
|
|
|
from quart import Blueprint, jsonify, send_file
|
|
|
|
from fabledassistant.services.images import get_image_path, get_image_record
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
images_bp = Blueprint("images", __name__, url_prefix="/api/images")
|
|
|
|
|
|
@images_bp.route("/<int:image_id>", methods=["GET"])
|
|
async def serve_image(image_id: int):
|
|
"""Serve a locally-cached image by its DB ID."""
|
|
record = await get_image_record(image_id)
|
|
if record is None:
|
|
return jsonify({"error": "Image not found"}), 404
|
|
|
|
file_path = get_image_path(record)
|
|
if not file_path.exists():
|
|
logger.warning("Image file missing on disk for id=%d (%s)", image_id, file_path)
|
|
return jsonify({"error": "Image file not found"}), 404
|
|
|
|
return await send_file(
|
|
file_path,
|
|
mimetype=record.content_type,
|
|
cache_timeout=86400,
|
|
)
|