Files
silo-server/internal/catalog/folder_repo.go
91e1164090 feat(metadata): local NFO metadata and sidecar artwork (builtin chain provider) (#390)
* feat(metadata): register builtin NFO provider and broaden parsing

Phases A and B of the #216 local-NFO work, implemented test-first.

Registration & hint-first identity (Phase A):
- Migration seeds a reserved kind='builtin' silo.builtin installation
  and an 'nfo' metadata capability (default_enabled=false, priority 1
  for movie/series) with a partial unique index and documented Down.
- In-process builtin provider registry (internal/metadata/builtin.go);
  buildProviders returns the registered provider for builtin rows.
- Guard rails keep the reserved row out of every plugin surface (user
  plugin-settings, installations list, image resolvers, preload,
  auto-update, store Delete, mutation handlers -> 409); silo.builtin is
  a reserved manifest id.
- Startup sync materializes legacy content_level='' chains per level,
  then appends builtin capabilities disabled via
  AppendProviderToAllChains (idempotent); resolveEnabledProvidersBy
  priority now respects default_enabled=false.
- NFO uniqueids seed the trusted-hint machinery via IdentityHintProvider
  with per-mode conflict policy (stored IDs win on scheduled refresh,
  NFO wins on manual refresh, Identify skips NFO); ID-less candidates
  are excluded from provider-priority tie-breaks and nfo never counts
  as corroboration.
- Web chain-editor empty-state gate is now server-derived so builtin
  providers are reachable on plugin-less servers.

Parser breadth & sidecar hardening (Phase B):
- Parser covers the practical Kodi/Jellyfin field set for <movie> and
  <tvshow>: original title, tagline, runtime, dates, content rating,
  genres/studios/countries/tags, multi-source ratings with scale
  normalization, cast with roles/order, director/credits. Empty
  collections stay nil so merge early-returns apply.
- findNFO parses candidates and falls through on read/parse failure or
  root-type mismatch, so a stray movie.nfo cannot shadow tvshow.nfo;
  GetMetadata gains the same ContentType guard Search has.
- New FieldReleaseDates lock gates Year/ReleaseDate/First+LastAirDate
  in merge (Go) and the edit-metadata dialog (web), closing the gap
  where a manual refresh re-applied NFO dates over admin corrections.
- Merge-contract tests pin NFO fill semantics, genres whole-list
  first-provider-wins, and NFO edits propagating on manual refresh only.
- Docs: new admin wiki page (supported fields, merge semantics,
  naming-supplies-structure contract), index bullet, sidecar wording
  revision, v1-scope feature-detection note.

Zero behavior change while the provider is disabled (default); pinned
by CI-mode and DB-gated test suites.

Part of #216

AI-use disclosure: implemented with Claude Code (Fable 5) via
spec-driven TDD and agent-assisted implementation.

* feat(metadata): ingest local sidecar artwork and read series-depth NFO

Phases C and D of the #216 local-NFO work, implemented test-first, plus
the mixed-library use-case pins. Together these deliver the headline
case: a series absent from every remote database (e.g. a fitness
library) scans into a fully presented show -> named seasons -> titled
episodes tree from NFO files and sidecar art alone.

Local sidecar artwork through the S3 image cache (Phase C):
- The NFO provider implements ImageProvider: poster/backdrop/logo
  sidecar discovery with a fixed precedence map, symlink/non-regular
  rejection, an 8 MiB cap, and file:// source URLs at rating 0. Generic
  filenames apply only via the sidecar search paths, so a shared
  folder.jpg in a flat multi-movie directory applies to none.
- file:// becomes a live local source scheme: routed into *_source_path
  (never *_path), accepted by every image enqueue gate, attributed as
  provider "local", excluded from cached-path detection.
- The image-cache processor caches local files with lexical-on-logical
  confinement to the library roots, open-handle reads with re-checks,
  the same variant widths as remote art, and stable (7-day) failure
  classification. Keys land under
  local/{contentType}/{contentID}/{hash8}/{imageType}; superseded
  prefixes are cleaned on re-cache and item deletion.
- applyIfBetter gains a local exemption so rating-0 local art can fill
  matched items without being stickily displaced; ImageRequest carries
  additive sidecar path context.

Series depth (Phase D):
- SeasonsRequest/EpisodesRequest carry additive local path context
  (series roots, per-season directories, per-episode file paths),
  derived from naming at match time and reconstructed on refresh.
- season.nfo supplies season name/plot; NFO season numbers are advisory
  (directory-derived number wins with a Warn - naming owns structure).
  <episodedetails> gains aired/runtime/ratings; <basename>.nfo titles
  episodes and <basename>-thumb.ext supplies thumbs; filename SxxEyy
  wins over NFO numbers.
- Episode NFOs work without a season.nfo (provider seasons unioned with
  on-disk seasons); SynthesizeFallbackEpisodes always runs after persist
  so NFO-less episodes keep synthesized rows. Season/episode file:// art
  rides the Phase C pipeline unchanged.
- Migration adds season:1/episode:1 to the builtin NFO capability's
  default_priority (still default_enabled=false).

Mixed sports-library use case (tests only, no product change):
- Pins the classification contract for one library holding movie-shaped
  and show-shaped content (WWE PPV events as movies next to a "WWE
  SmackDown" show, NASCAR/F1/FIFA with partial TVDB/TMDB data): naming
  decides movie-vs-series per file before any provider runs; the NFO
  supplies metadata/identity but never flips type (ContentType guard);
  the per-root Type override is the correction path.
- NFO-driven type classification at scan time is recorded as an explicit
  deferred open question.

Part of #216

AI-use disclosure: implemented with Claude Code (Fable 5) via
spec-driven TDD and agent-assisted implementation.

* docs(metadata): document local NFO metadata architecture

Add a single as-built architecture page
(docs/architecture/local-nfo-metadata.md) for the #216 local-NFO
feature: the builtin registration model, hint-first identity semantics,
the file:// -> S3 artwork pipeline and its deployment constraint, series
depth, the mixed-library classification contract, and known limitations.

This replaces the working implementation plan, the per-phase specs, and
the narrow sidecar-artwork note, which were planning drafts and are left
untracked; admin-facing behavior remains in the wiki.

Part of #216

AI-use disclosure: planned, drafted, and consolidated with Claude Code
(Fable 5) using multi-agent exploration and adversarial review.

* fix(metadata): address PR review findings on NFO builtin provider

Fold in the valid, low-risk fixes surfaced by automated review on #390:

- imagecache: extract validateCacheRequest so CacheBytes (the local
  sidecar season/episode path) enforces the same episode-requires-season
  guard as Cache, preventing distinct episodes' art from colliding under
  one S3 key.
- image_cache_processor: close the sidecar symlink-swap window by
  rejecting the opened handle unless os.SameFile matches the Lstat'd
  file, so a leaf swapped to a symlink can't pull an out-of-root target
  into the public cache.
- plugins: guard the reserved builtin installation row in the store's
  Update, matching Delete, so its version/enabled/capabilities can never
  be rewritten even if a mutation slips past the HTTP layer.
- cmd/silo: bound SyncBuiltinProviderChains with a 30s timeout so a stuck
  DB round-trip fails fast at startup instead of hanging.
- metadata: panic instead of silently no-op'ing on an invalid
  RegisterBuiltinProvider call (init-time programmer error).
- docs: correct the media-folder-and-naming NFO paragraph to state
  season/episode NFOs and sidecar artwork are actively read.

---------

Co-authored-by: Quick104 <31828688+Quick104@users.noreply.github.com>
2026-07-16 17:55:36 -04:00

1234 lines
37 KiB
Go

package catalog
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/Silo-Server/silo-server/internal/artworkkey"
"github.com/Silo-Server/silo-server/internal/models"
)
// Retry parameters for transient serialization/deadlock failures. They are
// package vars (not consts) only so tests can shrink them; production code
// never mutates them.
var (
deadlockMaxAttempts = 5
deadlockBaseBackoff = 50 * time.Millisecond
)
const (
// orphanDeleteBatch is small because each media_items row cascades across
// ~15 child tables.
orphanDeleteBatch = 1000
// folderChildDeleteBatch covers the lighter folder-scoped media_files and
// junction deletes.
folderChildDeleteBatch = 5000
)
// retryOnDeadlock runs op, retrying when Postgres reports a deadlock (40P01) or
// serialization failure (40001), with exponential backoff. It returns
// immediately for any other error, and honors context cancellation between
// attempts.
func retryOnDeadlock(ctx context.Context, op func() error) error {
backoff := deadlockBaseBackoff
for attempt := 1; ; attempt++ {
err := op()
if err == nil {
return nil
}
var pgErr *pgconn.PgError
if attempt < deadlockMaxAttempts && errors.As(err, &pgErr) &&
(pgErr.Code == "40P01" || pgErr.Code == "40001") {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(backoff):
}
backoff *= 2
continue
}
return err
}
}
// deleteInBatches repeatedly runs deleteBatch (each a single autocommit
// statement) until a batch removes fewer than batchSize rows. Each batch is
// retried on deadlock. It returns the total number of rows deleted.
func deleteInBatches(
ctx context.Context,
batchSize int,
deleteBatch func(ctx context.Context) (int64, error),
) (int64, error) {
var total int64
for {
var affected int64
if err := retryOnDeadlock(ctx, func() error {
n, e := deleteBatch(ctx)
affected = n
return e
}); err != nil {
return total, err
}
total += affected
if affected < int64(batchSize) {
return total, nil
}
}
}
// Sentinel errors for folder repository operations.
var (
ErrFolderNotFound = errors.New("folder not found")
ErrDuplicatePath = errors.New("duplicate folder path")
)
// CreateFolderInput contains the fields required to create a new media folder.
type CreateFolderInput struct {
Paths []string
Type string
Name string
MetadataLanguage string // ISO 639-1 code; defaults to "en" if empty
ChapterThumbnailsEnabled bool
IntroDetectionEnabled bool
// TrailerKinds is the allow-list of remote video kinds fetched during
// metadata refresh; nil applies the default (all provider kinds), an
// empty slice disables remote videos.
TrailerKinds []string
}
// FolderReorderEntry carries a folder ID and its new sort position.
type FolderReorderEntry struct {
ID int `json:"id"`
Position int `json:"position"`
}
// UpdateFolderInput contains optional fields for a partial update. Only non-nil
// fields are written to the database.
type UpdateFolderInput struct {
Paths *[]string // nil = no change, non-nil = replace all paths
Type *string
Name *string
Enabled *bool
MetadataLanguage *string
AutoTranslateMetadata *bool
ChapterThumbnailsEnabled *bool
IntroDetectionEnabled *bool
TrailerKinds *[]string // nil = no change; empty slice disables remote videos
}
// FolderRepository provides CRUD operations for the media_folders table.
type FolderRepository struct {
pool *pgxpool.Pool
}
type DeleteFolderStats struct {
LibraryName string
MediaFiles int
MediaItemLinks int
OrphanedItems int
OrphanedImageDirs []string // S3 base paths (directories) for orphaned images needing cleanup
}
// NewFolderRepository creates a new FolderRepository backed by the given pool.
func NewFolderRepository(pool *pgxpool.Pool) *FolderRepository {
return &FolderRepository{pool: pool}
}
// Pool returns the underlying pgxpool.Pool. This is useful when other
// repositories need the same pool for cross-repo operations.
func (r *FolderRepository) Pool() *pgxpool.Pool {
return r.pool
}
// defaultTrailerKinds mirrors the media_folders.trailer_kinds column default:
// every provider-reported kind (deleted_scene is local-only and never comes
// from providers).
func defaultTrailerKinds() []string {
kinds := make([]string, 0, len(models.AllExtraKinds))
for _, k := range models.AllExtraKinds {
if k == models.ExtraKindDeletedScene {
continue
}
kinds = append(kinds, string(k))
}
return kinds
}
// normalizeTrailerKindsInput canonicalizes an allow-list from the API:
// trim/lowercase, drop values outside the known vocabulary (rather than
// folding them to "other", which would silently widen the allow-list), and
// dedupe while preserving order.
func normalizeTrailerKindsInput(kinds []string) []string {
normalized := make([]string, 0, len(kinds))
seen := make(map[string]bool, len(kinds))
for _, raw := range kinds {
kind := strings.ToLower(strings.TrimSpace(raw))
if kind == "" || seen[kind] {
continue
}
if string(models.NormalizeExtraKind(kind)) != kind {
// Unknown value: NormalizeExtraKind would fold it to "other".
continue
}
seen[kind] = true
normalized = append(normalized, kind)
}
return normalized
}
// folderColumns is the list of columns returned by all SELECT queries.
// Kept in one place so scanFolder stays in sync.
const folderColumns = `id, type, name, enabled, metadata_language, auto_translate_metadata, chapter_thumbnails_enabled, intro_detection_enabled, trailer_kinds, poster_path, last_scanned_at,
scan_warning_code, scan_warning_message, scan_warning_at, allow_empty_cleanup_once, sort_order`
// scanFolder scans a single row into a *models.MediaFolder.
func scanFolder(row pgx.Row) (*models.MediaFolder, error) {
var f models.MediaFolder
err := row.Scan(
&f.ID,
&f.Type,
&f.Name,
&f.Enabled,
&f.MetadataLanguage,
&f.AutoTranslateMetadata,
&f.ChapterThumbnailsEnabled,
&f.IntroDetectionEnabled,
&f.TrailerKinds,
&f.PosterPath,
&f.LastScannedAt,
&f.ScanWarningCode,
&f.ScanWarningMessage,
&f.ScanWarningAt,
&f.AllowEmptyCleanupOnce,
&f.SortOrder,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrFolderNotFound
}
return nil, fmt.Errorf("scanning folder: %w", err)
}
return &f, nil
}
// scanFolders scans multiple rows into a []*models.MediaFolder slice.
func scanFolders(rows pgx.Rows) ([]*models.MediaFolder, error) {
var folders []*models.MediaFolder
for rows.Next() {
var f models.MediaFolder
err := rows.Scan(
&f.ID,
&f.Type,
&f.Name,
&f.Enabled,
&f.MetadataLanguage,
&f.AutoTranslateMetadata,
&f.ChapterThumbnailsEnabled,
&f.IntroDetectionEnabled,
&f.TrailerKinds,
&f.PosterPath,
&f.LastScannedAt,
&f.ScanWarningCode,
&f.ScanWarningMessage,
&f.ScanWarningAt,
&f.AllowEmptyCleanupOnce,
&f.SortOrder,
)
if err != nil {
return nil, fmt.Errorf("scanning folder row: %w", err)
}
folders = append(folders, &f)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating folder rows: %w", err)
}
return folders, nil
}
// loadPaths bulk-loads paths from media_folder_paths for the given folders
// and populates each folder's Paths field.
func (r *FolderRepository) loadPaths(ctx context.Context, folders []*models.MediaFolder) error {
if len(folders) == 0 {
return nil
}
ids := make([]int, len(folders))
folderByID := make(map[int]*models.MediaFolder, len(folders))
for i, f := range folders {
ids[i] = f.ID
folderByID[f.ID] = f
f.Paths = []string{} // initialize to empty slice
}
rows, err := r.pool.Query(ctx,
`SELECT media_folder_id, path FROM media_folder_paths WHERE media_folder_id = ANY($1) ORDER BY id ASC`,
ids,
)
if err != nil {
return fmt.Errorf("loading folder paths: %w", err)
}
defer rows.Close()
for rows.Next() {
var folderID int
var path string
if err := rows.Scan(&folderID, &path); err != nil {
return fmt.Errorf("scanning folder path row: %w", err)
}
if f, ok := folderByID[folderID]; ok {
f.Paths = append(f.Paths, path)
}
}
return rows.Err()
}
// Create inserts a new media folder with its paths and returns the created row.
func (r *FolderRepository) Create(ctx context.Context, input CreateFolderInput) (*models.MediaFolder, error) {
tx, err := r.pool.Begin(ctx)
if err != nil {
return nil, fmt.Errorf("beginning create transaction: %w", err)
}
defer func() { _ = tx.Rollback(ctx) }()
metaLang := input.MetadataLanguage
if metaLang == "" {
metaLang = "en"
}
trailerKinds := input.TrailerKinds
if trailerKinds == nil {
trailerKinds = defaultTrailerKinds()
} else {
trailerKinds = normalizeTrailerKindsInput(trailerKinds)
}
query := `INSERT INTO media_folders (type, name, metadata_language, chapter_thumbnails_enabled, intro_detection_enabled, trailer_kinds, sort_order)
VALUES ($1, $2, $3, $4, $5, $6, (SELECT COALESCE(MAX(sort_order), 0) + 1 FROM media_folders))
RETURNING ` + folderColumns
row := tx.QueryRow(ctx, query,
input.Type,
input.Name,
metaLang,
input.ChapterThumbnailsEnabled,
input.IntroDetectionEnabled,
trailerKinds,
)
folder, err := scanFolder(row)
if err != nil {
return nil, fmt.Errorf("creating folder: %w", err)
}
// Insert paths into child table.
for _, p := range input.Paths {
_, err := tx.Exec(ctx,
`INSERT INTO media_folder_paths (media_folder_id, path) VALUES ($1, $2)`,
folder.ID, p,
)
if err != nil {
if isDuplicateKeyError(err) {
return nil, fmt.Errorf("%w: %s", ErrDuplicatePath, extractConstraint(err))
}
return nil, fmt.Errorf("inserting folder path %q: %w", p, err)
}
}
if err := tx.Commit(ctx); err != nil {
return nil, fmt.Errorf("committing create transaction: %w", err)
}
folder.Paths = input.Paths
return folder, nil
}
// GetByID retrieves a media folder by its numeric ID.
func (r *FolderRepository) GetByID(ctx context.Context, id int) (*models.MediaFolder, error) {
query := `SELECT ` + folderColumns + ` FROM media_folders WHERE id = $1`
folder, err := scanFolder(r.pool.QueryRow(ctx, query, id))
if err != nil {
return nil, err
}
if err := r.loadPaths(ctx, []*models.MediaFolder{folder}); err != nil {
return nil, fmt.Errorf("loading paths for folder %d: %w", id, err)
}
return folder, nil
}
// List returns all media folders ordered by ID ascending.
func (r *FolderRepository) List(ctx context.Context) ([]*models.MediaFolder, error) {
query := `SELECT ` + folderColumns + ` FROM media_folders ORDER BY sort_order ASC, id ASC`
rows, err := r.pool.Query(ctx, query)
if err != nil {
return nil, fmt.Errorf("listing folders: %w", err)
}
defer rows.Close()
folders, err := scanFolders(rows)
if err != nil {
return nil, err
}
if err := r.loadPaths(ctx, folders); err != nil {
return nil, fmt.Errorf("loading paths for list: %w", err)
}
return folders, nil
}
// GetEnabled returns all enabled media folders ordered by ID ascending.
func (r *FolderRepository) GetEnabled(ctx context.Context) ([]*models.MediaFolder, error) {
query := `SELECT ` + folderColumns + ` FROM media_folders WHERE enabled = true ORDER BY sort_order ASC, id ASC`
rows, err := r.pool.Query(ctx, query)
if err != nil {
return nil, fmt.Errorf("listing enabled folders: %w", err)
}
defer rows.Close()
folders, err := scanFolders(rows)
if err != nil {
return nil, err
}
if err := r.loadPaths(ctx, folders); err != nil {
return nil, fmt.Errorf("loading paths for enabled: %w", err)
}
return folders, nil
}
// ListByIDs returns enabled media folders matching the given IDs, ordered by ID ascending.
func (r *FolderRepository) ListByIDs(ctx context.Context, ids []int) ([]*models.MediaFolder, error) {
query := `SELECT ` + folderColumns + ` FROM media_folders WHERE id = ANY($1) AND enabled = true ORDER BY sort_order ASC, id ASC`
rows, err := r.pool.Query(ctx, query, ids)
if err != nil {
return nil, fmt.Errorf("listing folders by IDs: %w", err)
}
defer rows.Close()
folders, err := scanFolders(rows)
if err != nil {
return nil, err
}
if err := r.loadPaths(ctx, folders); err != nil {
return nil, fmt.Errorf("loading paths for IDs: %w", err)
}
return folders, nil
}
// Update modifies a folder's fields. Only non-nil fields in the input are updated.
func (r *FolderRepository) Update(ctx context.Context, id int, input UpdateFolderInput) error {
tx, err := r.pool.Begin(ctx)
if err != nil {
return fmt.Errorf("beginning update transaction: %w", err)
}
defer func() { _ = tx.Rollback(ctx) }()
setClauses := []string{}
args := []any{}
argIndex := 1
if input.Type != nil {
setClauses = append(setClauses, fmt.Sprintf("type = $%d", argIndex))
args = append(args, *input.Type)
argIndex++
}
if input.Name != nil {
setClauses = append(setClauses, fmt.Sprintf("name = $%d", argIndex))
args = append(args, *input.Name)
argIndex++
}
if input.Enabled != nil {
setClauses = append(setClauses, fmt.Sprintf("enabled = $%d", argIndex))
args = append(args, *input.Enabled)
argIndex++
}
if input.MetadataLanguage != nil {
setClauses = append(setClauses, fmt.Sprintf("metadata_language = $%d", argIndex))
args = append(args, *input.MetadataLanguage)
argIndex++
}
if input.AutoTranslateMetadata != nil {
setClauses = append(setClauses, fmt.Sprintf("auto_translate_metadata = $%d", argIndex))
args = append(args, *input.AutoTranslateMetadata)
argIndex++
}
if input.ChapterThumbnailsEnabled != nil {
setClauses = append(setClauses, fmt.Sprintf("chapter_thumbnails_enabled = $%d", argIndex))
args = append(args, *input.ChapterThumbnailsEnabled)
argIndex++
}
if input.IntroDetectionEnabled != nil {
setClauses = append(setClauses, fmt.Sprintf("intro_detection_enabled = $%d", argIndex))
args = append(args, *input.IntroDetectionEnabled)
argIndex++
}
if input.TrailerKinds != nil {
setClauses = append(setClauses, fmt.Sprintf("trailer_kinds = $%d", argIndex))
args = append(args, normalizeTrailerKindsInput(*input.TrailerKinds))
argIndex++
}
if len(setClauses) > 0 {
query := fmt.Sprintf("UPDATE media_folders SET %s WHERE id = $%d",
strings.Join(setClauses, ", "), argIndex)
args = append(args, id)
tag, execErr := tx.Exec(ctx, query, args...)
if execErr != nil {
return fmt.Errorf("updating folder: %w", execErr)
}
if tag.RowsAffected() == 0 {
return ErrFolderNotFound
}
} else if input.Paths == nil {
// Nothing to update; still verify the folder exists.
var exists bool
err := tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM media_folders WHERE id = $1)", id).Scan(&exists)
if err != nil {
return fmt.Errorf("checking folder existence: %w", err)
}
if !exists {
return ErrFolderNotFound
}
}
// Replace paths if provided.
if input.Paths != nil {
// Verify folder exists if we haven't already via SET clauses.
if len(setClauses) == 0 {
var exists bool
err := tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM media_folders WHERE id = $1)", id).Scan(&exists)
if err != nil {
return fmt.Errorf("checking folder existence: %w", err)
}
if !exists {
return ErrFolderNotFound
}
}
_, err := tx.Exec(ctx, "DELETE FROM media_folder_paths WHERE media_folder_id = $1", id)
if err != nil {
return fmt.Errorf("deleting old paths: %w", err)
}
for _, p := range *input.Paths {
_, err := tx.Exec(ctx,
`INSERT INTO media_folder_paths (media_folder_id, path) VALUES ($1, $2)`,
id, p,
)
if err != nil {
if isDuplicateKeyError(err) {
return fmt.Errorf("%w: %s", ErrDuplicatePath, extractConstraint(err))
}
return fmt.Errorf("inserting folder path %q: %w", p, err)
}
}
}
if err := tx.Commit(ctx); err != nil {
return fmt.Errorf("committing update transaction: %w", err)
}
return nil
}
// Delete removes a media folder by its ID and cleans up any media items that
// were exclusively associated with this library (orphaned items).
func (r *FolderRepository) Delete(ctx context.Context, id int) error {
_, err := r.DeleteWithStats(ctx, id, nil)
return err
}
func (r *FolderRepository) DeleteWithStats(
ctx context.Context,
id int,
progress func(current, total int, message string),
) (*DeleteFolderStats, error) {
stats := &DeleteFolderStats{}
// Phase 0: preflight reads (no long-lived transaction).
if err := r.pool.QueryRow(ctx, `SELECT name FROM media_folders WHERE id = $1`, id).Scan(&stats.LibraryName); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrFolderNotFound
}
return nil, fmt.Errorf("loading folder before delete: %w", err)
}
if err := r.pool.QueryRow(ctx, `SELECT COUNT(*) FROM media_files WHERE media_folder_id = $1`, id).Scan(&stats.MediaFiles); err != nil {
return nil, fmt.Errorf("counting media files: %w", err)
}
if err := r.pool.QueryRow(ctx, `SELECT COUNT(*) FROM media_item_libraries WHERE media_folder_id = $1`, id).Scan(&stats.MediaItemLinks); err != nil {
return nil, fmt.Errorf("counting media item links: %w", err)
}
var orphanTotal int
if err := r.pool.QueryRow(ctx, `
SELECT COUNT(*)
FROM media_item_libraries mil
WHERE mil.media_folder_id = $1
AND NOT EXISTS (
SELECT 1 FROM media_item_libraries other
WHERE other.content_id = mil.content_id
AND other.media_folder_id <> $1
)`, id).Scan(&orphanTotal); err != nil {
return nil, fmt.Errorf("counting orphaned items: %w", err)
}
// Phase 1: delete orphaned media_items in detect-then-delete batches.
// Cascade removes their junctions, provider IDs, episodes, seasons, etc.
if progress != nil {
progress(0, orphanTotal, "Deleting orphaned items")
}
rawDirs := make(map[string]struct{})
for {
ids, err := r.collectOrphanBatch(ctx, id, orphanDeleteBatch)
if err != nil {
return nil, err
}
if len(ids) == 0 {
break
}
dirs, err := collectRawImageDirs(ctx, r.pool, ids)
if err != nil {
return nil, err
}
for _, d := range dirs {
rawDirs[d] = struct{}{}
}
var deletedContentIDs []string
if err := retryOnDeadlock(ctx, func() error {
tx, e := r.pool.Begin(ctx)
if e != nil {
return e
}
defer func() { _ = tx.Rollback(ctx) }()
// Re-check the orphan invariant inside the delete. A concurrent
// scan/import may have attached one of these content IDs to another
// library after collectOrphanBatch returned; without this guard the
// cascade would delete the shared media_items row (and the
// newly-added membership), dropping the item from the other library.
rows, e := tx.Query(ctx, `
DELETE FROM media_items
WHERE content_id = ANY($1)
AND NOT EXISTS (
SELECT 1 FROM media_item_libraries other
WHERE other.content_id = media_items.content_id
AND other.media_folder_id <> $2
)
RETURNING content_id`, ids, id)
if e != nil {
return e
}
attemptDeletedIDs, e := pgx.CollectRows(rows, pgx.RowTo[string])
if e != nil {
return fmt.Errorf("collecting deleted orphaned item IDs: %w", e)
}
if err := EnqueueSearchIndexDeletes(ctx, tx, attemptDeletedIDs); err != nil {
return fmt.Errorf("enqueueing catalog search orphan deletes: %w", err)
}
if e := tx.Commit(ctx); e != nil {
return e
}
deletedContentIDs = attemptDeletedIDs
return nil
}); err != nil {
return nil, fmt.Errorf("deleting orphaned items: %w", err)
}
stats.OrphanedItems += len(deletedContentIDs)
if progress != nil {
progress(stats.OrphanedItems, orphanTotal, "Deleting orphaned items")
}
}
// Phase 2: delete this folder's media_files (folder-tied, not item-tied).
if progress != nil {
progress(orphanTotal, orphanTotal, "Deleting media files")
}
if _, err := deleteInBatches(ctx, folderChildDeleteBatch, func(ctx context.Context) (int64, error) {
tag, e := r.pool.Exec(ctx, `
DELETE FROM media_files
WHERE id IN (
SELECT id FROM media_files WHERE media_folder_id = $1 LIMIT $2
)`, id, folderChildDeleteBatch)
if e != nil {
return 0, e
}
return tag.RowsAffected(), nil
}); err != nil {
return nil, fmt.Errorf("deleting media files: %w", err)
}
// Phase 3: delete remaining folder memberships (shared items kept; only the
// membership in this folder is removed). Deleting the memberships with
// RETURNING lets us catch skeleton items that a matcher created after the
// phase-1 orphan sweep but before the delete job reached this phase.
if progress != nil {
progress(orphanTotal, orphanTotal, "Removing library memberships")
}
lateOrphans, lateImageDirs, err := r.deleteFolderMemberships(ctx, id)
if err != nil {
return nil, fmt.Errorf("deleting media item links: %w", err)
}
stats.OrphanedItems += lateOrphans
for _, d := range lateImageDirs {
rawDirs[d] = struct{}{}
}
// Filter accumulated image dirs once, now that all item orphans are gone,
// against any surviving content. Empty deleting-set means "exclude nothing".
if len(rawDirs) > 0 {
filtered, err := filterUnreferencedImageDirs(ctx, r.pool, dirSetToSlice(rawDirs), []string{})
if err != nil {
return nil, err
}
stats.OrphanedImageDirs = filtered
}
// Phase 4: delete the now-lightweight folder row. Tolerate 0 rows so a
// resumed run that already removed it still succeeds.
if err := retryOnDeadlock(ctx, func() error {
_, e := r.pool.Exec(ctx, `DELETE FROM media_folders WHERE id = $1`, id)
return e
}); err != nil {
return nil, fmt.Errorf("deleting folder: %w", err)
}
if progress != nil {
progress(orphanTotal, orphanTotal, "Library deletion completed")
}
return stats, nil
}
// collectOrphanBatch returns up to limit content IDs whose only library
// membership is the given folder.
func (r *FolderRepository) collectOrphanBatch(ctx context.Context, folderID, limit int) ([]string, error) {
rows, err := r.pool.Query(ctx, `
SELECT mil.content_id
FROM media_item_libraries mil
WHERE mil.media_folder_id = $1
AND NOT EXISTS (
SELECT 1 FROM media_item_libraries other
WHERE other.content_id = mil.content_id
AND other.media_folder_id <> $1
)
LIMIT $2`, folderID, limit)
if err != nil {
return nil, fmt.Errorf("finding orphaned items: %w", err)
}
defer rows.Close()
var ids []string
for rows.Next() {
var contentID string
if err := rows.Scan(&contentID); err != nil {
return nil, fmt.Errorf("scanning orphan content_id: %w", err)
}
ids = append(ids, contentID)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating orphan rows: %w", err)
}
return ids, nil
}
// deleteFolderMemberships removes this folder's remaining item memberships and
// deletes any content that becomes orphaned as a result. This is intentionally
// separate from the phase-1 orphan sweep because in-flight matchers can create
// new skeleton memberships after phase 1 has already drained.
func (r *FolderRepository) deleteFolderMemberships(ctx context.Context, folderID int) (int, []string, error) {
var orphaned int
var imageDirs []string
for {
var contentIDs []string
if err := retryOnDeadlock(ctx, func() error {
ids, err := r.deleteFolderMembershipBatch(ctx, folderID, folderChildDeleteBatch)
if err != nil {
return err
}
contentIDs = ids
return nil
}); err != nil {
return orphaned, imageDirs, err
}
if len(contentIDs) > 0 {
deleted, dirs, err := r.deleteOrphanedItemsByContentID(ctx, contentIDs)
if err != nil {
return orphaned, imageDirs, err
}
orphaned += deleted
imageDirs = append(imageDirs, dirs...)
}
if len(contentIDs) < folderChildDeleteBatch {
return orphaned, imageDirs, nil
}
}
}
func (r *FolderRepository) deleteFolderMembershipBatch(ctx context.Context, folderID, limit int) ([]string, error) {
rows, err := r.pool.Query(ctx, `
DELETE FROM media_item_libraries
WHERE ctid IN (
SELECT ctid FROM media_item_libraries WHERE media_folder_id = $1 LIMIT $2
)
RETURNING content_id`, folderID, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var contentIDs []string
for rows.Next() {
var contentID string
if err := rows.Scan(&contentID); err != nil {
return nil, fmt.Errorf("scanning deleted membership content_id: %w", err)
}
contentIDs = append(contentIDs, contentID)
}
if err := rows.Err(); err != nil {
return nil, err
}
return contentIDs, nil
}
const collectOrphanedProvisionalIDsByContentIDSQL = `
SELECT mi.content_id
FROM public.media_items mi
WHERE mi.content_id = ANY($1)
AND ` + orphanedProvisionalMediaItemConditions
const deleteOrphanedProvisionalIDsByContentIDSQL = `
DELETE FROM public.media_items mi
WHERE mi.content_id = ANY($1)
AND ` + orphanedProvisionalMediaItemConditions + `
RETURNING mi.content_id`
func (r *FolderRepository) deleteOrphanedItemsByContentID(ctx context.Context, contentIDs []string) (int, []string, error) {
if len(contentIDs) == 0 {
return 0, nil, nil
}
orphanIDs, err := r.collectOrphanedProvisionalIDsByContentID(ctx, contentIDs)
if err != nil {
return 0, nil, err
}
if len(orphanIDs) == 0 {
return 0, nil, nil
}
rawImageDirs, err := collectRawImageDirs(ctx, r.pool, orphanIDs)
if err != nil {
return 0, nil, err
}
var deletedIDs []string
var imageDirs []string
if err := retryOnDeadlock(ctx, func() error {
tx, err := r.pool.Begin(ctx)
if err != nil {
return err
}
defer func() { _ = tx.Rollback(ctx) }()
rows, err := tx.Query(ctx, deleteOrphanedProvisionalIDsByContentIDSQL, orphanIDs)
if err != nil {
return err
}
attemptDeletedIDs, err := pgx.CollectRows(rows, pgx.RowTo[string])
if err != nil {
return fmt.Errorf("collecting deleted late orphan IDs: %w", err)
}
var attemptImageDirs []string
if len(attemptDeletedIDs) > 0 {
attemptImageDirs, err = filterUnreferencedImageDirs(ctx, tx, rawImageDirs, attemptDeletedIDs)
if err != nil {
return err
}
}
if err := EnqueueSearchIndexDeletes(ctx, tx, attemptDeletedIDs); err != nil {
return fmt.Errorf("enqueueing catalog search late orphan deletes: %w", err)
}
if err := tx.Commit(ctx); err != nil {
return err
}
deletedIDs = attemptDeletedIDs
imageDirs = attemptImageDirs
return nil
}); err != nil {
return 0, nil, err
}
return len(deletedIDs), imageDirs, nil
}
func (r *FolderRepository) collectOrphanedProvisionalIDsByContentID(ctx context.Context, contentIDs []string) ([]string, error) {
rows, err := r.pool.Query(ctx, collectOrphanedProvisionalIDsByContentIDSQL, contentIDs)
if err != nil {
return nil, err
}
defer rows.Close()
var orphanIDs []string
for rows.Next() {
var contentID string
if err := rows.Scan(&contentID); err != nil {
return nil, fmt.Errorf("scanning orphan content_id: %w", err)
}
orphanIDs = append(orphanIDs, contentID)
}
if err := rows.Err(); err != nil {
return nil, err
}
return orphanIDs, nil
}
// dirSetToSlice returns the keys of set as a slice (nil if empty).
func dirSetToSlice(set map[string]struct{}) []string {
if len(set) == 0 {
return nil
}
dirs := make([]string, 0, len(set))
for d := range set {
dirs = append(dirs, d)
}
return dirs
}
// imageDeletePrefix returns the S3 prefix to sweep when deleting the content
// that owns the cached image at path. Local artwork keys carry a content-hash
// segment (local/{contentType}/{contentID}/{hash8}/{imageType}/...), so local
// paths trim to local/{contentType}/{contentID}/ — deleting every stale hash
// prefix, not just the live one. Other keys keep the per-image-type dir.
func imageDeletePrefix(path string) string {
if !strings.HasPrefix(path, "local/") {
return pathDir(path)
}
segments := strings.SplitN(path, "/", 4)
if len(segments) < 4 {
return pathDir(path)
}
return segments[0] + "/" + segments[1] + "/" + segments[2] + "/"
}
// pathDir extracts the directory portion of an S3 image path.
// e.g. "tmdb/movies/550/poster/original.webp" → "tmdb/movies/550/poster/"
func pathDir(path string) string {
return artworkkey.Directory(path)
}
// rowQuerier is satisfied by both *pgxpool.Pool and pgx.Tx, letting read
// helpers run inside or outside an explicit transaction.
type rowQuerier interface {
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
}
// collectOrphanIDs returns content IDs from the given set that have no
// remaining library memberships. Must be called within a transaction.
func collectOrphanIDs(ctx context.Context, tx pgx.Tx, contentIDs []string) ([]string, error) {
rows, err := tx.Query(ctx, `
SELECT cid FROM unnest($1::text[]) AS cid
WHERE NOT EXISTS (
SELECT 1 FROM media_item_libraries mil WHERE mil.content_id = cid
)
`, contentIDs)
if err != nil {
return nil, fmt.Errorf("finding orphaned items: %w", err)
}
defer rows.Close()
var ids []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return nil, fmt.Errorf("scanning orphan id: %w", err)
}
ids = append(ids, id)
}
return ids, rows.Err()
}
// collectImageDirs returns S3 directory prefixes for images belonging to the
// given content IDs that are not still referenced by other surviving content.
func collectImageDirs(ctx context.Context, q rowQuerier, contentIDs []string) ([]string, error) {
dirs, err := collectRawImageDirs(ctx, q, contentIDs)
if err != nil {
return nil, err
}
return filterUnreferencedImageDirs(ctx, q, dirs, contentIDs)
}
// collectRawImageDirs returns the deduped S3 directory prefixes referenced by
// the given content IDs (items, their seasons, and their episodes), without
// filtering out dirs still used by other content.
func collectRawImageDirs(ctx context.Context, q rowQuerier, contentIDs []string) ([]string, error) {
imgRows, err := q.Query(ctx, `
SELECT poster_path, backdrop_path, logo_path FROM media_items WHERE content_id = ANY($1)
UNION ALL
SELECT poster_path, '', '' FROM seasons WHERE series_id = ANY($1)
UNION ALL
SELECT still_path, '', '' FROM episodes WHERE series_id = ANY($1)
`, contentIDs)
if err != nil {
return nil, fmt.Errorf("collecting image paths: %w", err)
}
defer imgRows.Close()
dirSet := make(map[string]struct{})
for imgRows.Next() {
var p1, p2, p3 string
if err := imgRows.Scan(&p1, &p2, &p3); err != nil {
return nil, fmt.Errorf("scanning image path: %w", err)
}
for _, p := range []string{p1, p2, p3} {
if p != "" && !strings.Contains(p, "://") {
if dir := imageDeletePrefix(p); dir != "" {
dirSet[dir] = struct{}{}
}
}
}
}
if err := imgRows.Err(); err != nil {
return nil, fmt.Errorf("iterating image paths: %w", err)
}
dirs := make([]string, 0, len(dirSet))
for dir := range dirSet {
dirs = append(dirs, dir)
}
return dirs, nil
}
func filterUnreferencedImageDirs(ctx context.Context, q rowQuerier, dirs, deletingContentIDs []string) ([]string, error) {
if len(dirs) == 0 {
return nil, nil
}
rows, err := q.Query(ctx, `
SELECT candidate.dir
FROM unnest($1::text[]) AS candidate(dir)
WHERE NOT EXISTS (
SELECT 1
FROM media_items mi
WHERE NOT (mi.content_id = ANY($2::text[]))
AND (
mi.poster_path LIKE candidate.dir || '%'
OR mi.backdrop_path LIKE candidate.dir || '%'
OR mi.logo_path LIKE candidate.dir || '%'
)
)
AND NOT EXISTS (
SELECT 1
FROM seasons s
WHERE NOT (s.series_id = ANY($2::text[]))
AND s.poster_path LIKE candidate.dir || '%'
)
AND NOT EXISTS (
SELECT 1
FROM episodes e
WHERE NOT (e.series_id = ANY($2::text[]))
AND e.still_path LIKE candidate.dir || '%'
)
ORDER BY candidate.dir
`, dirs, deletingContentIDs)
if err != nil {
return nil, fmt.Errorf("filtering referenced image dirs: %w", err)
}
defer rows.Close()
var unreferenced []string
for rows.Next() {
var dir string
if err := rows.Scan(&dir); err != nil {
return nil, fmt.Errorf("scanning unreferenced image dir: %w", err)
}
unreferenced = append(unreferenced, dir)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating unreferenced image dirs: %w", err)
}
return unreferenced, nil
}
// UpdateLastScanned sets the last_scanned_at timestamp for the given folder.
// LibraryRootsForContent returns the media folder root paths the given
// content belongs to, resolved through media_item_libraries. The metadata
// image-cache processor uses this to confine local file:// artwork sources
// (metadata.LibraryRootResolver).
func (r *FolderRepository) LibraryRootsForContent(ctx context.Context, contentID string) ([]string, error) {
rows, err := r.pool.Query(ctx, `
SELECT DISTINCT mfp.path
FROM media_folder_paths mfp
JOIN media_item_libraries mil ON mil.media_folder_id = mfp.media_folder_id
WHERE mil.content_id = $1
`, contentID)
if err != nil {
return nil, fmt.Errorf("querying library roots for content %q: %w", contentID, err)
}
defer rows.Close()
var paths []string
for rows.Next() {
var path string
if err := rows.Scan(&path); err != nil {
return nil, fmt.Errorf("scanning library root path: %w", err)
}
paths = append(paths, path)
}
return paths, rows.Err()
}
func (r *FolderRepository) UpdateLastScanned(ctx context.Context, id int, scannedAt time.Time) error {
tag, err := r.pool.Exec(ctx,
"UPDATE media_folders SET last_scanned_at = $1 WHERE id = $2",
scannedAt, id,
)
if err != nil {
return fmt.Errorf("updating last_scanned_at: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrFolderNotFound
}
return nil
}
// SetScanWarning records a non-fatal scan warning on the folder.
func (r *FolderRepository) SetScanWarning(ctx context.Context, id int, code, message string, warnedAt time.Time) error {
tag, err := r.pool.Exec(ctx,
`UPDATE media_folders
SET scan_warning_code = $1,
scan_warning_message = $2,
scan_warning_at = $3
WHERE id = $4`,
code, message, warnedAt, id,
)
if err != nil {
return fmt.Errorf("updating scan warning: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrFolderNotFound
}
return nil
}
// ClearScanWarning clears any scan warning state and resets one-shot cleanup confirmation.
func (r *FolderRepository) ClearScanWarning(ctx context.Context, id int) error {
tag, err := r.pool.Exec(ctx,
`UPDATE media_folders
SET scan_warning_code = NULL,
scan_warning_message = NULL,
scan_warning_at = NULL,
allow_empty_cleanup_once = false
WHERE id = $1`,
id,
)
if err != nil {
return fmt.Errorf("clearing scan warning: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrFolderNotFound
}
return nil
}
// AllowEmptyCleanupOnce arms a single destructive empty-root cleanup pass.
func (r *FolderRepository) AllowEmptyCleanupOnce(ctx context.Context, id int) error {
tag, err := r.pool.Exec(ctx,
`UPDATE media_folders
SET allow_empty_cleanup_once = true
WHERE id = $1`,
id,
)
if err != nil {
return fmt.Errorf("arming empty cleanup confirmation: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrFolderNotFound
}
return nil
}
// ConsumeEmptyCleanupAllowance returns whether a destructive empty-root cleanup
// has been approved and clears the one-shot flag.
func (r *FolderRepository) ConsumeEmptyCleanupAllowance(ctx context.Context, id int) (bool, error) {
var allowed bool
err := r.pool.QueryRow(ctx,
`WITH current_state AS (
SELECT allow_empty_cleanup_once
FROM media_folders
WHERE id = $1
)
UPDATE media_folders
SET allow_empty_cleanup_once = false
WHERE id = $1
RETURNING (SELECT allow_empty_cleanup_once FROM current_state)`,
id,
).Scan(&allowed)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return false, ErrFolderNotFound
}
return false, fmt.Errorf("consuming empty cleanup confirmation: %w", err)
}
return allowed, nil
}
// SetPosterPath stores the S3 key of the library poster image.
func (r *FolderRepository) SetPosterPath(ctx context.Context, id int, posterPath string) error {
tag, err := r.pool.Exec(ctx,
`UPDATE media_folders SET poster_path = $1 WHERE id = $2`,
posterPath, id,
)
if err != nil {
return fmt.Errorf("setting poster path: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrFolderNotFound
}
return nil
}
// ClearPosterPath removes the library poster path.
func (r *FolderRepository) ClearPosterPath(ctx context.Context, id int) error {
return r.SetPosterPath(ctx, id, "")
}
// Reorder batch-updates sort_order for the given folders in a transaction.
func (r *FolderRepository) Reorder(ctx context.Context, entries []FolderReorderEntry) error {
tx, err := r.pool.Begin(ctx)
if err != nil {
return fmt.Errorf("beginning reorder transaction: %w", err)
}
defer func() { _ = tx.Rollback(ctx) }()
for _, e := range entries {
if _, err := tx.Exec(ctx,
"UPDATE media_folders SET sort_order = $1 WHERE id = $2",
e.Position, e.ID,
); err != nil {
return fmt.Errorf("reordering folder %d: %w", e.ID, err)
}
}
return tx.Commit(ctx)
}
// isDuplicateKeyError checks if the error is a PostgreSQL unique_violation (code 23505).
func isDuplicateKeyError(err error) bool {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return pgErr.Code == "23505"
}
return false
}
// extractConstraint extracts the constraint name from a PgError for diagnostic messages.
func extractConstraint(err error) string {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return pgErr.ConstraintName
}
return "unknown"
}