feat(release): publish minimal self-contained Magent source
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,85 @@
|
||||
"""Administrator-only encrypted backup downloads and staged restores."""
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
||||
from fastapi.responses import Response
|
||||
from pydantic import BaseModel, ConfigDict, Field, SecretStr
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
|
||||
from ..auth import require_admin
|
||||
from ..db import get_rate_limit_status, record_rate_limit_event
|
||||
from ..services import backups
|
||||
|
||||
def _no_store(response: Response) -> None:
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
response.headers["Pragma"] = "no-cache"
|
||||
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/admin/backups", tags=["backups"],
|
||||
dependencies=[Depends(require_admin), Depends(_no_store)],
|
||||
)
|
||||
|
||||
|
||||
class ExportRequest(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
passphrase: SecretStr = Field(min_length=12, max_length=1024)
|
||||
include_cache: bool = False
|
||||
|
||||
|
||||
def _rate_limit(user: dict) -> None:
|
||||
key = str(user["username"])
|
||||
exceeded, retry = get_rate_limit_status("backups", key, 300, 3)
|
||||
if exceeded:
|
||||
raise HTTPException(429, "Too many backup operations; try again shortly", headers={"Retry-After": str(retry)})
|
||||
record_rate_limit_event("backups", key)
|
||||
|
||||
|
||||
@router.get("")
|
||||
def status() -> dict:
|
||||
return backups.backup_status()
|
||||
|
||||
|
||||
@router.post("/export")
|
||||
def export(payload: ExportRequest, user: dict = Depends(require_admin)) -> Response:
|
||||
_rate_limit(user)
|
||||
try:
|
||||
content, filename = backups.create_backup(payload.passphrase.get_secret_value(), payload.include_cache)
|
||||
except backups.BackupError as exc:
|
||||
raise HTTPException(400, str(exc)) from exc
|
||||
return Response(content, media_type="application/octet-stream", headers={
|
||||
"Content-Disposition": f'attachment; filename="{filename}"',
|
||||
"Cache-Control": "no-store", "Pragma": "no-cache",
|
||||
})
|
||||
|
||||
|
||||
@router.post("/restore", status_code=202)
|
||||
async def restore(
|
||||
file: UploadFile = File(...),
|
||||
passphrase: str = Form(..., min_length=12, max_length=1024),
|
||||
confirmation: Literal["RESTORE"] = Form(...),
|
||||
user: dict = Depends(require_admin),
|
||||
) -> dict:
|
||||
_rate_limit(user)
|
||||
try:
|
||||
if file.size is not None and file.size > backups.MAX_UPLOAD_BYTES:
|
||||
raise HTTPException(413, "Backup exceeds the 32 MiB upload limit")
|
||||
metadata = await run_in_threadpool(backups.stage_restore, file.file, passphrase)
|
||||
except backups.BackupError as exc:
|
||||
raise HTTPException(400, str(exc)) from exc
|
||||
finally:
|
||||
await file.close()
|
||||
return {
|
||||
"status": "staged", "restart_required": True, "backup": metadata,
|
||||
"message": "Backup validated. Restart Magent to apply it. Current data remains active until restart.",
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/restore")
|
||||
def cancel() -> dict:
|
||||
try:
|
||||
backups.cancel_restore()
|
||||
except backups.BackupError as exc:
|
||||
raise HTTPException(409, str(exc)) from exc
|
||||
return {"status": "cancelled"}
|
||||
@@ -0,0 +1,152 @@
|
||||
import os
|
||||
import warnings
|
||||
from io import BytesIO
|
||||
from typing import Any, Dict
|
||||
|
||||
from fastapi import APIRouter, HTTPException, UploadFile
|
||||
from fastapi.responses import FileResponse
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
router = APIRouter(prefix="/branding", tags=["branding"])
|
||||
|
||||
_BRANDING_DIR = os.path.join(os.getcwd(), "data", "branding")
|
||||
_LOGO_PATH = os.path.join(_BRANDING_DIR, "logo.png")
|
||||
_FAVICON_PATH = os.path.join(_BRANDING_DIR, "favicon.ico")
|
||||
_BUNDLED_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "assets", "branding"))
|
||||
_BUNDLED_LOGO_PATH = os.path.join(_BUNDLED_DIR, "logo.png")
|
||||
_BUNDLED_FAVICON_PATH = os.path.join(_BUNDLED_DIR, "favicon.ico")
|
||||
_BRANDING_SOURCE = os.getenv("BRANDING_SOURCE", "bundled").lower()
|
||||
_MAX_UPLOAD_BYTES = 5 * 1024 * 1024
|
||||
_MAX_IMAGE_PIXELS = 25_000_000
|
||||
_ALLOWED_IMAGE_TYPES = {"image/png", "image/jpeg", "image/webp"}
|
||||
_ALLOWED_IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp"}
|
||||
|
||||
|
||||
def _ensure_branding_dir() -> None:
|
||||
os.makedirs(_BRANDING_DIR, exist_ok=True)
|
||||
|
||||
|
||||
def _resize_image(image: Image.Image, max_size: int = 300) -> Image.Image:
|
||||
image = image.convert("RGBA")
|
||||
image.thumbnail((max_size, max_size))
|
||||
return image
|
||||
|
||||
|
||||
def _load_font(size: int) -> ImageFont.ImageFont:
|
||||
candidates = [
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
||||
]
|
||||
for path in candidates:
|
||||
if os.path.exists(path):
|
||||
try:
|
||||
return ImageFont.truetype(path, size)
|
||||
except OSError:
|
||||
continue
|
||||
return ImageFont.load_default()
|
||||
|
||||
|
||||
def _ensure_default_branding() -> None:
|
||||
if os.path.exists(_LOGO_PATH) and os.path.exists(_FAVICON_PATH):
|
||||
return
|
||||
_ensure_branding_dir()
|
||||
if not os.path.exists(_LOGO_PATH) and os.path.exists(_BUNDLED_LOGO_PATH):
|
||||
try:
|
||||
with open(_BUNDLED_LOGO_PATH, "rb") as source, open(_LOGO_PATH, "wb") as target:
|
||||
target.write(source.read())
|
||||
except OSError:
|
||||
pass
|
||||
if not os.path.exists(_FAVICON_PATH) and os.path.exists(_BUNDLED_FAVICON_PATH):
|
||||
try:
|
||||
with open(_BUNDLED_FAVICON_PATH, "rb") as source, open(_FAVICON_PATH, "wb") as target:
|
||||
target.write(source.read())
|
||||
except OSError:
|
||||
pass
|
||||
if not os.path.exists(_LOGO_PATH):
|
||||
image = Image.new("RGBA", (300, 300), (12, 18, 28, 255))
|
||||
draw = ImageDraw.Draw(image)
|
||||
font = _load_font(160)
|
||||
text = "M"
|
||||
box = draw.textbbox((0, 0), text, font=font)
|
||||
text_w = box[2] - box[0]
|
||||
text_h = box[3] - box[1]
|
||||
draw.text(
|
||||
((300 - text_w) / 2, (300 - text_h) / 2 - 6),
|
||||
text,
|
||||
font=font,
|
||||
fill=(255, 255, 255, 255),
|
||||
)
|
||||
image.save(_LOGO_PATH, format="PNG")
|
||||
if not os.path.exists(_FAVICON_PATH):
|
||||
favicon = Image.open(_LOGO_PATH).copy()
|
||||
favicon.thumbnail((64, 64))
|
||||
try:
|
||||
favicon.save(_FAVICON_PATH, format="ICO", sizes=[(32, 32), (64, 64)])
|
||||
except OSError:
|
||||
favicon.save(_FAVICON_PATH, format="ICO")
|
||||
|
||||
|
||||
def _resolve_branding_paths() -> tuple[str, str]:
|
||||
if _BRANDING_SOURCE == "data":
|
||||
_ensure_default_branding()
|
||||
return _LOGO_PATH, _FAVICON_PATH
|
||||
if os.path.exists(_BUNDLED_LOGO_PATH) and os.path.exists(_BUNDLED_FAVICON_PATH):
|
||||
return _BUNDLED_LOGO_PATH, _BUNDLED_FAVICON_PATH
|
||||
_ensure_default_branding()
|
||||
return _LOGO_PATH, _FAVICON_PATH
|
||||
|
||||
|
||||
@router.get("/logo.png")
|
||||
async def branding_logo() -> FileResponse:
|
||||
logo_path, _ = _resolve_branding_paths()
|
||||
if not os.path.exists(logo_path):
|
||||
raise HTTPException(status_code=404, detail="Logo not found")
|
||||
headers = {"Cache-Control": "no-store"}
|
||||
return FileResponse(logo_path, media_type="image/png", headers=headers)
|
||||
|
||||
|
||||
@router.get("/favicon.ico")
|
||||
async def branding_favicon() -> FileResponse:
|
||||
_, favicon_path = _resolve_branding_paths()
|
||||
if not os.path.exists(favicon_path):
|
||||
raise HTTPException(status_code=404, detail="Favicon not found")
|
||||
headers = {"Cache-Control": "no-store"}
|
||||
return FileResponse(favicon_path, media_type="image/x-icon", headers=headers)
|
||||
|
||||
|
||||
async def save_branding_image(file: UploadFile) -> Dict[str, Any]:
|
||||
content_type = str(file.content_type or "").lower()
|
||||
extension = os.path.splitext(str(file.filename or ""))[1].lower()
|
||||
if content_type not in _ALLOWED_IMAGE_TYPES or extension not in _ALLOWED_IMAGE_EXTENSIONS:
|
||||
raise HTTPException(status_code=400, detail="Upload a PNG, JPEG, or WebP image.")
|
||||
content = await file.read(_MAX_UPLOAD_BYTES + 1)
|
||||
if not content:
|
||||
raise HTTPException(status_code=400, detail="Uploaded file is empty.")
|
||||
if len(content) > _MAX_UPLOAD_BYTES:
|
||||
raise HTTPException(status_code=413, detail="Image is too large (maximum 5 MB).")
|
||||
try:
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("error", Image.DecompressionBombWarning)
|
||||
candidate = Image.open(BytesIO(content))
|
||||
if candidate.format not in {"PNG", "JPEG", "WEBP"}:
|
||||
raise ValueError("Unsupported image format")
|
||||
if candidate.width * candidate.height > _MAX_IMAGE_PIXELS:
|
||||
raise Image.DecompressionBombError("Image pixel limit exceeded")
|
||||
candidate.verify()
|
||||
image = Image.open(BytesIO(content))
|
||||
image.load()
|
||||
except (OSError, ValueError, Image.DecompressionBombError, Image.DecompressionBombWarning) as exc:
|
||||
raise HTTPException(status_code=400, detail="Image file could not be read.") from exc
|
||||
|
||||
_ensure_branding_dir()
|
||||
image = _resize_image(image, 300)
|
||||
image.save(_LOGO_PATH, format="PNG")
|
||||
|
||||
favicon = image.copy()
|
||||
favicon.thumbnail((64, 64))
|
||||
try:
|
||||
favicon.save(_FAVICON_PATH, format="ICO", sizes=[(32, 32), (64, 64)])
|
||||
except OSError:
|
||||
favicon.save(_FAVICON_PATH, format="ICO")
|
||||
|
||||
return {"status": "ok", "width": image.width, "height": image.height}
|
||||
@@ -0,0 +1,245 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from ..feature_guards import require_request_stream, check
|
||||
from ..feature_access import permissions
|
||||
from ..db import get_user_by_username
|
||||
from . import requests as requests_router
|
||||
|
||||
router = APIRouter(prefix="/events", tags=["events"])
|
||||
|
||||
|
||||
def _sse_json(payload: Dict[str, Any]) -> str:
|
||||
return f"data: {json.dumps(payload, ensure_ascii=True, separators=(',', ':'), default=str)}\n\n"
|
||||
|
||||
|
||||
def _jsonable(value: Any) -> Any:
|
||||
if hasattr(value, "model_dump"):
|
||||
try:
|
||||
return value.model_dump(mode="json")
|
||||
except TypeError:
|
||||
return value.model_dump()
|
||||
if hasattr(value, "dict"):
|
||||
try:
|
||||
return value.dict()
|
||||
except TypeError:
|
||||
return value
|
||||
return value
|
||||
|
||||
|
||||
def _request_history_brief(entries: Any) -> list[dict[str, Any]]:
|
||||
if not isinstance(entries, list):
|
||||
return []
|
||||
items: list[dict[str, Any]] = []
|
||||
for entry in entries:
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
items.append(
|
||||
{
|
||||
"request_id": entry.get("request_id"),
|
||||
"state": entry.get("state"),
|
||||
"state_reason": entry.get("state_reason"),
|
||||
"created_at": entry.get("created_at"),
|
||||
}
|
||||
)
|
||||
return items
|
||||
|
||||
|
||||
def _request_actions_brief(entries: Any) -> list[dict[str, Any]]:
|
||||
if not isinstance(entries, list):
|
||||
return []
|
||||
items: list[dict[str, Any]] = []
|
||||
for entry in entries:
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
items.append(
|
||||
{
|
||||
"request_id": entry.get("request_id"),
|
||||
"action_id": entry.get("action_id"),
|
||||
"label": entry.get("label"),
|
||||
"status": entry.get("status"),
|
||||
"message": entry.get("message"),
|
||||
"created_at": entry.get("created_at"),
|
||||
}
|
||||
)
|
||||
return items
|
||||
|
||||
|
||||
@router.get("/stream")
|
||||
async def events_stream(
|
||||
request: Request,
|
||||
recent_days: int = 90,
|
||||
recent_stage: str = "all",
|
||||
user: Dict[str, Any] = Depends(require_request_stream),
|
||||
) -> StreamingResponse:
|
||||
recent_days = max(0, min(int(recent_days or 90), 3650))
|
||||
recent_take = 50 if user.get("role") == "admin" else 6
|
||||
|
||||
async def event_generator():
|
||||
yield "retry: 2000\n\n"
|
||||
last_recent_signature: Optional[str] = None
|
||||
next_recent_at = 0.0
|
||||
heartbeat_counter = 0
|
||||
|
||||
while True:
|
||||
if await request.is_disconnected():
|
||||
break
|
||||
|
||||
try:
|
||||
account = get_user_by_username(user.get("username", ""))
|
||||
if not account or account.get("is_blocked") or account.get("is_expired"):
|
||||
break
|
||||
check({**account, "features": permissions(account)}, "requests")
|
||||
except HTTPException:
|
||||
break
|
||||
now = time.monotonic()
|
||||
sent_any = False
|
||||
|
||||
if now >= next_recent_at:
|
||||
next_recent_at = now + 15.0
|
||||
try:
|
||||
recent_payload = await requests_router.recent_requests(
|
||||
take=recent_take,
|
||||
skip=0,
|
||||
days=recent_days,
|
||||
stage=recent_stage,
|
||||
user=user,
|
||||
)
|
||||
results = recent_payload.get("results") if isinstance(recent_payload, dict) else []
|
||||
payload = {
|
||||
"type": "home_recent",
|
||||
"ts": datetime.now(timezone.utc).isoformat(),
|
||||
"days": recent_days,
|
||||
"stage": recent_stage,
|
||||
"results": results if isinstance(results, list) else [],
|
||||
}
|
||||
except Exception as exc:
|
||||
payload = {
|
||||
"type": "home_recent",
|
||||
"ts": datetime.now(timezone.utc).isoformat(),
|
||||
"days": recent_days,
|
||||
"stage": recent_stage,
|
||||
"error": str(exc),
|
||||
}
|
||||
signature = json.dumps(payload, ensure_ascii=True, separators=(",", ":"), default=str)
|
||||
if signature != last_recent_signature:
|
||||
last_recent_signature = signature
|
||||
yield _sse_json(payload)
|
||||
sent_any = True
|
||||
|
||||
if sent_any:
|
||||
heartbeat_counter = 0
|
||||
else:
|
||||
heartbeat_counter += 1
|
||||
if heartbeat_counter >= 15:
|
||||
yield ": ping\n\n"
|
||||
heartbeat_counter = 0
|
||||
|
||||
await asyncio.sleep(1.0)
|
||||
|
||||
headers = {
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
}
|
||||
return StreamingResponse(event_generator(), media_type="text/event-stream", headers=headers)
|
||||
|
||||
|
||||
@router.get("/requests/{request_id}/stream")
|
||||
async def request_events_stream(
|
||||
request_id: str,
|
||||
request: Request,
|
||||
user: Dict[str, Any] = Depends(require_request_stream),
|
||||
) -> StreamingResponse:
|
||||
request_id = str(request_id).strip()
|
||||
if not request_id:
|
||||
raise HTTPException(status_code=400, detail="Missing request id")
|
||||
|
||||
async def event_generator():
|
||||
yield "retry: 2000\n\n"
|
||||
last_signature: Optional[str] = None
|
||||
next_refresh_at = 0.0
|
||||
heartbeat_counter = 0
|
||||
|
||||
while True:
|
||||
if await request.is_disconnected():
|
||||
break
|
||||
|
||||
try:
|
||||
account = get_user_by_username(user.get("username", ""))
|
||||
if not account or account.get("is_blocked") or account.get("is_expired"):
|
||||
break
|
||||
check({**account, "features": permissions(account)}, "requests")
|
||||
except HTTPException:
|
||||
break
|
||||
now = time.monotonic()
|
||||
sent_any = False
|
||||
|
||||
if now >= next_refresh_at:
|
||||
next_refresh_at = now + 2.0
|
||||
try:
|
||||
snapshot = await requests_router.get_snapshot(request_id=request_id, user=user)
|
||||
history_payload = await requests_router.request_history(
|
||||
request_id=request_id, limit=5, user=user
|
||||
)
|
||||
actions_payload = await requests_router.request_actions(
|
||||
request_id=request_id, limit=5, user=user
|
||||
)
|
||||
payload = {
|
||||
"type": "request_live",
|
||||
"request_id": request_id,
|
||||
"ts": datetime.now(timezone.utc).isoformat(),
|
||||
"snapshot": _jsonable(snapshot),
|
||||
"history": _request_history_brief(
|
||||
history_payload.get("snapshots", []) if isinstance(history_payload, dict) else []
|
||||
),
|
||||
"actions": _request_actions_brief(
|
||||
actions_payload.get("actions", []) if isinstance(actions_payload, dict) else []
|
||||
),
|
||||
}
|
||||
except HTTPException as exc:
|
||||
payload = {
|
||||
"type": "request_live",
|
||||
"request_id": request_id,
|
||||
"ts": datetime.now(timezone.utc).isoformat(),
|
||||
"error": str(exc.detail),
|
||||
"status_code": int(exc.status_code),
|
||||
}
|
||||
except Exception as exc:
|
||||
payload = {
|
||||
"type": "request_live",
|
||||
"request_id": request_id,
|
||||
"ts": datetime.now(timezone.utc).isoformat(),
|
||||
"error": str(exc),
|
||||
}
|
||||
|
||||
signature = json.dumps(payload, ensure_ascii=True, separators=(",", ":"), default=str)
|
||||
if signature != last_signature:
|
||||
last_signature = signature
|
||||
yield _sse_json(payload)
|
||||
sent_any = True
|
||||
|
||||
if sent_any:
|
||||
heartbeat_counter = 0
|
||||
else:
|
||||
heartbeat_counter += 1
|
||||
if heartbeat_counter >= 15:
|
||||
yield ": ping\n\n"
|
||||
heartbeat_counter = 0
|
||||
|
||||
await asyncio.sleep(1.0)
|
||||
|
||||
headers = {
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
}
|
||||
return StreamingResponse(event_generator(), media_type="text/event-stream", headers=headers)
|
||||
@@ -0,0 +1,46 @@
|
||||
from typing import Any, Dict
|
||||
import httpx
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from ..auth import get_current_user
|
||||
from ..network_security import validate_notification_target_url
|
||||
from ..runtime import get_runtime_settings
|
||||
|
||||
router = APIRouter(prefix="/feedback", tags=["feedback"], dependencies=[Depends(get_current_user)])
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def send_feedback(payload: Dict[str, Any], user: Dict[str, str] = Depends(get_current_user)) -> dict:
|
||||
runtime = get_runtime_settings()
|
||||
webhook_url = (
|
||||
getattr(runtime, "magent_notify_discord_webhook_url", None)
|
||||
or runtime.discord_webhook_url
|
||||
)
|
||||
if not webhook_url:
|
||||
raise HTTPException(status_code=400, detail="Discord webhook not configured")
|
||||
try:
|
||||
webhook_url = validate_notification_target_url(webhook_url)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
feedback_type = str(payload.get("type") or "").strip().lower()
|
||||
if feedback_type not in {"bug", "feature"}:
|
||||
raise HTTPException(status_code=400, detail="Invalid feedback type")
|
||||
|
||||
message = str(payload.get("message") or "").strip()
|
||||
if not message:
|
||||
raise HTTPException(status_code=400, detail="Message is required")
|
||||
if len(message) > 2000:
|
||||
raise HTTPException(status_code=400, detail="Message is too long")
|
||||
|
||||
username = user.get("username") or "unknown"
|
||||
content = f"**{feedback_type.title()}** from **{username}**\n{message}"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
response = await client.post(webhook_url, json={"content": content})
|
||||
response.raise_for_status()
|
||||
except httpx.HTTPStatusError as exc:
|
||||
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||
|
||||
return {"status": "ok"}
|
||||
@@ -0,0 +1,99 @@
|
||||
from fastapi import APIRouter, Depends, Response
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from ..auth import require_admin
|
||||
from ..services.identity_review import confirm_identities, review_identities, resolve_identity, repair_identity
|
||||
from ..services.duplicate_accounts import repair_duplicates
|
||||
|
||||
router = APIRouter(prefix="/admin/identities", tags=["admin"], dependencies=[Depends(require_admin)])
|
||||
|
||||
|
||||
class Confirmation(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
revision: str = Field(pattern=r"^[a-f0-9]{64}$")
|
||||
user_ids: list[int] = Field(min_length=1, max_length=3000)
|
||||
|
||||
@field_validator("user_ids")
|
||||
@classmethod
|
||||
def unique_positive_ids(cls, value):
|
||||
if any(user_id <= 0 for user_id in value) or len(set(value)) != len(value):
|
||||
raise ValueError("Choose unique positive user IDs")
|
||||
return value
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def review(response: Response):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
report, _, _ = await review_identities()
|
||||
return report
|
||||
|
||||
|
||||
@router.post("/confirm")
|
||||
async def confirm(payload: Confirmation, response: Response, admin: dict = Depends(require_admin)):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return await confirm_identities(payload.revision, payload.user_ids, admin)
|
||||
|
||||
|
||||
class Resolution(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
user_id: int = Field(gt=0, strict=True)
|
||||
jellyfin_user_id: str = Field(pattern=r"^[a-f0-9]{32}$")
|
||||
|
||||
|
||||
class ResolutionConfirmation(Resolution):
|
||||
revision: str = Field(pattern=r"^[a-f0-9]{64}$")
|
||||
|
||||
|
||||
@router.post("/resolve/check")
|
||||
async def check_resolution(payload: Resolution, response: Response):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return await resolve_identity(payload.user_id, payload.jellyfin_user_id)
|
||||
|
||||
|
||||
@router.post("/resolve/confirm")
|
||||
async def confirm_resolution(payload: ResolutionConfirmation, response: Response, admin: dict = Depends(require_admin)):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return await resolve_identity(payload.user_id, payload.jellyfin_user_id, payload.revision, admin)
|
||||
|
||||
|
||||
class RepairResolution(Resolution):
|
||||
create_seerr: bool = Field(default=False, strict=True)
|
||||
|
||||
|
||||
class RepairConfirmation(RepairResolution):
|
||||
revision: str = Field(pattern=r'^[a-f0-9]{64}$')
|
||||
|
||||
|
||||
@router.post('/repair/check')
|
||||
async def check_repair(payload: RepairResolution, response: Response):
|
||||
response.headers['Cache-Control'] = 'no-store'
|
||||
return await repair_identity(payload.user_id, payload.jellyfin_user_id, create_seerr=payload.create_seerr)
|
||||
|
||||
|
||||
@router.post('/repair/confirm')
|
||||
async def confirm_repair(payload: RepairConfirmation, response: Response, admin: dict = Depends(require_admin)):
|
||||
response.headers['Cache-Control'] = 'no-store'
|
||||
return await repair_identity(payload.user_id, payload.jellyfin_user_id, payload.revision, admin, payload.create_seerr)
|
||||
|
||||
|
||||
class DuplicateCheck(BaseModel):
|
||||
model_config = ConfigDict(extra='forbid')
|
||||
user_id: int = Field(gt=0, strict=True)
|
||||
keep_id: int | None = Field(default=None, gt=0, strict=True)
|
||||
|
||||
|
||||
class DuplicateConfirmation(DuplicateCheck):
|
||||
keep_id: int = Field(gt=0, strict=True)
|
||||
revision: str = Field(pattern=r'^[a-f0-9]{64}$')
|
||||
|
||||
|
||||
@router.post('/duplicates/check')
|
||||
async def check_duplicates(payload: DuplicateCheck, response: Response):
|
||||
response.headers['Cache-Control'] = 'no-store'
|
||||
return await repair_duplicates(payload.user_id, payload.keep_id)
|
||||
|
||||
|
||||
@router.post('/duplicates/confirm')
|
||||
async def confirm_duplicates(payload: DuplicateConfirmation, response: Response, admin: dict = Depends(require_admin)):
|
||||
response.headers['Cache-Control'] = 'no-store'
|
||||
return await repair_duplicates(payload.user_id, payload.keep_id, payload.revision, admin)
|
||||
@@ -0,0 +1,100 @@
|
||||
import os
|
||||
import re
|
||||
import mimetypes
|
||||
import logging
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import FileResponse, RedirectResponse
|
||||
import httpx
|
||||
|
||||
from ..runtime import get_runtime_settings
|
||||
|
||||
router = APIRouter(prefix="/images", tags=["images"])
|
||||
|
||||
_TMDB_BASE = "https://image.tmdb.org/t/p"
|
||||
_ALLOWED_SIZES = {"w92", "w154", "w185", "w342", "w500", "w780", "original"}
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _safe_filename(path: str) -> str:
|
||||
trimmed = path.strip("/")
|
||||
trimmed = trimmed.replace("/", "_")
|
||||
safe = re.sub(r"[^A-Za-z0-9_.-]", "_", trimmed)
|
||||
return safe or "image"
|
||||
|
||||
def tmdb_cache_path(path: str, size: str) -> Optional[str]:
|
||||
if not path or "://" in path or ".." in path:
|
||||
return None
|
||||
if not path.startswith("/"):
|
||||
path = f"/{path}"
|
||||
if size not in _ALLOWED_SIZES:
|
||||
return None
|
||||
cache_dir = os.path.join(os.getcwd(), "data", "artwork", "tmdb", size)
|
||||
return os.path.join(cache_dir, _safe_filename(path))
|
||||
|
||||
|
||||
def is_tmdb_cached(path: str, size: str) -> bool:
|
||||
file_path = tmdb_cache_path(path, size)
|
||||
return bool(file_path and os.path.exists(file_path))
|
||||
|
||||
|
||||
async def cache_tmdb_image(path: str, size: str = "w342") -> bool:
|
||||
if not path or "://" in path or ".." in path:
|
||||
return False
|
||||
|
||||
runtime = get_runtime_settings()
|
||||
cache_mode = (runtime.artwork_cache_mode or "remote").lower()
|
||||
if cache_mode != "cache":
|
||||
return False
|
||||
|
||||
file_path = tmdb_cache_path(path, size)
|
||||
if not file_path:
|
||||
return False
|
||||
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
||||
if os.path.exists(file_path):
|
||||
return True
|
||||
|
||||
url = f"{_TMDB_BASE}/{size}{path}"
|
||||
async with httpx.AsyncClient(timeout=20) as client:
|
||||
response = await client.get(url)
|
||||
response.raise_for_status()
|
||||
content = response.content
|
||||
with open(file_path, "wb") as handle:
|
||||
handle.write(content)
|
||||
return True
|
||||
|
||||
|
||||
@router.get("/tmdb")
|
||||
async def tmdb_image(path: str, size: str = "w342"):
|
||||
if not path or "://" in path or ".." in path:
|
||||
raise HTTPException(status_code=400, detail="Invalid image path")
|
||||
if not path.startswith("/"):
|
||||
path = f"/{path}"
|
||||
if size not in _ALLOWED_SIZES:
|
||||
raise HTTPException(status_code=400, detail="Invalid size")
|
||||
|
||||
runtime = get_runtime_settings()
|
||||
cache_mode = (runtime.artwork_cache_mode or "remote").lower()
|
||||
url = f"{_TMDB_BASE}/{size}{path}"
|
||||
if cache_mode != "cache":
|
||||
return RedirectResponse(url=url)
|
||||
|
||||
file_path = tmdb_cache_path(path, size)
|
||||
if not file_path:
|
||||
raise HTTPException(status_code=400, detail="Invalid image path")
|
||||
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
||||
headers = {"Cache-Control": "public, max-age=86400"}
|
||||
if os.path.exists(file_path):
|
||||
media_type = mimetypes.guess_type(file_path)[0] or "image/jpeg"
|
||||
return FileResponse(file_path, media_type=media_type, headers=headers)
|
||||
|
||||
try:
|
||||
await cache_tmdb_image(path, size)
|
||||
if os.path.exists(file_path):
|
||||
media_type = mimetypes.guess_type(file_path)[0] or "image/jpeg"
|
||||
return FileResponse(file_path, media_type=media_type, headers=headers)
|
||||
logger.warning("TMDB cache miss after fetch: path=%s size=%s", path, size)
|
||||
except (httpx.HTTPError, OSError) as exc:
|
||||
logger.warning("TMDB cache failed: path=%s size=%s error=%s", path, size, exc)
|
||||
|
||||
return RedirectResponse(url=url)
|
||||
@@ -0,0 +1,78 @@
|
||||
from ..feature_guards import require_stats
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from ..auth import get_current_user
|
||||
from ..clients.jellystat import HistoryLimitError, JellystatError
|
||||
from ..services.insights import get_insights
|
||||
from ..services.insights_artwork import get_artwork
|
||||
from ..services.monthly_reports import get_monthly_report, report_csv
|
||||
from ..runtime import get_runtime_settings
|
||||
|
||||
router = APIRouter(prefix="/insights", tags=["insights"], dependencies=[Depends(require_stats)])
|
||||
|
||||
|
||||
class MonthlyReportQuery(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
month: str | None = Field(default=None, max_length=7, pattern=r"^[0-9]{4}-[0-9]{2}$")
|
||||
|
||||
|
||||
async def monthly_data(user: dict, month: str | None) -> dict:
|
||||
try:
|
||||
return await get_monthly_report(user, month)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(422, "Choose the current month or one of the previous 23 months.") from exc
|
||||
except HistoryLimitError as exc:
|
||||
raise HTTPException(422, "This report exceeds Jellystat's history limit. No partial report has been generated.") from exc
|
||||
except JellystatError as exc:
|
||||
raise HTTPException(502, "Your monthly report is temporarily unavailable. Please try again shortly.") from exc
|
||||
|
||||
|
||||
@router.get("/reports/monthly")
|
||||
async def monthly_report(query: Annotated[MonthlyReportQuery, Query()], response: Response,
|
||||
user: dict = Depends(get_current_user)) -> dict:
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return await monthly_data(user, query.month)
|
||||
|
||||
|
||||
@router.get("/reports/monthly.csv")
|
||||
async def monthly_export(query: Annotated[MonthlyReportQuery, Query()], user: dict = Depends(get_current_user)):
|
||||
report = await monthly_data(user, query.month)
|
||||
if report["state"] != "ready":
|
||||
raise HTTPException(409, "Connect Jellystat and link your viewing account before downloading a report.")
|
||||
return Response(report_csv(report), media_type="text/csv; charset=utf-8", headers={
|
||||
"Cache-Control": "no-store", "X-Content-Type-Options": "nosniff",
|
||||
"Content-Disposition": f'attachment; filename="magent-monthly-report-{report["month"]}.csv"'})
|
||||
|
||||
|
||||
@router.get("/artwork/{item_id}")
|
||||
async def artwork(item_id: str, token: Annotated[str, Query(max_length=100)], user: dict = Depends(get_current_user)):
|
||||
content, media_type = await get_artwork(user, get_runtime_settings(), item_id, token)
|
||||
return Response(content=content, media_type=media_type,
|
||||
headers={"Cache-Control": "private, max-age=600", "Vary": "Cookie, Authorization", "X-Content-Type-Options": "nosniff"})
|
||||
|
||||
|
||||
class InsightsQuery(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
days: int = 30
|
||||
|
||||
@field_validator("days")
|
||||
@classmethod
|
||||
def supported_period(cls, value: int) -> int:
|
||||
if value not in {7, 30, 90, 365}:
|
||||
raise ValueError("Choose 7, 30, 90 or 365 days")
|
||||
return value
|
||||
|
||||
|
||||
@router.get("")
|
||||
async def dashboard(query: Annotated[InsightsQuery, Query()], response: Response,
|
||||
user: dict = Depends(get_current_user)) -> dict:
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
return await get_insights(user, query.days)
|
||||
except HistoryLimitError as exc:
|
||||
raise HTTPException(status_code=422, detail="There is too much history for this period. Choose a shorter period.") from exc
|
||||
except JellystatError as exc:
|
||||
raise HTTPException(status_code=502, detail="Your viewing stats are temporarily unavailable. Please try again shortly.") from exc
|
||||
@@ -0,0 +1,193 @@
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from typing import Literal
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
from pydantic import Field, field_validator
|
||||
|
||||
from ..services.public_urls import magent_public_url
|
||||
from ..auth import get_current_user, require_admin
|
||||
from ..runtime import get_runtime_settings
|
||||
from ..services import newsletters as service, newsletter_store as store, newsletter_catalog as catalog
|
||||
from .recaps import StrictPayload, Preference, RecapSettings, TokenAction, no_cache
|
||||
|
||||
router = APIRouter(tags=['newsletters'], dependencies=[Depends(no_cache)])
|
||||
|
||||
|
||||
class Settings(StrictPayload):
|
||||
enabled: bool
|
||||
weekday: int = Field(ge=0, le=6)
|
||||
hour: int = Field(ge=0, le=23)
|
||||
limit_titles: int = Field(ge=1, le=24)
|
||||
public_url: str = Field(default="", max_length=500)
|
||||
intro: str = Field(default='', max_length=2000)
|
||||
revision: int = Field(ge=1)
|
||||
_url = field_validator('public_url')(RecapSettings.origin_only.__func__)
|
||||
|
||||
|
||||
class NewDraft(StrictPayload):
|
||||
days: Literal[7, 14, 30] = 7
|
||||
|
||||
|
||||
class Selection(StrictPayload):
|
||||
id: str = Field(pattern=r'^[a-f0-9]{32}$')
|
||||
selected: bool
|
||||
featured: bool
|
||||
|
||||
|
||||
class Version(StrictPayload):
|
||||
revision: int = Field(ge=1)
|
||||
|
||||
|
||||
class EditionUpdate(Version):
|
||||
subject: str = Field(min_length=1, max_length=150)
|
||||
intro: str = Field(default='', max_length=2000)
|
||||
titles: list[Selection] = Field(max_length=60)
|
||||
|
||||
@field_validator('subject')
|
||||
@classmethod
|
||||
def subject_line(cls, value):
|
||||
value = value.strip()
|
||||
if not value or any(ord(char) < 32 or ord(char) == 127 for char in value):
|
||||
raise ValueError('Use a single, non-empty subject line.')
|
||||
return value
|
||||
|
||||
|
||||
class Test(Version):
|
||||
request_id: UUID
|
||||
|
||||
|
||||
class Publish(Version):
|
||||
send_at: datetime | None = None
|
||||
|
||||
|
||||
def fail(exc):
|
||||
if isinstance(exc, service.NewsletterError):
|
||||
raise HTTPException(exc.status, exc.detail) from exc
|
||||
if isinstance(exc, store.Conflict):
|
||||
raise HTTPException(429 if 'five minutes' in str(exc) else 409, str(exc)) from exc
|
||||
raise HTTPException(502, str(exc) if isinstance(exc, catalog.CatalogError) else 'Jellyfin took too long to prepare this edition. Please try again.') from exc
|
||||
|
||||
|
||||
@router.get('/profile/newsletters')
|
||||
def preference(user: dict = Depends(get_current_user)):
|
||||
try:
|
||||
return service.preferences(user)
|
||||
except service.NewsletterError as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.put('/profile/newsletters')
|
||||
async def set_preference(payload: Preference, user: dict = Depends(get_current_user)):
|
||||
try:
|
||||
if payload.enabled:
|
||||
return await service.subscribe(user)
|
||||
store.disable(service.account_for(user)['id'])
|
||||
return service.preferences(user)
|
||||
except service.NewsletterError as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.post('/newsletter-subscription/check')
|
||||
def check_token(payload: TokenAction):
|
||||
try:
|
||||
return service.token_action(payload.token, payload.action)
|
||||
except service.NewsletterError as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.post('/newsletter-subscription/confirm')
|
||||
def confirm_token(payload: TokenAction):
|
||||
try:
|
||||
return service.token_action(payload.token, payload.action, apply=True)
|
||||
except service.NewsletterError as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.get('/admin/newsletters')
|
||||
def overview(offset: int = Query(default=0, ge=0, le=1_000_000), user: dict = Depends(require_admin)):
|
||||
ready, detail = service.delivery_ready()
|
||||
return {'settings': store.public_settings(), 'ready': ready, 'detail': detail,
|
||||
'playback_url': service.playback_url(get_runtime_settings()), **store.overview(offset)}
|
||||
|
||||
|
||||
@router.put('/admin/newsletters')
|
||||
def settings(payload: Settings, user: dict = Depends(require_admin)):
|
||||
try:
|
||||
public_url = magent_public_url(payload.public_url or store.settings()['public_url'])
|
||||
ready, detail = service.delivery_ready(public_url)
|
||||
if payload.enabled and not ready:
|
||||
raise service.NewsletterError(detail)
|
||||
return store.save_settings({**payload.model_dump(), "public_url": public_url}, datetime.now(timezone.utc))
|
||||
except (service.NewsletterError, store.Conflict) as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.post('/admin/newsletters/drafts', status_code=201)
|
||||
async def create_draft(payload: NewDraft, user: dict = Depends(require_admin)):
|
||||
try:
|
||||
return await service.create_draft(user, payload.days)
|
||||
except (service.NewsletterError, catalog.CatalogError, TimeoutError) as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.get('/admin/newsletters/editions/{identity}')
|
||||
def edition(identity: UUID, user: dict = Depends(require_admin)):
|
||||
try:
|
||||
return service.require_edition(identity.hex)
|
||||
except service.NewsletterError as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.put('/admin/newsletters/editions/{identity}')
|
||||
def update_edition(identity: UUID, payload: EditionUpdate, user: dict = Depends(require_admin)):
|
||||
try:
|
||||
return store.update_edition(identity.hex, payload.revision, payload.subject, payload.intro,
|
||||
[entry.model_dump() for entry in payload.titles], time.time())
|
||||
except store.Conflict as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.post('/admin/newsletters/editions/{identity}/preview')
|
||||
async def preview(identity: UUID, payload: Version, user: dict = Depends(require_admin)):
|
||||
try:
|
||||
return await service.preview(identity.hex, payload.revision)
|
||||
except (service.NewsletterError, catalog.CatalogError, TimeoutError) as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.post('/admin/newsletters/editions/{identity}/test', status_code=202)
|
||||
def send_test(identity: UUID, payload: Test, user: dict = Depends(require_admin)):
|
||||
try:
|
||||
return service.queue_test(user, identity.hex, payload.revision, str(payload.request_id))
|
||||
except (service.NewsletterError, store.Conflict) as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.post('/admin/newsletters/editions/{identity}/publish', status_code=202)
|
||||
def publish(identity: UUID, payload: Publish, user: dict = Depends(require_admin)):
|
||||
try:
|
||||
return service.publish(identity.hex, payload.revision, payload.send_at)
|
||||
except (service.NewsletterError, store.Conflict) as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.post('/admin/newsletters/editions/{identity}/cancel')
|
||||
def cancel(identity: UUID, user: dict = Depends(require_admin)):
|
||||
try:
|
||||
service.require_edition(identity.hex)
|
||||
return store.cancel(identity.hex, time.time())
|
||||
except service.NewsletterError as exc:
|
||||
fail(exc)
|
||||
|
||||
|
||||
@router.get('/admin/newsletters/artwork/{identity}')
|
||||
async def artwork(identity: UUID, user: dict = Depends(require_admin)):
|
||||
runtime = get_runtime_settings()
|
||||
if not runtime.jellyfin_base_url or not runtime.jellyfin_api_key:
|
||||
raise HTTPException(404, 'Artwork unavailable')
|
||||
content = await catalog.poster(runtime, identity.hex)
|
||||
if not content:
|
||||
raise HTTPException(404, 'Artwork unavailable')
|
||||
return Response(content=content, media_type='image/jpeg', headers={'Cache-Control': 'private, max-age=600'})
|
||||
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from ..auth import get_current_user
|
||||
from ..services.operation_progress import get_operation
|
||||
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/operations",
|
||||
tags=["operations"],
|
||||
dependencies=[Depends(get_current_user)],
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{operation_id}")
|
||||
async def operation_status(operation_id: str) -> dict:
|
||||
operation = get_operation(operation_id)
|
||||
if not operation:
|
||||
raise HTTPException(status_code=404, detail="Operation not found")
|
||||
return operation
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,144 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import Literal
|
||||
from urllib.parse import urlsplit
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
from ..services.public_urls import magent_public_url
|
||||
from ..auth import require_admin
|
||||
from ..feature_guards import require_stats
|
||||
from ..services import email_recaps as recaps, recap_store as store
|
||||
|
||||
|
||||
def no_cache(response: Response):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
|
||||
|
||||
router = APIRouter(tags=["email-recaps"], dependencies=[Depends(no_cache)])
|
||||
|
||||
|
||||
class StrictPayload(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class Preference(StrictPayload):
|
||||
enabled: bool
|
||||
automatic_monthly: bool | None = Field(default=None, strict=True)
|
||||
|
||||
|
||||
class RecapSettings(StrictPayload):
|
||||
enabled: bool
|
||||
day: int = Field(ge=1, le=28)
|
||||
hour: int = Field(ge=0, le=23)
|
||||
public_url: str = Field(default="", max_length=500)
|
||||
|
||||
@field_validator("public_url")
|
||||
@classmethod
|
||||
def origin_only(cls, value: str) -> str:
|
||||
value = value.strip().rstrip('/')
|
||||
if not value:
|
||||
return value
|
||||
try:
|
||||
url = urlsplit(value)
|
||||
port = url.port
|
||||
except ValueError as exc:
|
||||
raise ValueError("Enter the public Magent address, such as https://magent.example.com.") from exc
|
||||
if (url.scheme not in {"http", "https"} or not url.hostname or url.username or url.password
|
||||
or url.path or url.query or url.fragment or any(char.isspace() or ord(char) < 33 for char in value)
|
||||
or any(char in value for char in '<>"\\') or (port is not None and port < 1)):
|
||||
raise ValueError("Enter a http(s) Magent address without a path, credentials or query.")
|
||||
return value
|
||||
|
||||
|
||||
class TestEmail(StrictPayload):
|
||||
month: str | None = Field(default=None, pattern=r"^[0-9]{4}-[0-9]{2}$")
|
||||
request_id: UUID
|
||||
|
||||
|
||||
class TokenAction(StrictPayload):
|
||||
token: str = Field(min_length=40, max_length=100, pattern=r"^[A-Za-z0-9_-]+$")
|
||||
action: Literal["confirm", "unsubscribe"]
|
||||
|
||||
|
||||
def error(exc: recaps.RecapError):
|
||||
raise HTTPException(exc.status, exc.detail) from exc
|
||||
|
||||
|
||||
@router.get("/profile/email-recaps")
|
||||
def preferences(user: dict = Depends(require_stats)) -> dict:
|
||||
try:
|
||||
return recaps.preferences(user)
|
||||
except recaps.RecapError as exc:
|
||||
error(exc)
|
||||
|
||||
|
||||
@router.put("/profile/email-recaps")
|
||||
async def preference(payload: Preference, user: dict = Depends(require_stats)) -> dict:
|
||||
try:
|
||||
if payload.enabled:
|
||||
return await recaps.subscribe(user, payload.automatic_monthly)
|
||||
store.disable(recaps.current_account(user)["id"])
|
||||
return recaps.preferences(user)
|
||||
except recaps.RecapError as exc:
|
||||
error(exc)
|
||||
|
||||
|
||||
@router.post("/email-recaps/check")
|
||||
def check_token(payload: TokenAction) -> dict:
|
||||
try:
|
||||
return recaps.token_action(payload.token, payload.action)
|
||||
except recaps.RecapError as exc:
|
||||
error(exc)
|
||||
|
||||
|
||||
@router.post("/email-recaps/confirm")
|
||||
def apply_token(payload: TokenAction) -> dict:
|
||||
try:
|
||||
return recaps.token_action(payload.token, payload.action, apply=True)
|
||||
except recaps.RecapError as exc:
|
||||
error(exc)
|
||||
|
||||
|
||||
@router.get("/admin/email-recaps")
|
||||
def overview(offset: int = Query(default=0, ge=0), user: dict = Depends(require_admin)) -> dict:
|
||||
ready, detail = recaps.delivery_ready()
|
||||
months = recaps.month_periods(None, datetime.now(timezone.utc))["available_months"][1:]
|
||||
return {"settings": store.settings(), "ready": ready, "detail": detail, "months": months,
|
||||
"worker_enabled": recaps.worker_enabled(), **store.history(offset=offset)}
|
||||
|
||||
|
||||
@router.put("/admin/email-recaps")
|
||||
def settings(payload: RecapSettings, user: dict = Depends(require_admin)) -> dict:
|
||||
if payload.enabled:
|
||||
# Validate against the proposed URL without writing any partial settings.
|
||||
ready, detail = recaps.smtp_email_config_ready()
|
||||
runtime = recaps.get_runtime_settings()
|
||||
if not magent_public_url(payload.public_url or store.settings()["public_url"]) or not ready or not recaps.worker_enabled() or not runtime.jellystat_base_url or not runtime.jellystat_api_key:
|
||||
raise HTTPException(409, "Set the public address, enable SMTP email and connect Jellystat before starting the schedule." if ready else detail)
|
||||
return store.save_settings({**payload.model_dump(), "public_url": magent_public_url(payload.public_url or store.settings()["public_url"])}, datetime.now(timezone.utc))
|
||||
|
||||
|
||||
@router.get("/admin/email-recaps/preview")
|
||||
async def preview(month: str | None = Query(default=None, max_length=7, pattern=r"^[0-9]{4}-[0-9]{2}$"), user: dict = Depends(require_admin)) -> dict:
|
||||
try:
|
||||
return await recaps.preview(user, month)
|
||||
except recaps.RecapError as exc:
|
||||
error(exc)
|
||||
|
||||
|
||||
@router.post("/admin/email-recaps/test", status_code=202)
|
||||
def test_email(payload: TestEmail, user: dict = Depends(require_admin)) -> dict:
|
||||
try:
|
||||
return recaps.queue_test(user, payload.month, str(payload.request_id))
|
||||
except recaps.RecapError as exc:
|
||||
error(exc)
|
||||
|
||||
|
||||
@router.post('/profile/email-recaps/send', status_code=202)
|
||||
def email_personal_report(payload: TestEmail, user: dict = Depends(require_stats)) -> dict:
|
||||
try:
|
||||
return recaps.queue_personal(user, payload.month, str(payload.request_id))
|
||||
except recaps.RecapError as exc:
|
||||
error(exc)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,92 @@
|
||||
"""Initial install bootstrap and authenticated setup wizard endpoints."""
|
||||
|
||||
from inspect import isawaitable
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
||||
from pydantic import Field, SecretStr
|
||||
|
||||
from ..api_models import COMMON_ERROR_RESPONSES, StrictRequest
|
||||
from ..auth import _extract_client_ip, require_admin
|
||||
from ..services import setup as setup_service
|
||||
from ..installation_origin import normalize_application_origin
|
||||
from ..services.request_origins import can_claim_initial_origin
|
||||
|
||||
|
||||
router = APIRouter(prefix="/setup", tags=["setup"], responses=COMMON_ERROR_RESPONSES)
|
||||
|
||||
|
||||
class BootstrapRequest(StrictRequest):
|
||||
setup_token: SecretStr = Field(min_length=1, max_length=1024)
|
||||
username: str = Field(min_length=1, max_length=100)
|
||||
password: SecretStr = Field(min_length=1, max_length=1024)
|
||||
application_url: str | None = Field(default=None, max_length=2048)
|
||||
|
||||
|
||||
class SetupProgress(StrictRequest):
|
||||
step: setup_service.SetupStep
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
def public_status(response: Response) -> dict:
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return setup_service.get_public_setup_status()
|
||||
|
||||
|
||||
@router.post("/bootstrap", status_code=201)
|
||||
def bootstrap(payload: BootstrapRequest, request: Request) -> dict:
|
||||
status = setup_service.get_public_setup_status()
|
||||
if not status["needs_admin"]:
|
||||
raise HTTPException(status_code=409, detail="Initial administrator setup is no longer available.")
|
||||
retry_after = setup_service.consume_bootstrap_attempt(_extract_client_ip(request))
|
||||
if retry_after is not None:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail="Too many setup attempts. Try again later.",
|
||||
headers={"Retry-After": str(retry_after)},
|
||||
)
|
||||
try:
|
||||
application_url = payload.application_url
|
||||
if application_url is not None:
|
||||
application_url = normalize_application_origin(application_url)
|
||||
origin = request.headers.get("origin", "")
|
||||
if not origin or application_url != normalize_application_origin(origin):
|
||||
raise HTTPException(status_code=403, detail="The site address must match the address open in your browser.")
|
||||
elif can_claim_initial_origin():
|
||||
raise HTTPException(status_code=400, detail="Confirm the application URL to create the administrator.")
|
||||
setup_service.bootstrap_administrator(
|
||||
payload.setup_token.get_secret_value(), payload.username, payload.password.get_secret_value(),
|
||||
application_url=application_url,
|
||||
)
|
||||
except setup_service.InvalidSetupTokenError as exc:
|
||||
raise HTTPException(status_code=403, detail=str(exc)) from exc
|
||||
except setup_service.SetupUnavailableError as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
return {"status": "created", "username": payload.username.strip()}
|
||||
|
||||
|
||||
@router.get("/state", dependencies=[Depends(require_admin)])
|
||||
def get_state() -> dict:
|
||||
return setup_service.get_setup_state()
|
||||
|
||||
|
||||
@router.put("/state", dependencies=[Depends(require_admin)])
|
||||
def update_state(payload: SetupProgress) -> dict:
|
||||
return setup_service.update_setup_step(payload.step)
|
||||
|
||||
|
||||
@router.post("/complete", dependencies=[Depends(require_admin)])
|
||||
async def finish_setup(request: Request) -> dict:
|
||||
try:
|
||||
state = setup_service.complete_setup()
|
||||
except setup_service.SetupUnavailableError as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
# Startup owns worker lifecycle. Its callback must be idempotent so retries
|
||||
# after a network interruption cannot start duplicate import/automation jobs.
|
||||
callback = getattr(request.app.state, "on_setup_complete", None)
|
||||
if callback is not None:
|
||||
result = callback()
|
||||
if isawaitable(result):
|
||||
await result
|
||||
return state
|
||||
@@ -0,0 +1,62 @@
|
||||
from typing import Any, Dict
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
|
||||
from ..auth import get_current_user
|
||||
from ..build_info import BUILD_NUMBER, CHANGELOG
|
||||
from ..config import normalize_banner_color
|
||||
from ..runtime import get_runtime_settings
|
||||
|
||||
router = APIRouter(prefix="/site", tags=["site"])
|
||||
|
||||
_BANNER_TONES = {"info", "warning", "error", "maintenance"}
|
||||
|
||||
|
||||
def _build_site_info(include_changelog: bool) -> Dict[str, Any]:
|
||||
runtime = get_runtime_settings()
|
||||
banner_message = (runtime.site_banner_message or "").strip()
|
||||
login_message = (runtime.site_login_message or "").strip()
|
||||
tone = (runtime.site_banner_tone or "info").strip().lower()
|
||||
if tone not in _BANNER_TONES:
|
||||
tone = "info"
|
||||
info = {
|
||||
"buildNumber": (runtime.site_build_number or BUILD_NUMBER or "").strip(),
|
||||
"banner": {
|
||||
"enabled": bool(runtime.site_banner_enabled and banner_message),
|
||||
"message": banner_message,
|
||||
"tone": tone,
|
||||
"backgroundColor": normalize_banner_color(runtime.site_banner_background_color),
|
||||
"borderColor": normalize_banner_color(runtime.site_banner_border_color),
|
||||
},
|
||||
"login": {
|
||||
"message": login_message,
|
||||
"showJellyfinLogin": bool(runtime.site_login_show_jellyfin_login),
|
||||
"showLocalLogin": bool(runtime.site_login_show_local_login),
|
||||
"showForgotPassword": bool(runtime.site_login_show_forgot_password),
|
||||
"showSignupLink": bool(runtime.site_login_show_signup_link),
|
||||
},
|
||||
"navigation": {
|
||||
"showRequests": bool(runtime.site_nav_show_requests),
|
||||
},
|
||||
}
|
||||
if include_changelog:
|
||||
info["changelog"] = (CHANGELOG or "").strip()
|
||||
playback_url = (runtime.jellyfin_public_url or "").strip()
|
||||
try:
|
||||
parsed = urlsplit(playback_url)
|
||||
valid = parsed.scheme in {"http", "https"} and bool(parsed.hostname) and not parsed.username and not parsed.password
|
||||
except ValueError:
|
||||
valid = False
|
||||
info["mediaServerUrl"] = playback_url if valid else None
|
||||
return info
|
||||
|
||||
|
||||
@router.get("/public")
|
||||
async def site_public() -> Dict[str, Any]:
|
||||
return _build_site_info(False)
|
||||
|
||||
|
||||
@router.get("/info")
|
||||
async def site_info(user: Dict[str, Any] = Depends(get_current_user)) -> Dict[str, Any]:
|
||||
return _build_site_info(True)
|
||||
@@ -0,0 +1,188 @@
|
||||
from typing import Any, Dict
|
||||
import httpx
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from ..auth import require_admin
|
||||
from ..runtime import get_runtime_settings
|
||||
from ..clients.jellyseerr import JellyseerrClient
|
||||
from ..clients.sonarr import SonarrClient
|
||||
from ..clients.radarr import RadarrClient
|
||||
from ..clients.bazarr import BazarrClient
|
||||
from ..clients.prowlarr import ProwlarrClient
|
||||
from ..clients.qbittorrent import QBittorrentClient
|
||||
from ..clients.jellyfin import JellyfinClient
|
||||
from ..clients.jellystat import JellystatClient
|
||||
|
||||
router = APIRouter(prefix="/status", tags=["status"], dependencies=[Depends(require_admin)])
|
||||
|
||||
|
||||
async def _check(name: str, configured: bool, func) -> Dict[str, Any]:
|
||||
if not configured:
|
||||
return {"name": name, "status": "not_configured"}
|
||||
try:
|
||||
result = await func()
|
||||
return {"name": name, "status": "up", "detail": result}
|
||||
except httpx.HTTPError as exc:
|
||||
return {"name": name, "status": "down", "message": str(exc)}
|
||||
except Exception as exc:
|
||||
return {"name": name, "status": "down", "message": str(exc)}
|
||||
|
||||
|
||||
async def _check_qbittorrent(qbittorrent: QBittorrentClient) -> Dict[str, Any]:
|
||||
if not qbittorrent.base_url:
|
||||
return {"name": "qBittorrent", "status": "not_configured"}
|
||||
if not qbittorrent.username or not qbittorrent.password:
|
||||
reachable = await qbittorrent.is_webui_reachable()
|
||||
return {
|
||||
"name": "qBittorrent",
|
||||
"status": "degraded" if reachable else "not_configured",
|
||||
"message": "qBittorrent credentials are incomplete" if reachable else "qBittorrent is not fully configured",
|
||||
}
|
||||
try:
|
||||
result = await qbittorrent.get_app_version()
|
||||
return {"name": "qBittorrent", "status": "up", "detail": result}
|
||||
except RuntimeError as exc:
|
||||
if "login failed" in str(exc).lower():
|
||||
reachable = await qbittorrent.is_webui_reachable()
|
||||
if reachable:
|
||||
return {
|
||||
"name": "qBittorrent",
|
||||
"status": "degraded",
|
||||
"message": "qBittorrent is reachable but the saved credentials were rejected",
|
||||
}
|
||||
return {"name": "qBittorrent", "status": "down", "message": str(exc)}
|
||||
except httpx.HTTPError as exc:
|
||||
return {"name": "qBittorrent", "status": "down", "message": str(exc)}
|
||||
except Exception as exc:
|
||||
return {"name": "qBittorrent", "status": "down", "message": str(exc)}
|
||||
|
||||
|
||||
@router.get("/services")
|
||||
async def services_status() -> Dict[str, Any]:
|
||||
runtime = get_runtime_settings()
|
||||
jellyseerr = JellyseerrClient(runtime.jellyseerr_base_url, runtime.jellyseerr_api_key)
|
||||
sonarr = SonarrClient(runtime.sonarr_base_url, runtime.sonarr_api_key)
|
||||
radarr = RadarrClient(runtime.radarr_base_url, runtime.radarr_api_key)
|
||||
bazarr = BazarrClient(runtime.bazarr_base_url, runtime.bazarr_api_key)
|
||||
prowlarr = ProwlarrClient(runtime.prowlarr_base_url, runtime.prowlarr_api_key)
|
||||
qbittorrent = QBittorrentClient(
|
||||
runtime.qbittorrent_base_url, runtime.qbittorrent_username, runtime.qbittorrent_password
|
||||
)
|
||||
jellyfin = JellyfinClient(runtime.jellyfin_base_url, runtime.jellyfin_api_key)
|
||||
|
||||
services = []
|
||||
services.append(
|
||||
await _check(
|
||||
"Seerr",
|
||||
jellyseerr.configured(),
|
||||
lambda: jellyseerr.get_recent_requests(take=1, skip=0),
|
||||
)
|
||||
)
|
||||
services.append(
|
||||
await _check(
|
||||
"Sonarr",
|
||||
sonarr.configured(),
|
||||
sonarr.get_system_status,
|
||||
)
|
||||
)
|
||||
services.append(
|
||||
await _check(
|
||||
"Radarr",
|
||||
radarr.configured(),
|
||||
radarr.get_system_status,
|
||||
)
|
||||
)
|
||||
services.append(
|
||||
await _check(
|
||||
"Bazarr",
|
||||
bazarr.configured() and bool(runtime.bazarr_api_key),
|
||||
bazarr.get_system_status,
|
||||
)
|
||||
)
|
||||
prowlarr_status = await _check(
|
||||
"Prowlarr",
|
||||
prowlarr.configured(),
|
||||
prowlarr.get_health,
|
||||
)
|
||||
if prowlarr_status.get("status") == "up":
|
||||
health = prowlarr_status.get("detail")
|
||||
if isinstance(health, list) and health:
|
||||
prowlarr_status["status"] = "degraded"
|
||||
prowlarr_status["message"] = "Health warnings"
|
||||
services.append(prowlarr_status)
|
||||
services.append(await _check_qbittorrent(qbittorrent))
|
||||
services.append(
|
||||
await _check(
|
||||
"Jellyfin",
|
||||
jellyfin.configured(),
|
||||
jellyfin.get_system_info,
|
||||
)
|
||||
)
|
||||
|
||||
jellystat = JellystatClient(runtime.jellystat_base_url, runtime.jellystat_api_key)
|
||||
# Optional analytics must not degrade the media pipeline when not configured.
|
||||
if jellystat.configured():
|
||||
services.append(await _check("Jellystat", True, jellystat.test_connection))
|
||||
|
||||
overall = "up"
|
||||
if any(s.get("status") == "down" for s in services):
|
||||
overall = "down"
|
||||
elif any(s.get("status") in {"degraded", "not_configured"} for s in services):
|
||||
overall = "degraded"
|
||||
|
||||
return {"overall": overall, "services": services}
|
||||
|
||||
|
||||
@router.post("/services/{service}/test")
|
||||
async def test_service(service: str) -> Dict[str, Any]:
|
||||
runtime = get_runtime_settings()
|
||||
jellyseerr = JellyseerrClient(runtime.jellyseerr_base_url, runtime.jellyseerr_api_key)
|
||||
sonarr = SonarrClient(runtime.sonarr_base_url, runtime.sonarr_api_key)
|
||||
radarr = RadarrClient(runtime.radarr_base_url, runtime.radarr_api_key)
|
||||
bazarr = BazarrClient(runtime.bazarr_base_url, runtime.bazarr_api_key)
|
||||
prowlarr = ProwlarrClient(runtime.prowlarr_base_url, runtime.prowlarr_api_key)
|
||||
qbittorrent = QBittorrentClient(
|
||||
runtime.qbittorrent_base_url, runtime.qbittorrent_username, runtime.qbittorrent_password
|
||||
)
|
||||
jellyfin = JellyfinClient(runtime.jellyfin_base_url, runtime.jellyfin_api_key)
|
||||
|
||||
service_key = service.strip().lower()
|
||||
if service_key == "jellystat":
|
||||
jellystat = JellystatClient(runtime.jellystat_base_url, runtime.jellystat_api_key)
|
||||
return await _check("Jellystat", jellystat.configured(), jellystat.test_connection)
|
||||
checks = {
|
||||
"seerr": (
|
||||
"Seerr",
|
||||
jellyseerr.configured(),
|
||||
lambda: jellyseerr.get_recent_requests(take=1, skip=0),
|
||||
),
|
||||
"jellyseerr": (
|
||||
"Seerr",
|
||||
jellyseerr.configured(),
|
||||
lambda: jellyseerr.get_recent_requests(take=1, skip=0),
|
||||
),
|
||||
"sonarr": ("Sonarr", sonarr.configured(), sonarr.get_system_status),
|
||||
"radarr": ("Radarr", radarr.configured(), radarr.get_system_status),
|
||||
"bazarr": (
|
||||
"Bazarr",
|
||||
bazarr.configured() and bool(runtime.bazarr_api_key),
|
||||
bazarr.get_system_status,
|
||||
),
|
||||
"prowlarr": ("Prowlarr", prowlarr.configured(), prowlarr.get_health),
|
||||
"jellyfin": ("Jellyfin", jellyfin.configured(), jellyfin.get_system_info),
|
||||
}
|
||||
|
||||
if service_key == "qbittorrent":
|
||||
return await _check_qbittorrent(qbittorrent)
|
||||
|
||||
if service_key not in checks:
|
||||
raise HTTPException(status_code=404, detail="Unknown service")
|
||||
|
||||
name, configured, func = checks[service_key]
|
||||
result = await _check(name, configured, func)
|
||||
if name == "Prowlarr" and result.get("status") == "up":
|
||||
health = result.get("detail")
|
||||
if isinstance(health, list) and health:
|
||||
result["status"] = "degraded"
|
||||
result["message"] = "Health warnings"
|
||||
return result
|
||||
Reference in New Issue
Block a user