imagefetch

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package imagefetch fetches and checks images: the reader's image proxy and prefetch download remote ones through a client of its own (public hosts only, no cookies, no Referer, a generic User-Agent), and a message's inline ones count against one Budget. Every image must be a PNG, JPEG, GIF or WebP within the per-image limits.

Index

Constants

View Source
const (
	MaxImages      = 50       // images, remote and inline
	MaxImageBytes  = 5 << 20  // one image
	MaxTotalBytes  = 20 << 20 // all images together
	MaxPixels      = 40_000_000
	MaxTotalPixels = 100_000_000 // all images together, decoded
	Timeout        = 15 * time.Second
	MaxRedirects   = 5
)

The budget of one message's images (global constraints).

View Source
const UserAgent = "Mozilla/5.0 (compatible; ImageProxy)"

UserAgent is what the proxy sends: no browser, no app version.

Variables

View Source
var (
	ErrTooMany       = errors.New("too many images")
	ErrTooLarge      = errors.New("image too large")
	ErrTotalTooLarge = errors.New("images too large together")
	ErrPixels        = errors.New("image has too many pixels")
	ErrTotalPixels   = errors.New("images have too many pixels together")
	ErrFormat        = errors.New("not a PNG, JPEG, GIF or WebP image")
	ErrScheme        = errors.New("image address is not http or https")
	ErrHost          = errors.New("image host is not public")
)

The reasons an image is refused.

View Source
var Formats = map[string]bool{"png": true, "jpeg": true, "gif": true, "webp": true}

Formats are the formats shown, as image.DecodeConfig names them.

Functions

func Config

func Config(data []byte) (format string, px int64, err error)

Config is Dims, with the pixels.

func DataURL

func DataURL(src string) ([]byte, error)

DataURL decodes a data: URL's bytes, at most MaxImageBytes of them.

func Dims added in v0.1.0

func Dims(data []byte) (format string, width, height int, err error)

Dims is data's format and size, read from its header: a PNG, JPEG, GIF or WebP image (by content) of at most MaxPixels.

func Download added in v0.1.0

func Download(ctx context.Context, c *http.Client, src string) ([]byte, error)

Download gets src (http or https) with c: at most MaxImageBytes of it.

func Encode

func Encode(format string, data []byte) string

Encode is data, an image of format (as Config names it), as a data: URL.

func Fetch added in v0.1.0

func Fetch(ctx context.Context, c *http.Client, src string) (data []byte, format string, width, height int, err error)

Fetch downloads src and checks it: its bytes, format and size in pixels.

func IsHTTP

func IsHTTP(u *url.URL) bool

IsHTTP reports an http or https URL.

func NewClient

func NewClient(allowLoopback bool) *http.Client

NewClient is a client of its own: no proxy, no cookie jar, no Referer, public hosts only (checked on the address dialed, after DNS), http(s) redirects only and at most MaxRedirects of them. allowLoopback lets tests reach httptest servers, which listen on loopback.

func PublicIP

func PublicIP(ip netip.Addr) bool

PublicIP is false for loopback, private, link-local, unspecified, multicast and other non-global addresses (shared 100.64.0.0/10 included).

Types

type Budget

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

Budget is one message's images' budget: their count, bytes and decoded pixels, inline and remote together. It is safe for concurrent use.

func NewBudget

func NewBudget() *Budget

NewBudget is a whole budget.

func (*Budget) Take

func (b *Budget) Take() error

Take counts one more image; ErrTooMany past MaxImages.

func (*Budget) TakeBytes

func (b *Budget) TakeBytes(n int64) error

TakeBytes takes n bytes; ErrTotalTooLarge when fewer are left.

func (*Budget) TakeInline

func (b *Budget) TakeInline(size, px int64) error

TakeInline takes an inline image of size bytes and px pixels: its count, bytes and pixels, all or none of them but the count (as a remote image's, which is counted once asked for).

func (*Budget) TakePixels

func (b *Budget) TakePixels(px int64) error

TakePixels takes px decoded pixels; ErrTotalPixels when fewer are left.

Jump to

Keyboard shortcuts

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