imagecache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package imagecache fetches remote images (covers) once and keeps them on disk, so browsers never fetch from metadata or source sites themselves: that would leak users' addresses and referrers, and many sites refuse hotlinking anyway.

Index

Constants

View Source
const MaxBytes = 15 << 20

MaxBytes caps one image.

Variables

View Source
var ErrNotImage = errors.New("imagecache: not an image")

ErrNotImage is a response that isn't an image.

Functions

This section is empty.

Types

type Cache

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

Cache is a directory of fetched images, keyed by a hash of their URL. It is rebuildable: deleting the directory only costs refetches.

func New

func New(dir string) *Cache

New returns a cache in dir.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, rawURL string) (Image, error)

Get returns the image at rawURL, fetching it over HTTP on first use.

func (*Cache) GetFresh

func (c *Cache) GetFresh(ctx context.Context, key string, maxAge time.Duration, fetch Fetcher) (Image, error)

GetFresh is GetWith for images that may change: one cached longer than maxAge (0: forever) is fetched again, and kept when that fails.

func (*Cache) GetWith

func (c *Cache) GetWith(ctx context.Context, key string, fetch Fetcher) (Image, error)

GetWith returns the image cached under key, calling fetch on first use. Concurrent requests for the same key share one fetch.

func (*Cache) SetHTTPClient

func (c *Cache) SetHTTPClient(hc *http.Client)

SetHTTPClient replaces the HTTP client (tests).

type Fetcher

type Fetcher func(ctx context.Context) (contentType string, body io.Reader, err error)

Fetcher produces an image: its content type and bytes.

type Image

type Image struct {
	Path        string
	ContentType string
	ModTime     time.Time
}

Image is a cached image.

type Signer

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

Signer signs image references the server hands out (e.g. a source's cover reference), so the image endpoint only fetches what the server itself listed, never an arbitrary URL a caller makes up.

func LoadSigner

func LoadSigner(ctx context.Context, st *store.Store) (*Signer, error)

LoadSigner reads the instance's signing key, creating it on first use.

func NewSigner

func NewSigner(key []byte) *Signer

NewSigner returns a signer with key.

func (*Signer) Sign

func (s *Signer) Sign(parts ...string) string

Sign returns a signature over parts.

func (*Signer) Verify

func (s *Signer) Verify(sig string, parts ...string) bool

Verify reports whether sig signs parts.

Jump to

Keyboard shortcuts

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