"""Serve locally-cached images.""" import logging from quart import Blueprint, jsonify, send_file from fabledassistant.auth import login_required 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"]) @login_required 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, )