Spaces:
Runtime error
Runtime error
Greg commited on
Commit ·
ccc4fb4
1
Parent(s): a1c2d97
diag(ta-01): log resolved photo backend + admin /photos/_debug endpoint
Browse files
backend/app/api/v1/endpoints/photos.py
CHANGED
|
@@ -3,11 +3,37 @@ from pathlib import Path
|
|
| 3 |
from fastapi import APIRouter, Depends, HTTPException
|
| 4 |
from fastapi.responses import FileResponse
|
| 5 |
from app.dependencies.auth import get_current_user
|
|
|
|
| 6 |
from app.models.user import User
|
|
|
|
| 7 |
|
| 8 |
router = APIRouter(prefix="/photos", tags=["photos"])
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@router.get("/{account_id}/{user_id}/{filename}")
|
| 12 |
def serve_photo(
|
| 13 |
account_id: int,
|
|
|
|
| 3 |
from fastapi import APIRouter, Depends, HTTPException
|
| 4 |
from fastapi.responses import FileResponse
|
| 5 |
from app.dependencies.auth import get_current_user
|
| 6 |
+
from app.middleware.rbac import RBAC
|
| 7 |
from app.models.user import User
|
| 8 |
+
from app.storage import resolved_backend
|
| 9 |
|
| 10 |
router = APIRouter(prefix="/photos", tags=["photos"])
|
| 11 |
|
| 12 |
|
| 13 |
+
@router.get("/_debug")
|
| 14 |
+
def storage_debug(_: User = Depends(RBAC(["admin"]))):
|
| 15 |
+
"""Diagnose what photo storage env vars the running container actually sees.
|
| 16 |
+
Returns the resolved backend and which related env vars are populated
|
| 17 |
+
(values are masked; we only report set/unset)."""
|
| 18 |
+
def is_set(name: str) -> bool:
|
| 19 |
+
v = os.getenv(name)
|
| 20 |
+
return bool(v and v.strip())
|
| 21 |
+
|
| 22 |
+
return {
|
| 23 |
+
"resolved_backend": resolved_backend(),
|
| 24 |
+
"raw_env": {
|
| 25 |
+
"PHOTO_STORAGE_BACKEND": os.getenv("PHOTO_STORAGE_BACKEND"),
|
| 26 |
+
},
|
| 27 |
+
"vars_set": {
|
| 28 |
+
"SUPABASE_ACCESS_KEY_ID": is_set("SUPABASE_ACCESS_KEY_ID"),
|
| 29 |
+
"SUPABASE_ACCESS_KEY_SECRET": is_set("SUPABASE_ACCESS_KEY_SECRET"),
|
| 30 |
+
"SUPABASE_ENDPOINT": is_set("SUPABASE_ENDPOINT"),
|
| 31 |
+
"SUPABASE_REGION": is_set("SUPABASE_REGION"),
|
| 32 |
+
"SUPABASE_PHOTO_BUCKET": is_set("SUPABASE_PHOTO_BUCKET"),
|
| 33 |
+
},
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
@router.get("/{account_id}/{user_id}/{filename}")
|
| 38 |
def serve_photo(
|
| 39 |
account_id: int,
|
backend/app/storage/__init__.py
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
from app.storage.base import PhotoStorage
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def get_storage() -> PhotoStorage:
|
| 6 |
# Trim + default-on-empty so a blank Space Secret behaves like an unset one
|
| 7 |
# (some hosts persist the var as an empty string when you only set the name).
|
| 8 |
-
backend = (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
if backend == "local":
|
| 10 |
from app.storage.local_disk import LocalDiskStorage
|
| 11 |
return LocalDiskStorage(
|
|
|
|
| 1 |
+
import logging
|
| 2 |
import os
|
| 3 |
from app.storage.base import PhotoStorage
|
| 4 |
|
| 5 |
+
_log = logging.getLogger(__name__)
|
| 6 |
+
_logged_backend = False
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def resolved_backend() -> str:
|
| 10 |
+
"""Resolve the active PHOTO_STORAGE_BACKEND with defaults. Pure function."""
|
| 11 |
+
return (os.getenv("PHOTO_STORAGE_BACKEND") or "").strip().lower() or "local"
|
| 12 |
+
|
| 13 |
|
| 14 |
def get_storage() -> PhotoStorage:
|
| 15 |
# Trim + default-on-empty so a blank Space Secret behaves like an unset one
|
| 16 |
# (some hosts persist the var as an empty string when you only set the name).
|
| 17 |
+
backend = resolved_backend()
|
| 18 |
+
global _logged_backend
|
| 19 |
+
if not _logged_backend:
|
| 20 |
+
_log.warning("[photo_storage] resolved backend = %r", backend)
|
| 21 |
+
_logged_backend = True
|
| 22 |
if backend == "local":
|
| 23 |
from app.storage.local_disk import LocalDiskStorage
|
| 24 |
return LocalDiskStorage(
|