Fix: Staging-Media — PROD_MEDIA_DIR Fallback statt überlappende Docker-Volumes

This commit is contained in:
rene 2026-05-02 09:51:03 +02:00
parent 00109a0929
commit 5a412c425c
2 changed files with 21 additions and 3 deletions

View file

@ -279,9 +279,26 @@ app.mount("/icons", StaticFiles(directory=f"{STATIC_DIR}/icons"), name="icons")
app.mount("/img", StaticFiles(directory=f"{STATIC_DIR}/img"), name="img")
# User-generierte Medien (Fotos aus Tagebuch, Giftköder-Alarm, etc.)
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
PROD_MEDIA_DIR = os.getenv("PROD_MEDIA_DIR", "") # Staging-only: Fallback auf Prod-Media
os.makedirs(MEDIA_DIR, exist_ok=True)
app.mount("/media", StaticFiles(directory=MEDIA_DIR), name="media")
if PROD_MEDIA_DIR:
# Staging: erst eigenes media-Verzeichnis, dann Prod-Fallback
from pathlib import Path as _Path
from starlette.responses import FileResponse as _FileResponse
@app.get("/media/{path:path}")
async def serve_media(path: str):
p = _Path(MEDIA_DIR) / path
if p.is_file():
return _FileResponse(str(p))
pp = _Path(PROD_MEDIA_DIR) / path
if pp.is_file():
return _FileResponse(str(pp))
raise HTTPException(404, "Media not found")
else:
app.mount("/media", StaticFiles(directory=MEDIA_DIR), name="media")
@app.get("/robots.txt")
async def robots():