"""Recording frame endpoints.""" import os import glob from flask import Blueprint, jsonify bp = Blueprint("frames", __name__, url_prefix="/api/1/recording") FRAMES_DIR = "/tmp/adacam/pics" @bp.route("/frames/latest", methods=["GET"]) def latest_frame(): """Get path to most recent frame file.""" # Handle case where capture hasn't started yet if not os.path.exists(FRAMES_DIR): return jsonify({'frames': [], 'message': 'capture not started'}), 200 if not os.path.isdir(FRAMES_DIR): return jsonify({"error": "Frames directory invalid"}), 500 files = glob.glob(os.path.join(FRAMES_DIR, "*")) if not files: return jsonify({'frames': [], 'message': 'no frames captured yet'}), 200 latest = max(files, key=os.path.getmtime) return jsonify({"path": latest}) @bp.route("/frames", methods=["GET"]) def list_frames(): """List all available frames.""" if not os.path.exists(FRAMES_DIR): return jsonify({'frames': [], 'message': 'capture not started'}) files = glob.glob(os.path.join(FRAMES_DIR, "*")) files.sort(key=os.path.getmtime, reverse=True) return jsonify({ 'frames': files[:100], # limit to 100 most recent 'total': len(files) })