# main.py - FastAPI + Jinja2 from fastapi import FastAPI from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from pathlib import Path from datetime import datetime from routes import upload, viewer, admin, crop # -- Middleware ------------------------------------------------ from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request from starlette.responses import Response app = FastAPI(title="PointCloud Frontend") MAX_UPLOAD_BYTES = 10 * 1024 * 1024 * 1024 # 10 GB à ajuster class LimitUploadSize(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): if request.method == "POST": content_length = request.headers.get("content-length") if content_length and int(content_length) > MAX_UPLOAD_BYTES: return Response( content=f"Fichier trop volumineux. Maximum : {MAX_UPLOAD_BYTES // (1024**3)} GB", status_code=413, ) return await call_next(request) app.add_middleware(LimitUploadSize) # ── Fichiers statiques ──────────────────────────────────────────────────────── app.mount( "/static", StaticFiles(directory=Path(__file__).parent / "static"), name="static", ) # ── Jinja2 pour les pages et partials ──────────────────────────────────────── templates = Jinja2Templates(directory=Path(__file__).parent / "templates") templates.env.filters["datetimeformat"] = lambda ts: ( datetime.fromtimestamp(int(ts)).strftime("%Y-%m-%d %H:%M") if ts else "—" ) # Rend les templates accessibles aux routes via app.state app.state.templates = templates # ── Routes ──────────────────────────────────────────────────────────────────── app.include_router(upload.router) app.include_router(viewer.router) app.include_router(admin.router) app.include_router(crop.router)