thumb

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2015 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package thumb implements a lazy image thumbnail cache. Supported input image formats are any format Go can decode natively from the standard library and subrepo golang.org/x/image.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeFunc

func DecodeFunc(name string) func(io.Reader) (image.Image, error)

DecodeFunc returns a func that can be used to decode the image with the given file name, or nil if it's not supported. TODO: sniff magic number instead of only using file extension TODO: allow externally registered format decoders

func FormatSupported

func FormatSupported(ext string) bool

FormatSupported returns true if the given file extension belongs to an image format that can be thumbnailed by this package.

Types

type Cache

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

Cache is a lazy, concurrent thumbnail cache for airlift-server with request batching for on-the-fly thumbnail generation. Only file paths are cached in memory.

func NewCache

func NewCache(dirPath string, enc Encoder, store FileStore, scaler draw.Scaler) (*Cache, error)

NewCache initializes a new thumbnail generator that stores files encoded from store by enc in dirPath. w and h determine the maximum dimensions of the thumbnails.

func (*Cache) Get

func (c *Cache) Get(id string, w, h int) string

Get the file path to the thumbnail of the file with the given id. Generate it if it doesn't exist already. If concurrent requests are made to the same non-existent thumbnail, it will only be generated once.

TODO: error handling

func (*Cache) Purge

func (c *Cache) Purge() error

func (*Cache) Remove

func (c *Cache) Remove(id string) error

Remove deletes all sizes of thumbnail of a given file.

func (*Cache) Serve

func (c *Cache) Serve()

Serve starts the cache request server, blocking forever. It should be launched in its own goroutine before any requests are made.

func (*Cache) Size

func (c *Cache) Size() int64

type Encoder

type Encoder interface {
	Extension() string // The file extension of the resulting image
	Encode(dst io.Writer, thumb image.Image) error
}

Encoder describes a way to encode a thumbnail image.

type FileStore

type FileStore interface {
	// Get should return the path to the file on disk, or the empty string if
	// not found.
	Get(id string) string
}

FileStore is a source of files that Cache will reference.

type JPEGEncoder

type JPEGEncoder struct{ *jpeg.Options }

func (JPEGEncoder) Encode

func (e JPEGEncoder) Encode(dst io.Writer, thumb image.Image) error

func (JPEGEncoder) Extension

func (JPEGEncoder) Extension() string

Jump to

Keyboard shortcuts

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