Documentation
¶
Overview ¶
Package movie manages movie records in the Prism library.
Index ¶
- Variables
- type AddRequest
- type AddUnmatchedRequest
- type FileInfo
- type ListRequest
- type ListResult
- type LookupRequest
- type MetadataProvider
- type Movie
- type ParsedFilename
- type RenamePreviewItem
- type RenameSettings
- type Service
- func (s *Service) Add(ctx context.Context, req AddRequest) (Movie, error)
- func (s *Service) AddUnmatched(ctx context.Context, req AddUnmatchedRequest) (Movie, error)
- func (s *Service) AttachFile(ctx context.Context, movieID, filePath string, sizeBytes int64, ...) error
- func (s *Service) CountCutoffUnmet(ctx context.Context) (int64, error)
- func (s *Service) Delete(ctx context.Context, id string) error
- func (s *Service) DeleteFile(ctx context.Context, fileID string, deleteFromDisk bool) error
- func (s *Service) Get(ctx context.Context, id string) (Movie, error)
- func (s *Service) GetByFilePath(ctx context.Context, filePath string) (Movie, error)
- func (s *Service) GetByTMDBID(ctx context.Context, tmdbID int) (Movie, error)
- func (s *Service) GetFile(ctx context.Context, fileID string) (FileInfo, error)
- func (s *Service) HasMetadataProvider() bool
- func (s *Service) LibraryIDsByTMDBIDs(ctx context.Context, tmdbIDs []int) (map[int]string, error)
- func (s *Service) List(ctx context.Context, req ListRequest) (ListResult, error)
- func (s *Service) ListCutoffUnmet(ctx context.Context) ([]Movie, error)
- func (s *Service) ListFiles(ctx context.Context, movieID string) ([]FileInfo, error)
- func (s *Service) ListMissing(ctx context.Context, page, perPage int) ([]Movie, int64, error)
- func (s *Service) Lookup(ctx context.Context, req LookupRequest) ([]tmdb.SearchResult, error)
- func (s *Service) MatchToTMDB(ctx context.Context, movieID string, tmdbID int) (Movie, error)
- func (s *Service) RefreshMetadata(ctx context.Context, id string) (Movie, error)
- func (s *Service) RenameFiles(ctx context.Context, movieID string, settings RenameSettings, dryRun bool) ([]RenamePreviewItem, error)
- func (s *Service) SetMetadataProvider(p MetadataProvider)
- func (s *Service) SetRenameFunc(fn func(oldPath, newPath string) error)
- func (s *Service) SuggestMatches(ctx context.Context, id string) ([]tmdb.SearchResult, ParsedFilename, error)
- func (s *Service) Update(ctx context.Context, id string, req UpdateRequest) (Movie, error)
- type ServiceOption
- type UpdateRequest
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("movie not found") ErrAlreadyExists = errors.New("movie already in library") ErrTMDBNotConfigured = errors.New("TMDB API key not configured") ErrFileNotFound = errors.New("movie file not found") )
Sentinel errors returned by Service methods.
Functions ¶
This section is empty.
Types ¶
type AddRequest ¶
type AddRequest struct {
TMDBID int
LibraryID string
QualityProfileID string
Monitored bool
MinimumAvailability string // defaults to "released" when empty
PreferredEdition string // empty = no preference
}
AddRequest carries the fields needed to add a movie to the library.
type AddUnmatchedRequest ¶
AddUnmatchedRequest carries the fields needed to add a file that has no TMDB match. The resulting movie record has tmdb_id = 0, monitored = false, and uses the caller-supplied title (typically the raw filename stem).
type FileInfo ¶
type FileInfo struct {
ID string
MovieID string
Path string
SizeBytes int64
Quality plugin.Quality
Edition string
ImportedAt time.Time
IndexedAt time.Time
MediainfoJSON string // raw JSON from ffprobe scan; empty if not yet scanned
}
FileInfo is the domain representation of a movie_files record.
type ListRequest ¶
type ListRequest struct {
LibraryID string // empty = all libraries
Page int // 1-indexed; defaults to 1
PerPage int // defaults to 50, max 250
}
ListRequest carries filter and pagination options for listing movies.
type ListResult ¶
ListResult is the paginated response from List.
type LookupRequest ¶
type LookupRequest struct {
Query string
TMDBID int // if set, fetch exact movie; Query is ignored
Year int // optional year filter for query search
}
LookupRequest carries parameters for searching TMDB without adding to the library.
type MetadataProvider ¶
type MetadataProvider interface {
SearchMovies(ctx context.Context, query string, year int) ([]tmdb.SearchResult, error)
GetMovie(ctx context.Context, tmdbID int) (*tmdb.MovieDetail, error)
}
MetadataProvider fetches movie metadata from an external source.
type Movie ¶
type Movie struct {
ID string
TMDBID int
IMDBID string
Title string
OriginalTitle string
Year int
Overview string
RuntimeMinutes int
Genres []string
PosterURL string
FanartURL string
Status string
Monitored bool
LibraryID string
QualityProfileID string
MinimumAvailability string
PreferredEdition string // empty = no preference
ReleaseDate string
Path string
AddedAt time.Time
UpdatedAt time.Time
MetadataRefreshedAt *time.Time
}
Movie is the domain representation of a movie record.
type ParsedFilename ¶
ParsedFilename holds the result of parsing a release-style filename.
func ParseFilename ¶
func ParseFilename(name string) ParsedFilename
ParseFilename extracts a clean title and year from a release-style filename.
Processing pipeline:
- Strip path prefix and video file extension.
- Remove disc-ripper noise labels (Title31, Chapter02, …).
- Normalise "Pt1" / "Pt.2" → "Part 1" / "Part 2".
- Replace dots and underscores with spaces; collapse whitespace.
- All-caps detection: if ≥60 % of letters are uppercase, lowercase first.
- Find the last 4-digit year at or before the first stop token → release year.
- Everything before that position is the title; trim and title-case.
type RenamePreviewItem ¶
RenamePreviewItem describes a single proposed or completed file rename.
type RenameSettings ¶
type RenameSettings struct {
Format string
ColonReplacement renamer.ColonReplacement
}
RenameSettings carries the naming format options used by RenameFiles.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service manages movie records.
func NewService ¶
func NewService(q dbgen.Querier, meta MetadataProvider, bus *events.Bus, logger *slog.Logger, opts ...ServiceOption) *Service
NewService creates a new Service backed by the given querier, metadata provider, event bus, and logger. meta may be nil when TMDB is not configured; methods that require it return ErrTMDBNotConfigured. sqlDB may be nil in tests; when non-nil, multi-step mutations run in a database transaction.
func (*Service) Add ¶
Add fetches movie details from TMDB and inserts the movie into the library. When no metadata provider is configured (TMDB key absent), the movie is still added as a stub record with a placeholder title so that it can be monitored and grabbed immediately. Calling RefreshMetadata later will populate the full metadata once a key is configured. Returns ErrAlreadyExists if a movie with the same TMDB ID is already present.
func (*Service) AddUnmatched ¶
AddUnmatched inserts a placeholder movie record for a file that has no TMDB match. The movie is created with tmdb_id = 0 and monitored = false so it appears in the library but is not actively sought for download. Calling Update or RefreshMetadata later can associate it with a real TMDB entry.
func (*Service) AttachFile ¶
func (s *Service) AttachFile(ctx context.Context, movieID, filePath string, sizeBytes int64, quality plugin.Quality) error
AttachFile links a file on disk to an existing movie record. It sets movies.path to the full file path, creates a movie_file record, and marks the movie status as "downloaded".
Callers are responsible for ensuring the file actually exists on disk before calling this method.
func (*Service) CountCutoffUnmet ¶
CountCutoffUnmet returns the number of monitored movies whose best file quality does not meet the quality profile cutoff. Unlike ListCutoffUnmet it skips the Movie conversion and sorting since only the count is needed.
func (*Service) Delete ¶
Delete removes a movie by ID. It does not delete files from disk. Returns ErrNotFound if the movie does not exist.
func (*Service) DeleteFile ¶
DeleteFile removes the movie_files record identified by fileID. If deleteFromDisk is true, the file at the stored path is also removed from disk (errors are logged but do not fail the operation). After the DB record is removed, if no files remain for the movie, the movie's path is cleared and its status is reset to "wanted".
func (*Service) Get ¶
Get returns a single movie by its internal UUID. Returns ErrNotFound if no movie with that ID exists.
func (*Service) GetByFilePath ¶
GetByFilePath returns the movie that owns the given file path, or ErrNotFound if no movie_files row exists for that path. If the movie_files row exists but the parent movie has been deleted (orphaned row), the orphan is cleaned up and ErrNotFound is returned.
func (*Service) GetByTMDBID ¶
GetByTMDBID returns a movie by its TMDB ID. Returns ErrNotFound if no movie with that TMDB ID exists.
func (*Service) HasMetadataProvider ¶
HasMetadataProvider reports whether a metadata provider is currently wired up.
func (*Service) LibraryIDsByTMDBIDs ¶
LibraryIDsByTMDBIDs maps TMDB IDs to library movie IDs for the subset that exist in the library, in a single query. Used by Discover to flag already-added results without an N+1 of GetByTMDBID per result. TMDB IDs not in the library are simply absent from the returned map.
func (*Service) List ¶
func (s *Service) List(ctx context.Context, req ListRequest) (ListResult, error)
List returns a paginated set of movies, optionally filtered by library.
func (*Service) ListCutoffUnmet ¶
ListCutoffUnmet returns all monitored movies whose best file quality does not meet the quality profile cutoff. Filtering is done in Go to avoid complex SQL over JSON-encoded quality columns.
func (*Service) ListMissing ¶
ListMissing returns paginated monitored movies that have no associated file.
func (*Service) Lookup ¶
func (s *Service) Lookup(ctx context.Context, req LookupRequest) ([]tmdb.SearchResult, error)
Lookup searches TMDB without adding anything to the library. If req.TMDBID is non-zero, it fetches that specific movie by ID. Otherwise it performs a text search using req.Query and optional req.Year. Returns ErrTMDBNotConfigured if no metadata provider is wired up.
func (*Service) MatchToTMDB ¶
MatchToTMDB associates an existing unmatched movie record with a TMDB entry. It updates the movie's tmdb_id and then refreshes all metadata fields from TMDB in a single step. Returns ErrAlreadyExists if another movie in the library already owns the supplied tmdbID. Returns ErrTMDBNotConfigured when no metadata provider is wired up.
func (*Service) RefreshMetadata ¶
RefreshMetadata re-fetches TMDB data and updates the movie record in place. Returns ErrNotFound if the movie does not exist. Returns ErrTMDBNotConfigured if no metadata provider is wired up.
func (*Service) RenameFiles ¶
func (s *Service) RenameFiles(ctx context.Context, movieID string, settings RenameSettings, dryRun bool) ([]RenamePreviewItem, error)
RenameFiles computes the standard-format destination filename for every file belonging to movieID using the supplied settings. Files whose current name already matches the computed name are skipped.
If dryRun is true, no disk or DB operations are performed; the returned slice describes what would happen. If dryRun is false, each file is renamed on disk and the DB record is updated. Errors for individual files are logged and skipped; a combined error is returned if any file could not be renamed.
Returns ErrNotFound if the movie does not exist.
func (*Service) SetMetadataProvider ¶
func (s *Service) SetMetadataProvider(p MetadataProvider)
SetMetadataProvider replaces the metadata provider at runtime without restarting the server. Safe for concurrent use.
func (*Service) SetRenameFunc ¶
SetRenameFunc replaces the function used to rename files on disk. Intended for use in tests to inject a mock without touching the real filesystem.
func (*Service) SuggestMatches ¶
func (s *Service) SuggestMatches(ctx context.Context, id string) ([]tmdb.SearchResult, ParsedFilename, error)
SuggestMatches parses the stored filename/title of an unmatched movie, searches TMDB with the extracted title and year, and returns ranked results. Returns ErrNotFound if the movie does not exist. Returns ErrTMDBNotConfigured if no metadata provider is wired up.
type ServiceOption ¶
type ServiceOption func(*Service)
ServiceOption configures optional Service dependencies.
func WithDB ¶
func WithDB(sqlDB *sql.DB) ServiceOption
WithDB provides a *sql.DB for transaction support. When set, multi-step mutations (AttachFile, DeleteFile) run atomically.
type UpdateRequest ¶
type UpdateRequest struct {
Title string
Monitored bool
LibraryID string
QualityProfileID string
MinimumAvailability string // preserved from existing when empty
PreferredEdition *string // nil = don't change; pointer to "" clears it
}
UpdateRequest carries the mutable fields for updating a movie.