✨ UI dynamique (couleurs par etat, spinner, terminal de conversion live, restes de conversion, liens MKV), responsive, scan ISO par contenu, retrait Sonarr/Prowlarr/Jellyfin/Seerr
This commit is contained in:
1 parent
078905e944
commit
70ce0b95a8
20 files changed
+793
-1143
No files matched your search
+76
-92
@@ -14,6 +14,7 @@ from sqlalchemy import select, update
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app import auth
|
||||
from app import worker as conversion_queue
|
||||
from app.conf_store import (
|
||||
ApiServiceConfig,
|
||||
QbittorrentConfig,
|
||||
@@ -21,23 +22,30 @@ from app.conf_store import (
|
||||
read_config,
|
||||
record_test,
|
||||
reset_test,
|
||||
service_configured,
|
||||
service_status,
|
||||
write_config,
|
||||
)
|
||||
from app.converter import cancel_conversion, conversion_paths, finalize_validation
|
||||
from app.converter import (
|
||||
cancel_conversion,
|
||||
clear_workspace,
|
||||
conversion_paths,
|
||||
finalize_validation,
|
||||
looks_like_iso,
|
||||
workspace_files,
|
||||
)
|
||||
from app.database import Base, engine, get_db
|
||||
from app.migrations import migrate_conf_file, run_db_migrations
|
||||
from app.models import IsoMovie
|
||||
from app.qbittorrent import QbittorrentClient, match_torrent
|
||||
from app.radarr import RadarrClient, movie_file_path
|
||||
from app.schemas import (
|
||||
ApiServiceSettingsRead,
|
||||
AuthCredentialsWrite,
|
||||
AuthSetupWrite,
|
||||
AuthUserRead,
|
||||
ConnectionResult,
|
||||
ConversionLogRead,
|
||||
HealthRead,
|
||||
IsoMovieDetailRead,
|
||||
IsoMovieRead,
|
||||
QbittorrentSettingsRead,
|
||||
QbittorrentSettingsWrite,
|
||||
@@ -82,8 +90,8 @@ app.add_middleware(
|
||||
DbSession = Annotated[Session, Depends(get_db)]
|
||||
|
||||
|
||||
def _api_service_read(service: ApiServiceConfig) -> ApiServiceSettingsRead:
|
||||
return ApiServiceSettingsRead(
|
||||
def _api_service_read(service: ApiServiceConfig) -> RadarrSettingsRead:
|
||||
return RadarrSettingsRead(
|
||||
url=service.url,
|
||||
port=service.port,
|
||||
api_key_configured=service.api_key is not None,
|
||||
@@ -112,10 +120,6 @@ def _settings_read() -> SettingsRead:
|
||||
cfg = read_config()
|
||||
return SettingsRead(
|
||||
radarr=_api_service_read(cfg.radarr),
|
||||
sonarr=_api_service_read(cfg.sonarr),
|
||||
prowlarr=_api_service_read(cfg.prowlarr),
|
||||
jellyfin=_api_service_read(cfg.jellyfin),
|
||||
seerr=_api_service_read(cfg.seerr),
|
||||
qbittorrent=_qbittorrent_read(cfg.qbittorrent),
|
||||
)
|
||||
|
||||
@@ -191,10 +195,6 @@ def put_settings(payload: SettingsWrite) -> SettingsRead:
|
||||
target.api_key = patch.api_key
|
||||
|
||||
apply_service(cfg.radarr, payload.radarr)
|
||||
apply_service(cfg.sonarr, payload.sonarr)
|
||||
apply_service(cfg.prowlarr, payload.prowlarr)
|
||||
apply_service(cfg.jellyfin, payload.jellyfin)
|
||||
apply_service(cfg.seerr, payload.seerr)
|
||||
|
||||
if payload.qbittorrent is not None:
|
||||
qb = payload.qbittorrent
|
||||
@@ -232,74 +232,6 @@ async def test_radarr() -> ConnectionResult:
|
||||
return ConnectionResult(success=True, version=version, message=f"Radarr {version} accessible")
|
||||
|
||||
|
||||
# --- Tests de connexion des autres services API (Sonarr, Prowlarr, Jellyfin,
|
||||
# Overseerr). Chacun expose un endpoint systeme et une version. ---
|
||||
API_SERVICE_TESTS: dict[str, tuple[str, str, str]] = {
|
||||
# service: (endpoint, nom de l'en-tete d'authentification, cle de version)
|
||||
"sonarr": ("api/v3/system/status", "X-Api-Key", "version"),
|
||||
"prowlarr": ("api/v1/system/status", "X-Api-Key", "version"),
|
||||
"jellyfin": ("System/Info", "X-Emby-Token", "Version"),
|
||||
"seerr": ("api/v1/status", "X-Api-Key", "version"),
|
||||
}
|
||||
|
||||
SERVICE_LABELS = {
|
||||
"sonarr": "Sonarr",
|
||||
"prowlarr": "Prowlarr",
|
||||
"jellyfin": "Jellyfin",
|
||||
"seerr": "Overseerr",
|
||||
}
|
||||
|
||||
|
||||
async def _test_api_service(name: str) -> ConnectionResult:
|
||||
service = getattr(read_config(), name)
|
||||
label = SERVICE_LABELS[name]
|
||||
url = build_base_url(service.url, service.port)
|
||||
if not service_configured(service):
|
||||
raise HTTPException(
|
||||
status_code=409, detail=f"La connexion {label} n'est pas configurée"
|
||||
)
|
||||
endpoint, auth_header, version_key = API_SERVICE_TESTS[name]
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=30) as client:
|
||||
response = await client.get(
|
||||
f"{url}/{endpoint}", headers={auth_header: service.api_key}
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
except (httpx.HTTPError, ValueError) as error:
|
||||
record_test(name, ok=False, error=str(error))
|
||||
raise HTTPException(
|
||||
status_code=502, detail=f"Connexion {label} impossible: {error}"
|
||||
) from error
|
||||
if not isinstance(data, dict):
|
||||
error = "Réponse inattendue du service"
|
||||
record_test(name, ok=False, error=error)
|
||||
raise HTTPException(status_code=502, detail=f"Connexion {label} impossible: {error}")
|
||||
version = str(data.get(version_key, "inconnue"))
|
||||
record_test(name, ok=True, version=version)
|
||||
return ConnectionResult(success=True, version=version, message=f"{label} {version} accessible")
|
||||
|
||||
|
||||
@app.post("/api/settings/sonarr/test", response_model=ConnectionResult)
|
||||
async def test_sonarr() -> ConnectionResult:
|
||||
return await _test_api_service("sonarr")
|
||||
|
||||
|
||||
@app.post("/api/settings/prowlarr/test", response_model=ConnectionResult)
|
||||
async def test_prowlarr() -> ConnectionResult:
|
||||
return await _test_api_service("prowlarr")
|
||||
|
||||
|
||||
@app.post("/api/settings/jellyfin/test", response_model=ConnectionResult)
|
||||
async def test_jellyfin() -> ConnectionResult:
|
||||
return await _test_api_service("jellyfin")
|
||||
|
||||
|
||||
@app.post("/api/settings/seerr/test", response_model=ConnectionResult)
|
||||
async def test_seerr() -> ConnectionResult:
|
||||
return await _test_api_service("seerr")
|
||||
|
||||
|
||||
def qbittorrent_from_config() -> QbittorrentClient:
|
||||
qb = read_config().qbittorrent
|
||||
url = build_base_url(qb.url, qb.port)
|
||||
@@ -371,22 +303,42 @@ def list_files(db: DbSession) -> list[IsoMovie]:
|
||||
return list_movies(db)
|
||||
|
||||
|
||||
@app.get("/api/files/{file_id}", response_model=IsoMovieRead)
|
||||
def get_file(file_id: int, db: DbSession) -> IsoMovie:
|
||||
def _movie_detail(movie: IsoMovie) -> IsoMovieDetailRead:
|
||||
"""Fiche detaillee: chemins du MKV brut et du MKV lie, etat du dossier."""
|
||||
paths = conversion_paths(movie)
|
||||
linked = _linked_mkv(movie)
|
||||
detail = IsoMovieDetailRead.model_validate(movie)
|
||||
detail.work_mkv_path = str(paths.mkv_path) if paths.mkv_path.is_file() else None
|
||||
detail.linked_mkv_path = str(linked) if linked is not None else None
|
||||
detail.workspace_folder = str(paths.downloads_folder)
|
||||
detail.workspace_files = workspace_files(paths.downloads_folder)
|
||||
return detail
|
||||
|
||||
|
||||
@app.get("/api/files/{file_id}", response_model=IsoMovieDetailRead)
|
||||
def get_file(file_id: int, db: DbSession) -> IsoMovieDetailRead:
|
||||
item = db.get(IsoMovie, file_id)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="Fichier introuvable")
|
||||
return item
|
||||
return _movie_detail(item)
|
||||
|
||||
|
||||
@app.get("/api/movies/{movie_id}/log", response_model=ConversionLogRead)
|
||||
def get_conversion_log(movie_id: int, db: DbSession) -> ConversionLogRead:
|
||||
"""Journal de la conversion (terminal de suivi), conserve apres echec."""
|
||||
movie = db.get(IsoMovie, movie_id)
|
||||
if movie is None:
|
||||
raise HTTPException(status_code=404, detail="Film introuvable")
|
||||
return ConversionLogRead(
|
||||
lines=conversion_queue.conversion_log(movie_id),
|
||||
running=conversion_queue.is_converting(movie_id),
|
||||
)
|
||||
|
||||
|
||||
def _log_config_summary() -> None:
|
||||
"""Rend visible au lancement le chargement de conf.json."""
|
||||
cfg = read_config()
|
||||
configured = [
|
||||
name
|
||||
for name in ("radarr", "sonarr", "prowlarr", "jellyfin", "seerr")
|
||||
if getattr(cfg, name).url
|
||||
]
|
||||
configured = ["radarr"] if cfg.radarr.url else []
|
||||
if cfg.qbittorrent.url:
|
||||
if cfg.qbittorrent.api_key:
|
||||
mode = "cle API"
|
||||
@@ -428,7 +380,9 @@ async def scan_radarr(db: DbSession) -> ScanResult:
|
||||
for movie in movies:
|
||||
movie_file = movie.get("movieFile") or {}
|
||||
path = movie_file_path(movie)
|
||||
if not path or not path.lower().endswith(".iso"):
|
||||
# L'extension preslectionne, mais des MKV circulent parfois sous un
|
||||
# nom en .iso: on verifie l'en-tete du contenu.
|
||||
if not path or not looks_like_iso(path):
|
||||
continue
|
||||
movie_id = movie.get("id")
|
||||
movie_file_id = movie_file.get("id")
|
||||
@@ -513,7 +467,14 @@ async def _delete_iso_torrent(iso_path: str) -> None:
|
||||
|
||||
|
||||
@app.post("/api/movies/{movie_id}/convert", response_model=IsoMovieRead)
|
||||
def convert_movie(movie_id: int, db: DbSession) -> IsoMovie:
|
||||
def convert_movie(movie_id: int, db: DbSession, force: bool = False) -> IsoMovie:
|
||||
"""Met la conversion en file.
|
||||
|
||||
Refuse (409, code ``workspace_not_empty``) si le dossier de travail
|
||||
contient des restes d'anciennes conversions, sauf si ``force`` est vrai:
|
||||
le contenu est alors supprime pour qu'un seul MKV soit produit a la fin.
|
||||
Une reprise apres echec d'import (MKV deja extrait) n'est pas bloquee.
|
||||
"""
|
||||
movie = db.get(IsoMovie, movie_id)
|
||||
if movie is None:
|
||||
raise HTTPException(status_code=404, detail="Film introuvable")
|
||||
@@ -521,8 +482,31 @@ def convert_movie(movie_id: int, db: DbSession) -> IsoMovie:
|
||||
raise HTTPException(
|
||||
status_code=409, detail="Ce film n'est pas convertible dans son état actuel"
|
||||
)
|
||||
paths = conversion_paths(movie)
|
||||
resumable = movie.failed_step == "importing" and paths.mkv_path.is_file()
|
||||
leftovers = [
|
||||
name
|
||||
for name in workspace_files(paths.downloads_folder)
|
||||
if not (resumable and (paths.downloads_folder / name) == paths.mkv_path)
|
||||
]
|
||||
if leftovers and not force:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"code": "workspace_not_empty",
|
||||
"message": (
|
||||
"Le dossier de travail contient des fichiers issus d'une ancienne "
|
||||
"conversion. Supprimez-les pour ne garder qu'un seul MKV en fin "
|
||||
"de conversion."
|
||||
),
|
||||
"folder": str(paths.downloads_folder),
|
||||
"files": leftovers,
|
||||
},
|
||||
)
|
||||
if leftovers:
|
||||
clear_workspace(paths.downloads_folder)
|
||||
paths.downloads_folder.mkdir(parents=True, exist_ok=True)
|
||||
movie.status = "queued"
|
||||
movie.failed_step = None
|
||||
movie.last_error = None
|
||||
db.commit()
|
||||
return movie
|
||||
|
||||
Reference in new issue
Block a user