sonda/internal/images/process.go
tgiachi 9e85d4d1ea fix(images): outages are 502, purges are not undone, bad pictures are not outages
- A timeout inside the provider (the outbound client's own deadline, which wraps
  context.DeadlineExceeded) was mistaken for the caller leaving and came out as a 500, so
  an older stored picture was not served. The decision now uses the caller's own context,
  and a build that runs out of its two minutes is an outage (502) too.
- A purge of the Commons data could be undone by a read that was already running: the row
  was deleted, so the generation guard started from zero, and the files were written after
  the files were removed. Pictures are now kept only under a purge counter; BeginArtistPurge
  is called before the purge and a read that started earlier starts again, or is refused if
  the provider is off by then.
- A redirect or an oversized picture from the picture host is a fault of that picture (no
  picture), not an outage, and the pictures are downloaded through a client of their own so
  that one bad file cannot open the breaker of the API calls.
- The author drops the invisible characters that reorder or hide text. Artist pictures are
  decoded only up to 4 million pixels.
- Tests: an outage that ends in a deadline, a build that times out, a purge during a read,
  a redirect and a huge picture from the host, a separate download client.
2026-11-11 22:12:49 +01:00

124 lines
4.1 KiB
Go

// Package images turns pictures found on the user's own disk into the covers the
// API serves, and keeps them in a data folder.
//
// Pictures are untrusted input: a file called cover.jpg can be anything. They are
// size-checked before they are decoded, decoded with the standard library only, and
// stored re-encoded, so what is served is always a plain JPEG made here.
package images
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
_ "image/gif" // registered for image.Decode
"image/jpeg"
_ "image/png" // registered for image.Decode
)
// Limits and sizes.
const (
// MaxSourceBytes is the largest picture file that is read at all.
MaxSourceBytes = 20 << 20
// MaxPixels is the largest picture that is decoded: a 4000 by 4000 cover. A file
// of a few kilobytes can claim billions of pixels and fill the memory.
MaxPixels = 16_000_000
// LargeEdge and SmallEdge are the longest side of the two stored sizes.
LargeEdge = 1000
SmallEdge = 300
jpegQuality = 85
)
// ErrUnusable is returned for a file that is not a picture Sonda can use.
var ErrUnusable = errors.New("not a usable picture")
// Processed is a picture in the two stored sizes, as JPEG.
type Processed struct {
Large, Small []byte
Width, Height int // of the original
}
// Process decodes a GIF, JPEG or PNG and returns it scaled down to fit LargeEdge and
// SmallEdge (never enlarged), on a white background where it was transparent. The
// same input always gives the same bytes.
func Process(data []byte) (Processed, error) { return ProcessLimited(data, MaxPixels) }
// ProcessLimited is Process with a smaller limit on the number of pixels, for pictures that
// are known to be small (a 960 pixel copy is never millions of pixels high).
func ProcessLimited(data []byte, maxPixels int64) (Processed, error) {
if len(data) == 0 || len(data) > MaxSourceBytes {
return Processed{}, fmt.Errorf("%w: %d bytes", ErrUnusable, len(data))
}
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
if err != nil {
return Processed{}, fmt.Errorf("%w: %v", ErrUnusable, err)
}
if cfg.Width <= 0 || cfg.Height <= 0 || int64(cfg.Width)*int64(cfg.Height) > maxPixels { // int64: a 32-bit int would overflow
return Processed{}, fmt.Errorf("%w: %dx%d pixels", ErrUnusable, cfg.Width, cfg.Height)
}
src, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return Processed{}, fmt.Errorf("%w: %v", ErrUnusable, err)
}
flat := image.NewRGBA(src.Bounds())
draw.Draw(flat, flat.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
draw.Draw(flat, flat.Bounds(), src, src.Bounds().Min, draw.Over)
large := fit(flat, LargeEdge)
small := fit(large, SmallEdge)
out := Processed{Width: cfg.Width, Height: cfg.Height}
if out.Large, err = encode(large); err != nil {
return Processed{}, err
}
if out.Small, err = encode(small); err != nil {
return Processed{}, err
}
return out, nil
}
func encode(img *image.RGBA) ([]byte, error) {
var b bytes.Buffer
if err := jpeg.Encode(&b, img, &jpeg.Options{Quality: jpegQuality}); err != nil {
return nil, fmt.Errorf("encode picture: %w", err)
}
return b.Bytes(), nil
}
// fit returns src scaled down so that its longest side is at most edge, averaging
// the source pixels under each new one. A picture that is small enough is returned as it is.
func fit(src *image.RGBA, edge int) *image.RGBA {
b := src.Bounds()
sw, sh := b.Dx(), b.Dy()
if sw <= edge && sh <= edge {
return src
}
dw, dh := edge, edge
if sw >= sh {
dh = max(1, sh*edge/sw)
} else {
dw = max(1, sw*edge/sh)
}
dst := image.NewRGBA(image.Rect(0, 0, dw, dh))
for y := 0; y < dh; y++ {
y0, y1 := y*sh/dh, max(y*sh/dh+1, (y+1)*sh/dh)
for x := 0; x < dw; x++ {
x0, x1 := x*sw/dw, max(x*sw/dw+1, (x+1)*sw/dw)
var r, g, bl, n uint32
for sy := y0; sy < y1; sy++ {
row := src.Pix[(sy)*src.Stride:]
for sx := x0; sx < x1; sx++ {
p := row[sx*4 : sx*4+4]
r, g, bl, n = r+uint32(p[0]), g+uint32(p[1]), bl+uint32(p[2]), n+1
}
}
o := dst.PixOffset(x, y)
dst.Pix[o], dst.Pix[o+1], dst.Pix[o+2], dst.Pix[o+3] = uint8(r/n), uint8(g/n), uint8(bl/n), 255
}
}
return dst
}