"""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("/", 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, max_age=86400, )