This commit is contained in:
2026-04-28 11:05:42 +07:00
parent 7cace210d6
commit 81c727b7a6
8 changed files with 102 additions and 99 deletions
+29 -22
View File
@@ -1,7 +1,6 @@
from __future__ import annotations
import asyncio
import os
import logging
from pathlib import Path
from typing import Optional
@@ -23,19 +22,12 @@ from .models import (
from .recordings import list_recordings
from .scheduler import Scheduler
try:
from dotenv import load_dotenv
load_dotenv(Path(__file__).resolve().parents[2] / ".env")
except Exception:
pass
def _cors_origins() -> list[str]:
raw = os.getenv("CORS_ORIGINS", "http://localhost:5173")
return [x.strip() for x in raw.split(",") if x.strip()]
return ["http://localhost:5173"]
logger = logging.getLogger("ipcam_dashboard")
PROJECT_ROOT = Path(__file__).resolve().parents[2]
DEFAULT_LOCAL_RECORDINGS_DIR = PROJECT_ROOT / "mediamtx" / "recordings"
app = FastAPI(title="IPCam Dashboard API")
@@ -58,8 +50,8 @@ async def _apply_recording(enabled: bool) -> None:
client = MediaMTXClient(
api_url=cfg.mediamtx_api_url,
username=os.getenv("MEDIAMTX_API_USER"),
password=os.getenv("MEDIAMTX_API_PASS"),
username=cfg.mediamtx_api_user,
password=cfg.mediamtx_api_pass,
)
await client.set_recording_bulk(names, enabled)
@@ -91,9 +83,24 @@ def _raise_mediamtx_http_error(err: httpx.HTTPError) -> None:
@app.on_event("startup")
async def _startup() -> None:
cfg = await store.load()
recordings_dir = cfg.recordings_dir
Path(recordings_dir).mkdir(parents=True, exist_ok=True)
app.mount("/videos", StaticFiles(directory=recordings_dir), name="videos")
recordings_dir = Path(cfg.recordings_dir)
# Backward-compatible migration: old defaults used "/recordings".
if str(recordings_dir) == "/recordings":
recordings_dir = DEFAULT_LOCAL_RECORDINGS_DIR
cfg.recordings_dir = str(recordings_dir)
await store.save(cfg)
try:
recordings_dir.mkdir(parents=True, exist_ok=True)
except PermissionError:
# Final fallback for local dev/deploy on same machine.
recordings_dir = DEFAULT_LOCAL_RECORDINGS_DIR
recordings_dir.mkdir(parents=True, exist_ok=True)
cfg.recordings_dir = str(recordings_dir)
await store.save(cfg)
app.mount("/videos", StaticFiles(directory=str(recordings_dir)), name="videos")
asyncio.create_task(_scheduler_loop())
@@ -118,8 +125,8 @@ async def add_camera(camera: Camera) -> AppConfig:
client = MediaMTXClient(
api_url=cfg.mediamtx_api_url,
username=os.getenv("MEDIAMTX_API_USER"),
password=os.getenv("MEDIAMTX_API_PASS"),
username=cfg.mediamtx_api_user,
password=cfg.mediamtx_api_pass,
)
try:
await client.upsert_paths_sources_bulk({camera.name: camera.rtsp_url})
@@ -139,8 +146,8 @@ async def delete_camera(name: str) -> AppConfig:
client = MediaMTXClient(
api_url=cfg.mediamtx_api_url,
username=os.getenv("MEDIAMTX_API_USER"),
password=os.getenv("MEDIAMTX_API_PASS"),
username=cfg.mediamtx_api_user,
password=cfg.mediamtx_api_pass,
)
try:
await client.delete_path(name)
@@ -154,8 +161,8 @@ async def list_paths() -> dict:
cfg = await store.load()
client = MediaMTXClient(
api_url=cfg.mediamtx_api_url,
username=os.getenv("MEDIAMTX_API_USER"),
password=os.getenv("MEDIAMTX_API_PASS"),
username=cfg.mediamtx_api_user,
password=cfg.mediamtx_api_pass,
)
try:
return await client.list_paths_status()