poster_resolver

package
v0.0.0-...-8d14f5d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package poster_resolver picks the best available source image for a torrent resource and exposes it through one in-process cached entry point. Two consumers use this:

  • Library / continue-watching cards (resized JPEG per width)
  • Resource share previews (1200x630 OG canvas)

Both share resolution (IMDb poster > per-resource thumbnail > brand-default), S3 cache (keyed by source-content), and lazymap dedup (keyed by request). Cross-resource sharing happens at the S3 layer — two resources matched to the same IMDb work serve the same cached object even though their request URLs differ.

Index

Constants

View Source
const (
	MinWidth = 32
	MaxWidth = 1600
)

MinWidth and MaxWidth bound every caller-supplied resize width.

The bound is load-bearing, not cosmetic: the width reaches imaging.Resize(src, w, 0, Lanczos), which allocates w × h × 4 bytes with h derived from the source aspect ratio. On an ordinary 500x750 poster a width of 20000 asks for a single 2.4 GB allocation — one unauthenticated request against a 5Gi pod. Anything that turns a caller-supplied width into a resize MUST clamp through ValidateWidth.

Variables

View Source
var ErrNotFound = errors.New("poster: no source for resource")

ErrNotFound is returned when a resource has no resolvable source and no default fallback applies (resize mode). Distinct from real internal errors so the HTTP layer can map cleanly to 404.

Functions

func ValidateWidth

func ValidateWidth(w int) error

ValidateWidth rejects a resize width outside MinWidth..MaxWidth.

Types

type Mode

type Mode struct {
	OG    bool // true → render in 1200x630 OG canvas
	Width int  // resize-mode target width (only used when OG=false)
	Blur  bool // true → heavy Gaussian + darken (adult/sport)
	Raw   bool // true → suppress is_adult blur override (auth-gated route)
}

Mode is what the request asked the service to produce. Parsed from the `file` route parameter via ParseFileMode; the resolver may force Blur=true at miss time when resource_metadata.is_adult=true. Raw is set by the auth-gated /raw/ route to skip that blur override.

func ParseFileMode

func ParseFileMode(file string) (Mode, error)

ParseFileMode unpacks the `file` route parameter into a Mode. Accepts

og.jpg            → OG canvas
<width>.jpg       → resize to <width>

width is bounded (see ValidateWidth) so a hostile caller can't burn Lanczos CPU — or the pod's whole memory budget — asking for 99999.jpg.

func (Mode) CacheTag

func (m Mode) CacheTag() string

CacheTag is a short stable string for the cache key. Two requests with the same Mode share the same cached object for the same source. `blur-` and `raw-` prefixes keep blurred / unblurred / raw renders distinct so flipping is_adult on a resource doesn't invalidate the other variants.

type Result

type Result struct {
	Body []byte
	ETag string
	Mime string
}

Result is what the handler ultimately serves. ETag is precomputed once at miss-time so subsequent If-None-Match checks don't rehash the body on every request.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service caches rendered posters per (resource_id, file) for in-process dedup, and per (source-content, mode) on S3 for cross-pod + persistent sharing. The two layers compose:

request → lazymap (resource_id+file) → S3 (source-content+mode)
  → original fetch (IMDb URL / S3 thumbnail blob)

func New

func New(s3Cl *cs.S3Client, pg *cs.PG, cl *http.Client, thumb *thumbnail.Service, a *api.Api, cacheBucket string) *Service

New returns a configured Service. cacheBucket is the same poster cache bucket used by the legacy /lib/:type/poster/:imdb_id endpoint — sharing it costs nothing and lets either endpoint warm the cache for the other when source-content matches.

func (*Service) Get

func (s *Service) Get(ctx context.Context, resourceID, file string, force, allowRaw bool) (*Result, error)

Get is the entry point. file is the route's :file param ("500.jpg", "og.jpg"). force=true bypasses both cache layers — dev escape hatch wired off ?force=1 in non-release builds. allowRaw=true tells the miss path to skip the adult blur entirely; reserved for the auth-gated /raw/ route, which is the opt-out for users who've flipped UserSettings.ShowAdult.

type Source

type Source struct {
	Kind    SourceKind
	CacheID string
	Fetch   func(ctx context.Context) (image.Image, error)
}

Source is the resolved instruction for one resource. CacheID is the stable content-key used to scope the S3 cache so two resources that resolve to the same artwork share the cached object. Fetch decodes the original bytes to an in-memory image; callers chain a render step (resize or canvas) before serving.

func Resolve

func Resolve(ctx context.Context, db *pg.DB, thumb *thumbnail.Service, cl *http.Client, resourceID string) (*Source, error)

Resolve picks the best available source for a resource_id. Order:

  1. movie metadata → IMDb poster URL
  2. series metadata → IMDb poster URL
  3. per-resource thumbnail (image_file / ffmpeg_frame / audio_art)

Returns (nil, nil) when nothing usable exists — caller decides what to do (404 for resize, brand-default for OG canvas).

The *WithMetadata loaders preload the poster URL; the plain variants return rows with nil metadata. See handlers/resource/get.go for the same pattern.

type SourceKind

type SourceKind string

SourceKind labels where the resolved image came from. Used for observability (logs/metrics) and to drive cache-key prefixing.

const (
	SourceIMDbMovie  SourceKind = "imdb_movie"
	SourceIMDbSeries SourceKind = "imdb_series"
	SourceThumbnail  SourceKind = "thumbnail"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址