"""Admin settings — blocked image upload/management.""" import os from fastapi import APIRouter, Depends, HTTPException, UploadFile, File from fastapi.responses import FileResponse from ..auth import get_current_admin router = APIRouter(prefix="/settings", tags=["settings"]) BLOCKED_IMAGE_PATH = "/opt/kirastream/backend/static/blocked_image.jpg" MAX_SIZE = 10 * 1024 * 1024 # 10 MB @router.get("/blocked-image") async def get_blocked_image(_=Depends(get_current_admin)): if not os.path.exists(BLOCKED_IMAGE_PATH): raise HTTPException(404, "No blocked image configured") return FileResponse(BLOCKED_IMAGE_PATH, media_type="image/jpeg") @router.post("/blocked-image", status_code=201) async def upload_blocked_image(file: UploadFile = File(...), _=Depends(get_current_admin)): if file.content_type not in ("image/jpeg", "image/png", "image/webp", "image/gif"): raise HTTPException(400, "Only JPEG, PNG, WebP or GIF images allowed") data = await file.read() if len(data) > MAX_SIZE: raise HTTPException(413, "Image too large (max 10 MB)") os.makedirs(os.path.dirname(BLOCKED_IMAGE_PATH), exist_ok=True) with open(BLOCKED_IMAGE_PATH, "wb") as f: f.write(data) return {"ok": True, "size": len(data)} @router.delete("/blocked-image", status_code=204) async def delete_blocked_image(_=Depends(get_current_admin)): if os.path.exists(BLOCKED_IMAGE_PATH): os.remove(BLOCKED_IMAGE_PATH)