"""Public guidance examples; distinct from the evaluated sample set.""" import json from pathlib import Path from fastapi import HTTPException from fastapi.responses import FileResponse def register_tutorial_routes(app, root): root = Path(root) data_root = root / 'data' manifest_path = data_root / 'tutorials.json' def examples(): if not manifest_path.is_file(): return [] return json.loads(manifest_path.read_text()) @app.get('/api/tutorials') def tutorials(): return examples() @app.get('/api/tutorial-media/{name}') def tutorial_media(name: str): allowed = {Path(im['src']).name for row in examples() for im in row['images']} if name not in allowed or Path(name).name != name: raise HTTPException(404) base = (data_root / 'tutorials').resolve() path = (base / name).resolve() if not path.is_relative_to(base) or not path.is_file(): raise HTTPException(404) return FileResponse(path, headers={'Cache-Control': 'public, max-age=3600'})