ADD feature to allow donwload of source file with src endpoint

This commit is contained in:
CaffeineFueled 2025-06-24 00:55:40 +02:00
parent 36eed976f2
commit ade138ae05
2 changed files with 43 additions and 9 deletions

17
main.py
View file

@ -1,5 +1,5 @@
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.responses import JSONResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
import os
import csv
@ -47,6 +47,7 @@ def register_api_endpoints():
if file_path.is_file():
endpoint_name = file_path.stem
route = f"/api/{endpoint_name}"
src_route = f"/api/{endpoint_name}/src"
# Skip if route already exists
if any(route == route_info.path for route_info in app.routes):
@ -64,8 +65,13 @@ def register_api_endpoints():
content={"error": f"Failed to load CSV: {str(e)}"}
)
# Add the route
# Create a closure for serving the original file
async def get_csv_src(request: Request, file_path=file_path):
return FileResponse(file_path, media_type='text/csv', filename=file_path.name)
# Add the routes
app.add_api_route(route, get_csv_data, methods=["GET"])
app.add_api_route(src_route, get_csv_src, methods=["GET"])
elif file_path.suffix.lower() == '.json':
# Create a closure to capture the current file_path
@ -79,8 +85,13 @@ def register_api_endpoints():
content={"error": f"Failed to load JSON: {str(e)}"}
)
# Add the route
# Create a closure for serving the original file
async def get_json_src(request: Request, file_path=file_path):
return FileResponse(file_path, media_type='application/json', filename=file_path.name)
# Add the routes
app.add_api_route(route, get_json_data, methods=["GET"])
app.add_api_route(src_route, get_json_src, methods=["GET"])
@app.middleware("http")
async def check_for_new_files(request: Request, call_next):