Documentation
¶
Overview ¶
Package thumbnail is a pure-Go (CGO_ENABLED=0) implementation of the freedesktop.org Thumbnail Managing Standard.
It stores thumbnails under $XDG_CACHE_HOME/thumbnails in the four canonical size buckets — normal (128px), large (256px), x-large (512px) and xx-large (1024px). Each thumbnail is a PNG whose filename is the MD5 hex digest of the canonical file:// URI of the source, and which carries the mandated tEXt metadata chunks Thumb::URI and Thumb::MTime (plus optional Thumb::Size, Thumb::Mimetype and Software). A cached thumbnail is valid only while its recorded Thumb::MTime matches the source file's current modification time; otherwise it is regenerated. Sources that cannot be thumbnailed are recorded under thumbnails/fail/<appname>/ so they are not retried until they change.
The scaling engine dog-foods the fleet's github.com/go-images/images resizer (SIMD bilinear) by default and falls back to golang.org/x/image/draw (Catmull-Rom) when a higher-quality filter is requested.
A Provider seam decouples "decode a source into an image" from the caching machinery so that non-file sources can be plugged in. Two providers ship: the default file-decoding provider, and a live-framebuffer provider that resizes an in-memory image.Image directly, bypassing the on-disk cache — intended for a compositor's live window / exposé thumbnails.
Index ¶
Constants ¶
const ( KeyURI = "Thumb::URI" // canonical source URI (mandatory) KeyMTime = "Thumb::MTime" // source mtime, decimal seconds (mandatory) KeySize = "Thumb::Size" // source size in bytes (optional) KeyMimetype = "Thumb::Mimetype" // source MIME type (optional) KeySoftware = "Software" // generating software (optional) )
Standard tEXt keywords defined (or recommended) by the Thumbnail Managing Standard.
Variables ¶
var ErrFailed = errors.New("thumbnail: source previously failed to thumbnail")
ErrFailed is wrapped by the error returned from Get/GetImage when the source is recorded in the fail directory for the current mtime, meaning a previous attempt failed and the source has not changed since.
Functions ¶
func FileURI ¶
FileURI returns the canonical file:// URI for a local filesystem path, as required by the Thumbnail Managing Standard for hash computation. The path is made absolute and each path segment is percent-encoded. A path that already carries a URI scheme (e.g. "file://…" or "http://…") is returned unchanged.
func Get ¶
Get returns the path to the cached normal-size thumbnail for uri, using a default cache rooted at $XDG_CACHE_HOME/thumbnails. It generates and caches the thumbnail if absent or stale.
Types ¶
type Cache ¶
type Cache struct {
// Root is the base thumbnails directory (default $XDG_CACHE_HOME/thumbnails).
Root string
// Size is the size bucket this cache serves.
Size Size
// Filter selects the scaling kernel (default Bilinear via go-images).
Filter Filter
// AppName names the fail subdirectory (thumbnails/fail/<AppName>).
AppName string
// Provider decodes sources into images (default FileProvider).
Provider Provider
// Software is written to the Software tEXt chunk; empty omits it.
Software string
// MaxBytes bounds the default provider's source file size.
MaxBytes int64
}
Cache is a thumbnail cache bound to a single size bucket. Construct it with New. The exported fields may be read for introspection; mutate them only before first use.
func New ¶
New returns a Cache for the given size bucket with the supplied options applied. Unset options take fleet-standard defaults.
func (*Cache) Get ¶
Get returns the path to the cached thumbnail for uri, generating and caching it if absent or stale. uri may be a filesystem path or a file:// URI.
func (*Cache) GetImage ¶
GetImage returns the thumbnail image for uri, generating and caching it if absent or stale.
type FileProvider ¶
type FileProvider struct {
MaxBytes int64
}
FileProvider is the default Provider: it decodes an image file at the given path using the standard library's registered decoders (PNG, JPEG, GIF). If MaxBytes is positive, sources whose on-disk size exceeds MaxBytes are rejected before decoding to bound resource use.
type Filter ¶
type Filter int
Filter selects the scaling kernel used to fit a source image into a bucket.
type FramebufferProvider ¶
type FramebufferProvider struct {
// Img is the live source image. A nil Img yields an error.
Img image.Image
}
FramebufferProvider is a live-framebuffer Provider seam for callers such as a compositor that already hold an image in memory (a window's framebuffer, an exposé snapshot). Src is ignored; the stored image is returned directly. Used with Cache.Live, it resizes without ever touching the on-disk cache.
type Option ¶
type Option func(*Cache)
Option configures a Cache in New.
func WithAppName ¶
WithAppName sets the fail-directory application name.
func WithMaxBytes ¶
WithMaxBytes bounds the default provider's source file size (0 disables it).
func WithProvider ¶
WithProvider sets a custom source Provider.
func WithSoftware ¶
WithSoftware sets the Software tEXt value (empty string omits the chunk).
type Provider ¶
Provider decodes or synthesizes the full-resolution source image identified by src. The default file provider treats src as a filesystem path; other providers may interpret it however they wish. A Provider must never panic on malformed input — it returns an error instead — but the cache also guards against panics defensively.
type Size ¶
type Size int
Size selects one of the four canonical thumbnail size buckets defined by the Thumbnail Managing Standard. The zero value is Normal.
func (Size) Dir ¶
Dir returns the on-disk directory name of the size bucket, as mandated by the standard (normal, large, x-large, xx-large).