Files
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

487 lines
14 KiB
Go

package plugins
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
)
var ErrInstallationNotFound = errors.New("plugin installation not found")
var ErrArchiveNotFound = errors.New("plugin archive not found")
var ErrInstallationDisabled = errors.New("plugin installation is disabled")
// ErrBuiltinInstallationImmutable is returned when a caller tries to mutate
// the reserved builtin-host installation row (delete, update, config, ...).
var ErrBuiltinInstallationImmutable = errors.New("builtin installation cannot be modified")
// Installation kinds. A 'builtin' installation is the reserved row that
// anchors built-in host provider capabilities (silo.builtin); it has no
// archive, manifest, or binary and must never be launched, updated, or
// deleted. Generic reads must NOT filter builtins — the metadata chain's
// enabled-check depends on reading them.
const (
KindPlugin = "plugin"
KindBuiltin = "builtin"
)
type Installation struct {
ID int
RepositoryID *int
PluginID string
Version string
InstallPath string
Enabled bool
Kind string `json:"kind"`
UpdatePolicy string `json:"update_policy"`
AvailableVersion *string `json:"available_version,omitempty"`
CreatedAt time.Time
UpdatedAt time.Time
}
// IsBuiltin reports whether this is the reserved builtin-host installation.
func (i *Installation) IsBuiltin() bool {
return i != nil && i.Kind == KindBuiltin
}
type Capability struct {
InstallationID int
Type string
ID string
Metadata map[string]any
CreatedAt time.Time
UpdatedAt time.Time
}
type InstallationArchive struct {
InstallationID int
ManifestJSON []byte
Checksum string
Bytes []byte
CreatedAt time.Time
UpdatedAt time.Time
}
type CreateInstallationInput struct {
RepositoryID int
PluginID string
Version string
InstallPath string
Enabled bool
UpdatePolicy string
Capabilities []Capability
}
type UpdateInstallationInput struct {
Version *string
InstallPath *string
Enabled *bool
UpdatePolicy *string
AvailableVersion *string
Capabilities []Capability
}
type InstallationStore struct {
pool *pgxpool.Pool
}
func NewInstallationStore(pool *pgxpool.Pool) *InstallationStore {
return &InstallationStore{pool: pool}
}
const installationColumns = `id, repository_id, plugin_id, version, install_path, enabled, kind, update_policy, available_version, created_at, updated_at`
const capabilityColumns = `plugin_installation_id, capability_type, capability_id, metadata, created_at, updated_at`
const archiveColumns = `plugin_installation_id, manifest_json, checksum, archive_bytes, created_at, updated_at`
func scanInstallation(row pgx.Row) (*Installation, error) {
var installation Installation
var repositoryID *int
if err := row.Scan(
&installation.ID,
&repositoryID,
&installation.PluginID,
&installation.Version,
&installation.InstallPath,
&installation.Enabled,
&installation.Kind,
&installation.UpdatePolicy,
&installation.AvailableVersion,
&installation.CreatedAt,
&installation.UpdatedAt,
); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrInstallationNotFound
}
return nil, fmt.Errorf("scanning plugin installation: %w", err)
}
installation.RepositoryID = repositoryID
return &installation, nil
}
func scanCapabilities(rows pgx.Rows) ([]*Capability, error) {
var capabilities []*Capability
for rows.Next() {
var capability Capability
var metadataJSON []byte
if err := rows.Scan(
&capability.InstallationID,
&capability.Type,
&capability.ID,
&metadataJSON,
&capability.CreatedAt,
&capability.UpdatedAt,
); err != nil {
return nil, fmt.Errorf("scanning plugin capability: %w", err)
}
capability.Metadata = map[string]any{}
if len(metadataJSON) > 0 {
if err := json.Unmarshal(metadataJSON, &capability.Metadata); err != nil {
return nil, fmt.Errorf("unmarshaling plugin capability metadata: %w", err)
}
}
capabilities = append(capabilities, &capability)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating plugin capabilities: %w", err)
}
return capabilities, nil
}
func scanArchive(row pgx.Row) (*InstallationArchive, error) {
var archive InstallationArchive
if err := row.Scan(
&archive.InstallationID,
&archive.ManifestJSON,
&archive.Checksum,
&archive.Bytes,
&archive.CreatedAt,
&archive.UpdatedAt,
); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrArchiveNotFound
}
return nil, fmt.Errorf("scanning plugin archive: %w", err)
}
return &archive, nil
}
func (s *InstallationStore) Create(ctx context.Context, input CreateInstallationInput) (*Installation, error) {
tx, err := s.pool.Begin(ctx)
if err != nil {
return nil, fmt.Errorf("begin create installation transaction: %w", err)
}
defer tx.Rollback(ctx)
updatePolicy := input.UpdatePolicy
if updatePolicy == "" {
updatePolicy = "auto"
}
query := `INSERT INTO plugin_installations (repository_id, plugin_id, version, install_path, enabled, update_policy)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING ` + installationColumns
installation, err := scanInstallation(tx.QueryRow(
ctx,
query,
nilIfZero(input.RepositoryID),
input.PluginID,
input.Version,
input.InstallPath,
input.Enabled,
updatePolicy,
))
if err != nil {
return nil, fmt.Errorf("creating plugin installation: %w", err)
}
if err := s.replaceCapabilities(ctx, tx, installation.ID, input.Capabilities); err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, fmt.Errorf("commit create installation transaction: %w", err)
}
return installation, nil
}
func (s *InstallationStore) GetByID(ctx context.Context, id int) (*Installation, error) {
query := `SELECT ` + installationColumns + ` FROM plugin_installations WHERE id = $1`
return scanInstallation(s.pool.QueryRow(ctx, query, id))
}
func (s *InstallationStore) List(ctx context.Context) ([]*Installation, error) {
rows, err := s.pool.Query(ctx, `SELECT `+installationColumns+` FROM plugin_installations ORDER BY id ASC`)
if err != nil {
return nil, fmt.Errorf("listing plugin installations: %w", err)
}
defer rows.Close()
var installations []*Installation
for rows.Next() {
installation, err := scanInstallation(rows)
if err != nil {
return nil, err
}
installations = append(installations, installation)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating plugin installations: %w", err)
}
return installations, nil
}
func (s *InstallationStore) ListEnabled(ctx context.Context) ([]*Installation, error) {
rows, err := s.pool.Query(ctx, `SELECT `+installationColumns+` FROM plugin_installations WHERE enabled = true ORDER BY id ASC`)
if err != nil {
return nil, fmt.Errorf("listing enabled plugin installations: %w", err)
}
defer rows.Close()
var installations []*Installation
for rows.Next() {
installation, err := scanInstallation(rows)
if err != nil {
return nil, err
}
installations = append(installations, installation)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating enabled plugin installations: %w", err)
}
return installations, nil
}
func (s *InstallationStore) ListByPluginID(ctx context.Context, pluginID string) ([]*Installation, error) {
rows, err := s.pool.Query(
ctx,
`SELECT `+installationColumns+` FROM plugin_installations WHERE plugin_id = $1 ORDER BY id ASC`,
pluginID,
)
if err != nil {
return nil, fmt.Errorf("listing plugin installations for plugin %q: %w", pluginID, err)
}
defer rows.Close()
var installations []*Installation
for rows.Next() {
installation, err := scanInstallation(rows)
if err != nil {
return nil, err
}
installations = append(installations, installation)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating plugin installations for plugin %q: %w", pluginID, err)
}
return installations, nil
}
func (s *InstallationStore) Update(ctx context.Context, id int, input UpdateInstallationInput) error {
// Guard at the store, not only the HTTP handler: the reserved builtin row
// carries no archive/binary and must never have its version, path, enabled
// flag, or capabilities rewritten, or its chain participation breaks.
installation, err := s.GetByID(ctx, id)
if err != nil {
return err
}
if installation.IsBuiltin() {
return ErrBuiltinInstallationImmutable
}
tx, err := s.pool.Begin(ctx)
if err != nil {
return fmt.Errorf("begin update installation transaction: %w", err)
}
defer tx.Rollback(ctx)
var setClauses []string
var args []any
argIndex := 1
if input.Version != nil {
setClauses = append(setClauses, fmt.Sprintf("version = $%d", argIndex))
args = append(args, *input.Version)
argIndex++
}
if input.InstallPath != nil {
setClauses = append(setClauses, fmt.Sprintf("install_path = $%d", argIndex))
args = append(args, *input.InstallPath)
argIndex++
}
if input.Enabled != nil {
setClauses = append(setClauses, fmt.Sprintf("enabled = $%d", argIndex))
args = append(args, *input.Enabled)
argIndex++
}
if input.UpdatePolicy != nil {
setClauses = append(setClauses, fmt.Sprintf("update_policy = $%d", argIndex))
args = append(args, *input.UpdatePolicy)
argIndex++
}
if input.AvailableVersion != nil {
setClauses = append(setClauses, fmt.Sprintf("available_version = $%d", argIndex))
args = append(args, *input.AvailableVersion)
argIndex++
}
if len(setClauses) > 0 {
setClauses = append(setClauses, "updated_at = NOW()")
args = append(args, id)
query := fmt.Sprintf(
"UPDATE plugin_installations SET %s WHERE id = $%d",
strings.Join(setClauses, ", "),
argIndex,
)
tag, err := tx.Exec(ctx, query, args...)
if err != nil {
return fmt.Errorf("updating plugin installation: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrInstallationNotFound
}
}
if input.Capabilities != nil {
if err := s.replaceCapabilities(ctx, tx, id, input.Capabilities); err != nil {
return err
}
}
if err := tx.Commit(ctx); err != nil {
return fmt.Errorf("commit update installation transaction: %w", err)
}
return nil
}
func (s *InstallationStore) ListCapabilities(ctx context.Context, installationID int) ([]*Capability, error) {
query := `SELECT ` + capabilityColumns + ` FROM plugin_capabilities
WHERE plugin_installation_id = $1
ORDER BY capability_type ASC, capability_id ASC`
rows, err := s.pool.Query(ctx, query, installationID)
if err != nil {
return nil, fmt.Errorf("listing plugin capabilities: %w", err)
}
defer rows.Close()
return scanCapabilities(rows)
}
func (s *InstallationStore) SaveArchive(
ctx context.Context,
installationID int,
manifestJSON []byte,
checksum string,
archiveBytes []byte,
) error {
if len(manifestJSON) == 0 {
return fmt.Errorf("saving plugin archive: manifest JSON is required")
}
if checksum == "" {
return fmt.Errorf("saving plugin archive: checksum is required")
}
if len(archiveBytes) == 0 {
return fmt.Errorf("saving plugin archive: archive bytes are required")
}
query := `INSERT INTO plugin_archives (plugin_installation_id, manifest_json, checksum, archive_bytes)
VALUES ($1, $2, $3, $4)
ON CONFLICT (plugin_installation_id) DO UPDATE SET
manifest_json = EXCLUDED.manifest_json,
checksum = EXCLUDED.checksum,
archive_bytes = EXCLUDED.archive_bytes,
updated_at = NOW()`
tag, err := s.pool.Exec(ctx, query, installationID, manifestJSON, checksum, archiveBytes)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "23503" {
return ErrInstallationNotFound
}
return fmt.Errorf("saving plugin archive: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrInstallationNotFound
}
return nil
}
func (s *InstallationStore) GetArchive(ctx context.Context, installationID int) (*InstallationArchive, error) {
query := `SELECT ` + archiveColumns + ` FROM plugin_archives WHERE plugin_installation_id = $1`
return scanArchive(s.pool.QueryRow(ctx, query, installationID))
}
func (s *InstallationStore) Delete(ctx context.Context, id int) error {
installation, err := s.GetByID(ctx, id)
if err != nil {
return err
}
// Guard at the store, not only the HTTP handler: deleting the reserved
// builtin row would cascade through every builtin chain row and RemoveAll
// the (sentinel) install dir.
if installation.IsBuiltin() {
return ErrBuiltinInstallationImmutable
}
tag, err := s.pool.Exec(ctx, `DELETE FROM plugin_installations WHERE id = $1`, id)
if err != nil {
return fmt.Errorf("deleting plugin installation: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrInstallationNotFound
}
installDir := filepath.Dir(installation.InstallPath)
if err := os.RemoveAll(installDir); err != nil {
return fmt.Errorf("removing plugin installation files: %w", err)
}
return nil
}
func (s *InstallationStore) replaceCapabilities(
ctx context.Context,
tx pgx.Tx,
installationID int,
capabilities []Capability,
) error {
if _, err := tx.Exec(ctx, "DELETE FROM plugin_capabilities WHERE plugin_installation_id = $1", installationID); err != nil {
return fmt.Errorf("deleting plugin capabilities: %w", err)
}
for _, capability := range capabilities {
metadata := capability.Metadata
if metadata == nil {
metadata = map[string]any{}
}
metadataJSON, err := json.Marshal(metadata)
if err != nil {
return fmt.Errorf("marshaling plugin capability metadata: %w", err)
}
_, err = tx.Exec(
ctx,
`INSERT INTO plugin_capabilities
(plugin_installation_id, capability_type, capability_id, metadata)
VALUES ($1, $2, $3, $4)`,
installationID,
capability.Type,
capability.ID,
metadataJSON,
)
if err != nil {
return fmt.Errorf("inserting plugin capability: %w", err)
}
}
return nil
}
func nilIfZero(value int) *int {
if value == 0 {
return nil
}
return &value
}