- GET and HEAD /albums/{id}/cover serve a JPEG made from the picture embedded in
the album's files, or else a cover, folder, front, album or albumart image
(jpg, jpeg, png) in the album's folder. ?size=small (300 px) or large (1000 px).
The token can be in the query, so an <img> element can use it.
- Built the first time it is asked for, scaled, and kept in storage.data_dir
(SONDA_DATA_DIR, default "data"); an album with no cover is remembered for a day.
A new or changed file of the album, or a full rescan, clears the record.
- Nothing is sent anywhere. Pictures are untrusted: files are opened inside the
library only (links are not followed), over 20 MB or 16 million pixels are
ignored, and what is served is always a JPEG made here. Stored files are 0600.
- images table (migration 0013, SQLite and PostgreSQL).
- Spec, contract test, README and example config updated.
Closes #37
162 lines
4.6 KiB
Go
162 lines
4.6 KiB
Go
package images
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"hash/crc32"
|
|
"image"
|
|
"image/color"
|
|
"image/gif"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func solid(w, h int, c color.Color) image.Image {
|
|
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
for y := 0; y < h; y++ {
|
|
for x := 0; x < w; x++ {
|
|
img.Set(x, y, c)
|
|
}
|
|
}
|
|
return img
|
|
}
|
|
|
|
func encodePNG(t *testing.T, img image.Image) []byte {
|
|
t.Helper()
|
|
var b bytes.Buffer
|
|
if err := png.Encode(&b, img); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b.Bytes()
|
|
}
|
|
|
|
func encodeJPEG(t *testing.T, img image.Image) []byte {
|
|
t.Helper()
|
|
var b bytes.Buffer
|
|
if err := jpeg.Encode(&b, img, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b.Bytes()
|
|
}
|
|
|
|
// pngHeader is the start of a valid PNG: signature and IHDR, claiming a w by h picture.
|
|
func pngHeader(w, h uint32) []byte {
|
|
ihdr := make([]byte, 0, 17)
|
|
ihdr = append(ihdr, "IHDR"...)
|
|
ihdr = binary.BigEndian.AppendUint32(ihdr, w)
|
|
ihdr = binary.BigEndian.AppendUint32(ihdr, h)
|
|
ihdr = append(ihdr, 8, 6, 0, 0, 0) // 8 bits, RGBA, no interlace
|
|
out := []byte("\x89PNG\r\n\x1a\n")
|
|
out = binary.BigEndian.AppendUint32(out, 13)
|
|
out = append(out, ihdr...)
|
|
return binary.BigEndian.AppendUint32(out, crc32.ChecksumIEEE(ihdr))
|
|
}
|
|
|
|
func dims(t *testing.T, data []byte) (int, int, string) {
|
|
t.Helper()
|
|
cfg, format, err := image.DecodeConfig(bytes.NewReader(data))
|
|
if err != nil {
|
|
t.Fatalf("the result is not an image: %v", err)
|
|
}
|
|
return cfg.Width, cfg.Height, format
|
|
}
|
|
|
|
func TestProcessScalesToFitAndKeepsTheShape(t *testing.T) {
|
|
out, err := Process(encodePNG(t, solid(2000, 1000, color.RGBA{200, 30, 30, 255})))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w, h, f := dims(t, out.Large); w != 1000 || h != 500 || f != "jpeg" {
|
|
t.Errorf("large = %dx%d %s, want 1000x500 jpeg", w, h, f)
|
|
}
|
|
if w, h, f := dims(t, out.Small); w != 300 || h != 150 || f != "jpeg" {
|
|
t.Errorf("small = %dx%d %s, want 300x150 jpeg", w, h, f)
|
|
}
|
|
if out.Width != 2000 || out.Height != 1000 {
|
|
t.Errorf("original size = %dx%d, want 2000x1000", out.Width, out.Height)
|
|
}
|
|
}
|
|
|
|
func TestProcessNeverEnlarges(t *testing.T) {
|
|
out, err := Process(encodeJPEG(t, solid(200, 200, color.RGBA{10, 10, 200, 255})))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, data := range map[string][]byte{"large": out.Large, "small": out.Small} {
|
|
if w, h, _ := dims(t, data); w != 200 || h != 200 {
|
|
t.Errorf("%s = %dx%d, want it left at 200x200", name, w, h)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessKeepsTheColours(t *testing.T) {
|
|
out, err := Process(encodePNG(t, solid(1500, 1500, color.RGBA{220, 20, 20, 255})))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
img, err := jpeg.Decode(bytes.NewReader(out.Large))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, g, b, _ := img.At(500, 500).RGBA()
|
|
if r>>8 < 190 || g>>8 > 60 || b>>8 > 60 {
|
|
t.Errorf("the middle is (%d,%d,%d), want about (220,20,20)", r>>8, g>>8, b>>8)
|
|
}
|
|
}
|
|
|
|
func TestProcessPutsTransparencyOnWhite(t *testing.T) {
|
|
out, err := Process(encodePNG(t, solid(400, 400, color.NRGBA{0, 0, 0, 0})))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
img, _ := jpeg.Decode(bytes.NewReader(out.Large))
|
|
r, g, b, _ := img.At(10, 10).RGBA()
|
|
if r>>8 < 240 || g>>8 < 240 || b>>8 < 240 {
|
|
t.Errorf("a transparent picture became (%d,%d,%d), want white", r>>8, g>>8, b>>8)
|
|
}
|
|
}
|
|
|
|
func TestProcessReadsGIFAndJPEGAndPNG(t *testing.T) {
|
|
var g bytes.Buffer
|
|
if err := gif.Encode(&g, solid(50, 50, color.RGBA{1, 2, 3, 255}), nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, data := range map[string][]byte{"gif": g.Bytes(), "jpeg": encodeJPEG(t, solid(50, 50, color.White)), "png": encodePNG(t, solid(50, 50, color.White))} {
|
|
if _, err := Process(data); err != nil {
|
|
t.Errorf("%s: %v", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessRefusesWhatIsNotAUsablePicture(t *testing.T) {
|
|
bomb := pngHeader(60000, 60000) // a valid header that claims 3.6 billion pixels
|
|
for name, data := range map[string][]byte{
|
|
"empty": nil,
|
|
"text": []byte(strings.Repeat("not an image ", 100)),
|
|
"truncated": encodeJPEG(t, solid(300, 300, color.White))[:80],
|
|
"claims huge": bomb,
|
|
"zero width": pngHeader(0, 5),
|
|
} {
|
|
if _, err := Process(data); err == nil {
|
|
t.Errorf("%s: accepted", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessRefusesAFileThatIsTooBig(t *testing.T) {
|
|
big := append(encodeJPEG(t, solid(10, 10, color.White)), make([]byte, MaxSourceBytes)...)
|
|
if _, err := Process(big); err == nil {
|
|
t.Error("a file over the size limit was accepted")
|
|
}
|
|
}
|
|
|
|
func TestProcessIsDeterministicSoTheEtagIsStable(t *testing.T) {
|
|
src := encodePNG(t, solid(1200, 900, color.RGBA{5, 100, 200, 255}))
|
|
a, _ := Process(src)
|
|
b, _ := Process(src)
|
|
if !bytes.Equal(a.Large, b.Large) || !bytes.Equal(a.Small, b.Small) {
|
|
t.Error("the same picture gave different bytes")
|
|
}
|
|
}
|