mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
According to multiple FastAPI discussions [1], FastAPI only includes a built-in mechanism to redirect requests including a trailing slash, to its variation without slash, using a `307` status code. This can be an issue when certain clients do not send the same headers on the redirected request. This change adds a custom FastAPI `APIRouter`, that registers both route path variations (with and without trailing slash), while only marking the path without slash for being included in the OpenAPI schema. [1] https://github.com/fastapi/fastapi/discussions/7298
29 lines
860 B
Python
29 lines
860 B
Python
from config import ASSETS_BASE_PATH
|
|
from decorators.auth import protected_route
|
|
from fastapi import Request
|
|
from fastapi.responses import FileResponse
|
|
from utils.router import APIRouter
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@protected_route(router.head, "/raw/assets/{path:path}", ["assets.read"])
|
|
def head_raw_asset(request: Request, path: str):
|
|
asset_path = f"{ASSETS_BASE_PATH}/{path}"
|
|
return FileResponse(path=asset_path, filename=path.split("/")[-1])
|
|
|
|
|
|
@protected_route(router.get, "/raw/assets/{path:path}", ["assets.read"])
|
|
def get_raw_asset(request: Request, path: str):
|
|
"""Download a single asset file
|
|
|
|
Args:
|
|
request (Request): Fastapi Request object
|
|
|
|
Returns:
|
|
FileResponse: Returns a single asset file
|
|
"""
|
|
|
|
asset_path = f"{ASSETS_BASE_PATH}/{path}"
|
|
return FileResponse(path=asset_path, filename=path.split("/")[-1])
|