Spaces:
Sleeping
Sleeping
| """ | |
| Export Router — Generates downloadable files from extracted field data. | |
| POST /api/export | |
| - Accepts JSON body with fields and desired format | |
| - Generates the file using the export engine | |
| - Returns the file as a streaming download | |
| """ | |
| from fastapi import APIRouter, HTTPException | |
| from fastapi.responses import Response | |
| from models.schemas import ExportRequest, ExtractedField | |
| from services.export_engine import generate_export | |
| router = APIRouter(prefix="/api", tags=["export"]) | |
| async def export_file(request: ExportRequest): | |
| """ | |
| Generate and download an export file. | |
| Body: | |
| - fields: List of { name, value, field_type, confidence } objects | |
| - format: "csv" | "xlsx" | "docx" | "pdf" | |
| - filename: Optional base filename (default: "export") | |
| """ | |
| if not request.fields: | |
| raise HTTPException(status_code=400, detail="No fields provided for export.") | |
| try: | |
| file_bytes, content_type, extension = generate_export( | |
| fields=request.fields, | |
| format=request.format.value, | |
| filename=request.filename or "export", | |
| ) | |
| safe_filename = (request.filename or "puffnparse_export") + extension | |
| return Response( | |
| content=file_bytes, | |
| media_type=content_type, | |
| headers={ | |
| "Content-Disposition": f'attachment; filename="{safe_filename}"', | |
| "Access-Control-Expose-Headers": "Content-Disposition", | |
| }, | |
| ) | |
| except ValueError as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Export error: {str(e)}") | |