Posters: decode WebP and GIF; say what unreadable poster data looks like

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 00:20:14 +02:00
co-authored by Claude Sonnet 5.5
parent 706a094b20
commit 4d602b2acb
6 changed files with 105 additions and 10 deletions
+44
View File
@@ -3,10 +3,13 @@ package ui
import (
"bytes"
"context"
"encoding/base64"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"strings"
"sync/atomic"
"testing"
"time"
@@ -303,3 +306,44 @@ func TestCoverCellsKeepTheShape(t *testing.T) {
}
}
}
func TestDecodeCoverOtherFormats(t *testing.T) {
// A WebP stored under a .jpg name, as Sonarr keeps what the provider sends.
webp, _ := base64.StdEncoding.DecodeString("UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==")
img, err := decodeCover(webp, storedCoverHeight)
if err != nil || img.Bounds().Dx() != 1 || img.Bounds().Dy() != 1 {
t.Fatalf("webp: %v %v", img, err)
}
var buf bytes.Buffer
pal := image.NewPaletted(image.Rect(0, 0, 20, 30), []color.Color{color.RGBA{255, 0, 0, 255}, color.RGBA{0, 0, 255, 255}})
if err := gif.Encode(&buf, pal, nil); err != nil {
t.Fatal(err)
}
if img, err := decodeCover(buf.Bytes(), storedCoverHeight); err != nil || img.Bounds().Dy() != 30 {
t.Errorf("gif: %v %v", img, err)
}
}
func TestDescribeBytesAndTheErrorItGives(t *testing.T) {
for _, tc := range []struct {
name string
data []byte
want string
}{
{"empty", nil, "empty"},
{"avif", []byte("\x00\x00\x00\x1cftypavif\x00\x00"), "AVIF/HEIC, avif"},
{"html", []byte(" <html><body>Not Found</body></html>"), "HTML or SVG"},
{"bmp", []byte("BM\x00\x00\x00\x00"), "BMP"},
{"tiff", []byte("II*\x00\x08\x00"), "TIFF"},
{"binary", []byte{1, 2, 3, 4, 5}, "5 bytes starting 01 02 03 04 05"},
} {
if got := describeBytes(tc.data); !strings.Contains(got, tc.want) {
t.Errorf("%s: %q, want it to mention %q", tc.name, got, tc.want)
}
_, err := decodeCover(tc.data, storedCoverHeight)
if err == nil || !strings.Contains(err.Error(), tc.want) {
t.Errorf("%s: the error %v should say what the data is (%q)", tc.name, err, tc.want)
}
}
}