From a51091624759977da824a01709c78f28ccc6d9a8 Mon Sep 17 00:00:00 2001 From: Xavier Date: Thu, 27 Aug 2026 18:09:45 +0200 Subject: [PATCH] MAJ MAIN.PY POUR IMAGE --- main.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 829617f..0ac71d6 100644 --- a/main.py +++ b/main.py @@ -23,13 +23,25 @@ import modules.model_updater dossier_media = "/DATA/AppData/MULTI-IA-AETHAS38/RAG/Media" os.makedirs(dossier_media, exist_ok=True) -# --- MIDDLEWARE DE SÉCURITÉ --- +# --- MIDDLEWARE DE SÉCURITÉ & SERVEUR MÉDIA UNIFIÉ --- class SecurityMiddleware(BaseHTTPMiddleware): async def dispatch(self, request, call_next): ip = request.headers.get("X-Forwarded-For", request.client.host).split(",")[0].strip() utilisateur = request.cookies.get("aethas_user", "visiteur") - # EXCLUSION DE /Media POUR NE PAS BLOQUER L'IMAGE + # --- LECTURE EN RAM ULTRA-DIRECTE PAR LE MIDDLEWARE --- + if request.url.path.startswith("/Media/"): + fname = request.url.path.replace("/Media/", "", 1) + chemin = os.path.join(dossier_media, fname) + if os.path.exists(chemin): + try: + with open(chemin, "rb") as f: + return Response(content=f.read(), media_type="image/png") + except Exception as e: + return HTMLResponse(f"Erreur : {e}", status_code=500) + return HTMLResponse("Fichier introuvable", status_code=404) + + # EXCLUSION DE /Media POUR NE PAS BLOQUER LE RESTE if not request.url.path.startswith(("/Logo", "/login", "/Media")): if verifier_et_bloquer_vpn(ip, utilisateur): return HTMLResponse("

Accès refusé

VPN ou Proxy strictement interdit.

", status_code=403) @@ -49,18 +61,7 @@ app, rt = fast_app( ) app.mount("/Logo", StaticFiles(directory="Logo"), name="Logo") - -# --- ROUTE MÉDIA INDESTRUCTIBLE (SYNTAXE FASHTHTML NATIVE) --- -@rt('/Media/{fname:path}') -def get(fname: str): - chemin = os.path.join(dossier_media, fname) - if os.path.exists(chemin): - try: - with open(chemin, "rb") as f: - return Response(content=f.read(), media_type="image/png") - except Exception as e: - return HTMLResponse(f"Erreur lecture RAM : {e}", status_code=500) - return HTMLResponse("Fichier introuvable", status_code=404) +app.mount("/Media", StaticFiles(directory=dossier_media), name="Media") # --- ROUTES D'AUTHENTIFICATION --- @rt('/login')