Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions internal/catalog/item_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,49 @@ func NewItemRepository(pool *pgxpool.Pool) *ItemRepository {
return &ItemRepository{pool: pool}
}

// GetPoster returns the current poster path and thumbhash for a media item.
// Missing or NULL values are returned as empty strings.
func (r *ItemRepository) GetPoster(ctx context.Context, contentID string) (posterPath string, posterThumbhash string, err error) {
if r == nil || r.pool == nil {
return "", "", ErrItemNotFound
}
if err := r.pool.QueryRow(ctx, `
SELECT COALESCE(poster_path, ''), COALESCE(poster_thumbhash, '')
FROM media_items
WHERE content_id = $1
`, contentID).Scan(&posterPath, &posterThumbhash); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return "", "", ErrItemNotFound
}
return "", "", fmt.Errorf("getting media item poster: %w", err)
}
return posterPath, posterThumbhash, nil
}

// SetLocalPoster records a locally extracted poster without ever overwriting
// provider or manually applied artwork: the row is updated only when the item
// has no poster yet or its current poster lives under localPrefix. The
// condition is evaluated atomically in SQL so concurrent metadata writers
// (e.g. the ebook enrichment sweep) cannot be clobbered between a read and a
// write. Returns true when a row was updated.
func (r *ItemRepository) SetLocalPoster(ctx context.Context, contentID, posterPath, thumbhash, localPrefix string) (bool, error) {
if r == nil || r.pool == nil {
return false, ErrItemNotFound
}
tag, err := r.pool.Exec(ctx, `
UPDATE media_items
SET poster_path = $2,
poster_thumbhash = NULLIF($3, ''),
updated_at = NOW()
WHERE content_id = $1
AND (poster_path IS NULL OR poster_path = '' OR poster_path LIKE $4 || '%')
`, contentID, posterPath, thumbhash, localPrefix)
if err != nil {
return false, fmt.Errorf("setting local poster: %w", err)
}
return tag.RowsAffected() > 0, nil
}

// overviewMatchFloor is the minimum ts_rank_cd score an overview-only match
// must achieve to be returned when no title FTS match exists in the candidate
// set. The overview tsvector is built without setweight(), so PostgreSQL's
Expand All @@ -50,13 +93,22 @@ const itemColumns = `content_id, type, title, sort_title, default_metadata_langu
content_rating, runtime, overview, tagline,
rating_imdb, rating_tmdb, rating_rt_critic, rating_rt_audience,
imdb_id, tmdb_id, tvdb_id,
poster_path, poster_thumbhash, backdrop_path, backdrop_thumbhash, logo_path,
metadata_s3_path, metadata_etag, season_count,
COALESCE(poster_path, ''), COALESCE(poster_thumbhash, ''), COALESCE(backdrop_path, ''), COALESCE(backdrop_thumbhash, ''), COALESCE(logo_path, ''),
COALESCE(metadata_s3_path, ''), COALESCE(metadata_etag, ''), season_count,
studios, networks, countries, keywords, original_language, release_date::text, first_air_date, last_air_date, air_time, air_timezone,
show_status,
matched_at, last_refreshed, refresh_failures,
episode_metadata_incomplete, episode_metadata_last_checked_at, locked_fields, status, created_at, updated_at`

func qualifiedNullableStringColumn(alias, col string) string {
switch col {
case "poster_path", "poster_thumbhash", "backdrop_path", "backdrop_thumbhash", "logo_path", "metadata_s3_path", "metadata_etag":
return fmt.Sprintf("COALESCE(%s.%s, '')", alias, col)
default:
return alias + "." + col
}
}

func qualifiedItemColumns(alias string) string {
cols := []string{
"content_id", "type", "title", "sort_title", "default_metadata_language", "original_title", "year", "genres",
Expand All @@ -72,7 +124,7 @@ func qualifiedItemColumns(alias string) string {
}
prefixed := make([]string, len(cols))
for i, col := range cols {
prefixed[i] = alias + "." + col
prefixed[i] = qualifiedNullableStringColumn(alias, col)
}
return strings.Join(prefixed, ", ")
}
Expand All @@ -96,7 +148,7 @@ func qualifiedListItemColumns(alias string) string {
prefixed[i] = effectiveLastAirDateExpr(alias)
continue
}
prefixed[i] = alias + "." + col
prefixed[i] = qualifiedNullableStringColumn(alias, col)
}
return strings.Join(prefixed, ", ")
}
Expand Down
Loading