Просмотр исходного кода

support to images more optimized

latapp 9 месяцев назад
Родитель
Сommit
0b74de32ff
2 измененных файлов с 9 добавлено и 0 удалено
  1. 2 0
      app.py
  2. 7 0
      routes/static.py

+ 2 - 0
app.py

@@ -60,6 +60,8 @@ def setup_routes(app: FastAPI):
                      response_class=HTMLResponse, include_in_schema=False)
     app.add_api_route("/register", static.serve_register_html, methods=["GET"],
                      response_class=HTMLResponse, include_in_schema=False)
+    app.add_api_route("/images/{image_path:path}", static.serve_image, methods=["GET"],
+                     response_class=HTMLResponse, include_in_schema=False)
 
     # Mount static files
     static.mount_main_static_files(app)

+ 7 - 0
routes/static.py

@@ -25,3 +25,10 @@ def mount_register_static_files(app):
 def mount_main_static_files(app):
     """Mount static files"""
     app.mount("/express/", StaticFiles(directory="public/main", html=False), name="public_root_assets")
+
+def serve_image(image_path: str):
+    """Serve images from the public/images directory"""
+    image_full_path = os.path.join("public", "images", image_path)
+    if not os.path.exists(image_full_path):
+        raise HTTPException(status_code=404, detail=f"Image '{image_path}' not found.")
+    return FileResponse(image_full_path)