33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
"""Export API: enriched GPX / KML downloads."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
from fastapi.responses import Response
|
|
|
|
from app.deps import get_store
|
|
from app.export.gpx_writer import write_gpx
|
|
from app.export.kml_writer import write_kml
|
|
|
|
router = APIRouter(prefix="/api/routes", tags=["export"])
|
|
|
|
|
|
@router.get("/{route_id}/export")
|
|
def export_route(route_id: str, format: str = Query("gpx", pattern="^(gpx|kml)$")):
|
|
store = get_store()
|
|
route = store.get_route(route_id)
|
|
if route is None:
|
|
raise HTTPException(404, "route not found")
|
|
guide = store.get_trail_guide(route.trail_guide_id) if route.trail_guide_id else None
|
|
safe_name = re.sub(r"[^A-Za-z0-9_-]+", "_", route.name).strip("_") or "route"
|
|
if format == "gpx":
|
|
body, media = write_gpx(route, guide), "application/gpx+xml"
|
|
else:
|
|
body, media = write_kml(route, guide), "application/vnd.google-earth.kml+xml"
|
|
return Response(
|
|
content=body, media_type=media,
|
|
headers={"Content-Disposition": f'attachment; filename="{safe_name}.{format}"'},
|
|
)
|