diff --git a/backend/api/media_api.py b/backend/api/media_api.py index e534ca3f..98ad3864 100644 --- a/backend/api/media_api.py +++ b/backend/api/media_api.py @@ -33,6 +33,8 @@ from backend.util.arr_delete import ArrDeleter, ArrOutcome from backend.util.arr_instances import instance_detail from backend.util.config import ConfigError, load_config +from backend.util.helper import collection_title_fields +from backend.util.normalization import normalize_titles from backend.util.public_error import failure_text from backend.util.database import ( INCOMPLETE_METADATA_FIELDS, @@ -746,6 +748,56 @@ def delete_collection( ) +def _is_text(value: Any) -> bool: + """A string with more than whitespace in it.""" + return isinstance(value, str) and bool(value.strip()) + + +def _scalar_fields(payload: dict) -> Tuple[Dict[str, Any], Optional[str]]: + """The year, ids, folder and library ``payload`` sets, or why one is invalid.""" + fields: Dict[str, Any] = {} + for key in ("year", "tmdb_id", "tvdb_id"): + if key in payload: + number = None if payload[key] is None else _media_id(payload[key]) + if payload[key] is not None and number is None: + return {}, f"Field '{key}' must be a positive whole number or null" + fields[key] = number + for key in ("imdb_id", "folder"): + if key in payload: + if payload[key] is not None and not isinstance(payload[key], str): + return {}, f"Field '{key}' must be a string or null" + fields[key] = payload[key] + if "library_name" in payload: + if not _is_text(payload["library_name"]): + return {}, "Field 'library_name' must be a non-empty string" + fields["library_name"] = payload["library_name"] + return fields, None + + +def _collection_fields(payload: dict) -> Tuple[Dict[str, Any], Optional[str]]: + """The collection columns ``payload`` sets (a title derived as a Plex sync derives it), or why one is invalid.""" + fields, why = _scalar_fields(payload) + if why: + return {}, why + if "title" in payload: + if not _is_text(payload["title"]): + return {}, "Field 'title' must be a non-empty string" + titles = collection_title_fields(payload["title"].strip()) + if not _is_text(titles["title"]): + return {}, "Field 'title' is blank once decoded to ASCII" + fields.update(titles) + if "alternate_titles" in payload: + alts = payload["alternate_titles"] + if not isinstance(alts, list) or not all(_is_text(t) for t in alts): + return {}, "Field 'alternate_titles' must be a list of non-empty strings" + alts = [t.strip() for t in alts] + normalized = [normalize_titles(t) for t in alts] + if not all(normalized): + return {}, "Field 'alternate_titles' has a title that is blank once normalized" + fields.update(alternate_titles=alts, normalized_alternate_titles=normalized) + return fields, None + + @router.post( "/collections", summary="Create collection", @@ -763,7 +815,7 @@ def delete_collection( } }, }, - 400: {"description": "Missing 'title' or 'instance_name'"}, + 400: {"description": "Missing 'title', 'instance_name' or 'library_name', or a field of the wrong type"}, 413: {"description": "Request body too large"}, }, ) @@ -787,40 +839,40 @@ async def create_collection( return body_too_large_error() logger.debug(f"Serving POST /api/media/collections with payload: {payload}") - title = payload.get("title") - if not title: + if "title" not in payload: return error( "Field 'title' is required", code="MISSING_TITLE", status_code=400 ) - instance_name = payload.get("instance_name") - if not instance_name: + if "instance_name" not in payload: return error( "Field 'instance_name' is required", code="MISSING_INSTANCE", status_code=400, ) + instance_name = payload["instance_name"] + if not _is_text(instance_name): + return error( + "Field 'instance_name' must be a non-empty string", + code="INVALID_FIELDS", + status_code=400, + ) - record = { - "title": title, - "normalized_title": title.lower().strip() if title else None, - "year": payload.get("year"), - "tmdb_id": payload.get("tmdb_id"), - "tvdb_id": payload.get("tvdb_id"), - "imdb_id": payload.get("imdb_id"), - "folder": payload.get("folder"), - "library_name": payload.get("library_name"), - "alternate_titles": payload.get("alternate_titles", []), - "normalized_alternate_titles": payload.get( - "normalized_alternate_titles", [] - ), - } + if "library_name" not in payload: + return error( + "Field 'library_name' is required", + code="MISSING_LIBRARY", + status_code=400, + ) + record, why = _collection_fields(payload) + if why: + return error(why, code="INVALID_FIELDS", status_code=400) db.collection.upsert(record, instance_name) - # Fetch the created/updated record + # Read back by the stored title: the sync's derivation may have changed it. created = db.collection.get_by_title_and_instance( - title, instance_name, record["library_name"] + record["title"], instance_name, record["library_name"] ) return ok("Collection created successfully", {"collection": created}) @@ -852,7 +904,10 @@ async def create_collection( } } }, - } + }, + 400: {"description": "No updatable field, or a field of the wrong type"}, + 404: {"description": "No collection with that id"}, + 409: {"description": "Another collection in this library already has that title"}, }, ) async def update_collection( @@ -865,7 +920,7 @@ async def update_collection( Update a media collection by ID. Fetches the existing collection, merges in the provided updates, - and upserts the result back to the database. + and writes the result back onto that row. Args: collection_id: The unique identifier of the collection to update @@ -877,9 +932,8 @@ async def update_collection( payload = await read_request_json(request) if payload is BODY_TOO_LARGE: return body_too_large_error() - # An unusable body used to raise into the 500 handler; keep that - # response — falling through would upsert the record unchanged. - if not isinstance(payload, dict) or not payload: + # A non-object body used to raise into the 500 handler; keep that response. + if not isinstance(payload, dict): return error( "Error updating collection", code="COLLECTION_UPDATE_ERROR", @@ -897,33 +951,27 @@ async def update_collection( status_code=404, ) - # Merge updates into existing record - updatable_fields = [ - "title", - "year", - "tmdb_id", - "tvdb_id", - "imdb_id", - "folder", - "library_name", - "alternate_titles", - "normalized_alternate_titles", - ] - for field in updatable_fields: - if field in payload: - existing[field] = payload[field] - - # Update normalized_title if title changed - if "title" in payload: - existing["normalized_title"] = ( - payload["title"].lower().strip() if payload["title"] else None - ) - - instance_name = existing.get("instance_name") - db.collection.upsert(existing, instance_name) - - # Fetch the updated record + fields, why = _collection_fields(payload) + if why: + return error(why, code="INVALID_FIELDS", status_code=400) + if not fields: + return error("No valid fields to update", code="NO_FIELDS", status_code=400) + existing.update(fields) + # In place by id: an upsert keyed on the new title would add a second row. + written = db.collection.update_row(collection_id, existing) updated = db.collection.get_by_id(collection_id) + if updated is None: + return error( + f"Collection {collection_id} not found", + code="COLLECTION_NOT_FOUND", + status_code=404, + ) + if not written: + return error( + "Another collection in this library already has that title", + code="COLLECTION_EXISTS", + status_code=409, + ) return ok("Collection updated successfully", {"collection": updated}) diff --git a/backend/modules/poster_renamerr.py b/backend/modules/poster_renamerr.py index 9e7d22c7..4d560f63 100755 --- a/backend/modules/poster_renamerr.py +++ b/backend/modules/poster_renamerr.py @@ -27,6 +27,7 @@ ) from backend.util.helper import ( as_list, + parse_alternate_titles, classify_match, is_id_match_reason, create_table, @@ -529,15 +530,7 @@ def find_asset_candidate( season_number = media.get("season_number") expected_asset_type = "collection" if is_collection else asset_type - alt_titles = [] - try: - alt_titles = json.loads(media.get("alternate_titles") or "[]") - except Exception: - pass - # The collections API stores these unchecked: keep only the strings. - if not isinstance(alt_titles, list): - alt_titles = [] - alt_titles = [t for t in alt_titles if isinstance(t, str) and t] + alt_titles = parse_alternate_titles(media.get("alternate_titles")) reasons = [] candidates = [] @@ -644,9 +637,7 @@ def _title_match_reason( """is_match()'s reason when a title-lookup poster belongs to ``media``, else "".""" if cand.get("season_number") != media.get("season_number"): return "" - cand_alt_titles = set( - json.loads(cand.get("normalized_alternate_titles", "[]") or "[]") - ) + cand_alt_titles = set(parse_alternate_titles(cand.get("normalized_alternate_titles"))) if ( cand.get("normalized_title", "") not in all_titles and not all_titles & cand_alt_titles diff --git a/backend/util/asset_candidates.py b/backend/util/asset_candidates.py index 283e237a..92358e41 100644 --- a/backend/util/asset_candidates.py +++ b/backend/util/asset_candidates.py @@ -6,10 +6,9 @@ """ import difflib -import json from typing import Any, Dict, List, Optional, Tuple -from backend.util.helper import is_match +from backend.util.helper import is_match, parse_alternate_titles from backend.util.normalization import normalize_titles @@ -22,11 +21,7 @@ def rank_candidates( ) -> List[Tuple[Dict[str, Any], bool, float, str]]: """Best-first (row, would_match, similarity, reason) tuples for a media row.""" season_number = row.get("season_number") - try: - alts = json.loads(row.get("alternate_titles") or "[]") - except (ValueError, TypeError): - alts = [] - search_titles = [row.get("title")] + [a for a in alts if a] + search_titles = [row.get("title")] + parse_alternate_titles(row.get("alternate_titles")) row_norm = row.get("normalized_title") or normalize_titles(row.get("title") or "") seen = set() diff --git a/backend/util/database/collection_cache.py b/backend/util/database/collection_cache.py index 1a869097..9db4173d 100755 --- a/backend/util/database/collection_cache.py +++ b/backend/util/database/collection_cache.py @@ -10,18 +10,51 @@ class CollectionCache(DatabaseBase): Provides CRUD and sync operations. """ - def upsert(self, record: dict, instance_name: str) -> None: - """Insert or update a collection record for a given instance.""" - record["instance_name"] = instance_name - record["asset_type"] = "collection" - - # Serialize list/dict fields to JSON + @staticmethod + def _store_json(record: dict) -> None: + """Serialize ``record``'s title lists to JSON in place; a missing list is stored empty.""" for key in ("alternate_titles", "normalized_alternate_titles"): if isinstance(record.get(key), (list, dict)): record[key] = json.dumps(record[key]) elif record.get(key) is None: record[key] = json.dumps([]) + def update_row(self, id: int, record: dict) -> bool: + """Write ``record``'s fields onto row ``id`` in place; False when that row is gone or another holds its key.""" + self._store_json(record) + columns = ( + "title", + "normalized_title", + "alternate_titles", + "normalized_alternate_titles", + "year", + "tmdb_id", + "tvdb_id", + "imdb_id", + "folder", + "library_name", + ) + # `IS`: the UNIQUE key treats NULL libraries as distinct, so it never refuses a clash there. + written = self.execute_query( + f"UPDATE collections_cache SET {', '.join(c + '=?' for c in columns)} WHERE id=? AND NOT EXISTS " + "(SELECT 1 FROM collections_cache WHERE title IS ? AND library_name IS ? AND instance_name IS ? AND id<>?)", + ( + *(record.get(c) for c in columns), + id, + record.get("title"), + record.get("library_name"), + record.get("instance_name"), + id, + ), + ) + return bool(written) + + def upsert(self, record: dict, instance_name: str) -> None: + """Insert or update a collection record for a given instance.""" + record["instance_name"] = instance_name + record["asset_type"] = "collection" + self._store_json(record) + self.execute_query( """ INSERT INTO collections_cache diff --git a/backend/util/helper.py b/backend/util/helper.py index a2596e60..8a83f909 100755 --- a/backend/util/helper.py +++ b/backend/util/helper.py @@ -1,6 +1,7 @@ # util/helper.py import copy +import html import json import os import re @@ -12,6 +13,7 @@ import yaml from tqdm import tqdm +from unidecode import unidecode from backend.util.constants import ( folder_year_regex, @@ -453,17 +455,6 @@ def is_match( ) media["normalized_folder_title"] = normalize_titles(media["folder_title"]) - def json_list(value: Any) -> List[Any]: - if isinstance(value, list): - return value - if isinstance(value, str): - try: - parsed = json.loads(value) - return parsed if isinstance(parsed, list) else [] - except Exception: - return [] - return [] - asset_alt_titles = json_list(asset.get("alternate_titles")) asset_norm_alt_titles = json_list(asset.get("normalized_alternate_titles")) media_alt_titles = json_list(media.get("alternate_titles")) @@ -684,6 +675,24 @@ def classify_match(matched: bool, reason: str) -> Tuple[str, float]: return "matched", _MATCH_CONF_EXACT +def json_list(value: Any) -> List[Any]: + """``value`` as a list: itself, or the list its JSON holds; [] for anything else.""" + if isinstance(value, list): + return value + if isinstance(value, str): + try: + parsed = json.loads(value) + except (ValueError, RecursionError): + return [] + return parsed if isinstance(parsed, list) else [] + return [] + + +def parse_alternate_titles(raw: Any) -> List[str]: + """The non-empty strings in an alternate-titles column, raw or normalized (a list or its JSON).""" + return [t for t in json_list(raw) if isinstance(t, str) and t] + + def generate_title_variants(title: str) -> Dict[str, List[str]]: """Produce alternate title candidates plus their normalized forms. @@ -756,3 +765,9 @@ def generate_title_variants(title: str) -> Dict[str, List[str]]: "alternate_titles": alternate_titles, "normalized_alternate_titles": normalized_alternate_titles, } + + +def collection_title_fields(title: str) -> Dict[str, Any]: + """The title columns collections_cache stores for a Plex collection named ``title``.""" + plain = unidecode(html.unescape(title)) + return {"title": plain, "normalized_title": normalize_titles(plain), **generate_title_variants(plain)} diff --git a/backend/util/plex.py b/backend/util/plex.py index 69d7f121..6ec17770 100755 --- a/backend/util/plex.py +++ b/backend/util/plex.py @@ -1,4 +1,3 @@ -import html import itertools import os import re @@ -12,12 +11,11 @@ from plexapi.exceptions import NotFound from pathvalidate import sanitize_filename from plexapi.server import PlexServer -from unidecode import unidecode from backend.util.config import get_config_path from backend.util.helper import ( YEAR_MATCH_TOLERANCE, - generate_title_variants, + collection_title_fields, progress, ) from backend.util.normalization import normalize_titles @@ -250,9 +248,8 @@ def get_collections( leave=False, ) as inner: for collection in inner: - title_unescaped = unidecode(html.unescape(collection.title)) - normalized_title = normalize_titles(title_unescaped) - alternate_titles = generate_title_variants(title_unescaped) + titles = collection_title_fields(collection.title) + title_unescaped = titles["title"] # Use pathvalidate rather than the bare regex so the folder name # is safe across Linux + Windows + Samba shares (handles # reserved names like CON/AUX, max-length truncation, and @@ -271,15 +268,10 @@ def get_collections( imdb_id = None tvdb_id = None media_item = { - "title": title_unescaped, - "normalized_title": normalized_title, + **titles, "location": library_name, "year": year, "folder": folder, - "alternate_titles": alternate_titles["alternate_titles"], - "normalized_alternate_titles": alternate_titles[ - "normalized_alternate_titles" - ], "library_name": library_name, "asset_type": "collection", "tmdb_id": tmdb_id, diff --git a/backend/util/tmdb.py b/backend/util/tmdb.py index ce8abc95..55918b14 100644 --- a/backend/util/tmdb.py +++ b/backend/util/tmdb.py @@ -23,6 +23,7 @@ from backend.util.config import TMDBConfig from backend.util.database import ChubDB +from backend.util.helper import json_list from backend.util.normalization import normalize_titles @@ -638,18 +639,6 @@ def _norm_set(values: Iterable[Optional[str]]) -> Set[str]: return out -def _json_list(value) -> List: - if isinstance(value, list): - return value - if isinstance(value, str): - try: - parsed = json.loads(value) - return parsed if isinstance(parsed, list) else [] - except (ValueError, TypeError): - return [] - return [] - - def run_match_quality( db: ChubDB, cfg: TMDBConfig, @@ -734,7 +723,7 @@ def _verify_and_hydrate( ) row_titles = _norm_set( [row.get("title"), row.get("normalized_title")] - + _json_list(row.get("alternate_titles")) + + json_list(row.get("alternate_titles")) ) # row's normalized_title is already normalized; include raw too row_titles |= {n for n in [row.get("normalized_title")] if n} @@ -778,8 +767,8 @@ def _flag_review(db: ChubDB, media_id: int, reason: str) -> None: def _merge_akas(db: ChubDB, row: dict, tmdb_akas: List[str]) -> bool: """Union TMDB AKAs into a row's alternate_titles / normalized_alternate_titles. Returns True if anything new was added.""" - existing = _json_list(row.get("alternate_titles")) - existing_norm = set(_json_list(row.get("normalized_alternate_titles"))) + existing = json_list(row.get("alternate_titles")) + existing_norm = set(json_list(row.get("normalized_alternate_titles"))) added = False for aka in tmdb_akas: n = normalize_titles(aka) diff --git a/frontend/src/pages/media/MediaManagePage.jsx b/frontend/src/pages/media/MediaManagePage.jsx index aca0c99d..fa1eeeb9 100644 --- a/frontend/src/pages/media/MediaManagePage.jsx +++ b/frontend/src/pages/media/MediaManagePage.jsx @@ -6,7 +6,7 @@ import { mediaAPI } from '../../utils/api/media.js'; import { nestarrAPI } from '../../utils/api/nestarr.js'; import { apiCore } from '../../utils/api/core.js'; import { Modal } from '../../components/modals/Modal'; -import { Button, LoadingButton, IconButton } from '../../components/ui/index.js'; +import { Button, LoadingButton } from '../../components/ui/index.js'; import { PageHeader } from '../../components/ui/PageHeader'; import Spinner from '../../components/ui/Spinner.jsx'; import { LibraryMaintenance } from '../../components/maintenance/LibraryMaintenance.jsx'; @@ -426,10 +426,6 @@ const MediaManagePage = () => { const [resolveDeleteFiles, setResolveDeleteFiles] = useState(false); const [resolveMembers, setResolveMembers] = useState(null); const [resolveMembersLoading, setResolveMembersLoading] = useState(false); - const [showCreateCollection, setShowCreateCollection] = useState(false); - const [newCollectionName, setNewCollectionName] = useState(''); - const [editCollection, setEditCollection] = useState(null); - const [editCollectionName, setEditCollectionName] = useState(''); const [nestedIssues, setNestedIssues] = useState([]); const [lastScanTime, setLastScanTime] = useState(null); const [unmatchedEnabled, setUnmatchedEnabled] = useState(true); @@ -447,40 +443,11 @@ const MediaManagePage = () => { mediaAPI.exportMedia({ format: 'json' }) ); - const { execute: createCollection, isLoading: isCreatingCollection } = useApiMutation( - name => mediaAPI.createCollection({ name }), - { - successMessage: 'Collection created', - onSuccess: () => { - setShowCreateCollection(false); - setNewCollectionName(''); - refreshCollections(); - }, - } - ); - - const { execute: updateCollectionMutation, isLoading: isUpdatingCollection } = useApiMutation( - // execute() forwards a single arg — take one object, not (id, data). - ({ id, data }) => mediaAPI.updateCollection(id, data), - { - successMessage: 'Collection updated', - onSuccess: () => { - setEditCollection(null); - setEditCollectionName(''); - refreshCollections(); - }, - } - ); - - const { data: collectionsData, refresh: refreshCollections } = useApiData({ + const { data: collectionsData } = useApiData({ apiFunction: mediaAPI.fetchCollections, options: { showErrorToast: false }, }); - const { execute: deleteCollection } = useApiMutation(id => mediaAPI.deleteCollection(id), { - successMessage: 'Collection deleted', - }); - const { execute: deleteItem, isLoading: isDeleting } = useApiMutation( () => mediaAPI.deleteMediaItem(deleteTarget?.id, { deleteFiles }), { successMessage: 'Media item deleted' } @@ -675,24 +642,6 @@ const MediaManagePage = () => { } }; - const handleCreateCollection = async () => { - if (!newCollectionName.trim()) return; - try { - await createCollection(newCollectionName.trim()); - } catch { - toast.error('Failed to create collection'); - } - }; - - const handleDeleteCollection = async id => { - try { - await deleteCollection(id); - refreshCollections(); - } catch { - toast.error('Failed to delete collection'); - } - }; - const handleDelete = async () => { if (!deleteTarget) return; try { @@ -1108,20 +1057,16 @@ const MediaManagePage = () => { title="Collections" count={`${collections.length}`} countClass="bg-primary/15 text-primary-hover" - > - - + /> +
+ Your Plex collections as of the last sync. Create, rename or delete them in + Plex. +