Documentation
¶
Overview ¶
Package imagefetch fetches and checks a message's images within one budget: the native reader's loader and the reader page's Image share it. Remote images come through a client of its own (public hosts only, no cookies, no Referer); every image, remote or inline, must be a PNG, JPEG, GIF or WebP within the per-image and per-message limits.
Index ¶
- Constants
- Variables
- func Config(data []byte) (format string, px int64, err error)
- func DataURL(src string) ([]byte, error)
- func Encode(format string, data []byte) string
- func IsHTTP(u *url.URL) bool
- func NewClient(allowLoopback bool) *http.Client
- func PublicIP(ip netip.Addr) bool
- type Budget
- func (b *Budget) BytesLeft() int64
- func (b *Budget) Check(data []byte) (format string, px int64, err error)
- func (b *Budget) Download(ctx context.Context, c *http.Client, src string) ([]byte, error)
- func (b *Budget) GivePixels(px int64)
- func (b *Budget) Take() error
- func (b *Budget) TakeBytes(n int64) error
- func (b *Budget) TakeInline(size, px int64) error
- func (b *Budget) TakePixels(px int64) error
Constants ¶
const ( MaxImages = 50 // images, remote and inline MaxImageBytes = 5 << 20 // one image MaxTotalBytes = 20 << 20 // all images together Workers = 4 // downloads and decodes at once 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).
Variables ¶
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.
Formats are the formats shown, as image.DecodeConfig names them.
Functions ¶
func Config ¶
Config is data's format and pixels, read from its header: a PNG, JPEG, GIF or WebP image (by content) of at most MaxPixels.
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 (*Budget) Download ¶
Download gets src (http or https) with c, at most MaxImageBytes of it, reserving the bytes from the budget as they are read: once it is gone no request goes out and reads in flight stop.
func (*Budget) GivePixels ¶
GivePixels gives back pixels taken for an image not shown after all.
func (*Budget) TakeInline ¶
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 ¶
TakePixels takes px decoded pixels; ErrTotalPixels when fewer are left.