1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
package main
import (
"context"
"net/http"
"net/url"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
// Fixtures are real responses (HTML cut at </head>), fetched 2026-09-29.
func TestThumbExtract(t *testing.T) {
for _, c := range []struct {
file, page, want string
}{
{"ard.json", "https://www.ardmediathek.de/video/x",
"https://api.ardmediathek.de/image-service/images/urn:ard:image:e1aeb0d38db95a63?w=640&ch=c7cafedeecb7833c"},
{"phoenix.json", "https://www.phoenix.de/sendungen/x-a-5227157.html",
"https://www.phoenix.de/sixcms/media.php/21/thumbnails/_UDL%20Wahlen%20Berlin%20MeckPomm%20TN%20Medi.png.4041693.png"},
{"zdf.html", "https://www.zdf.de/video/x",
"https://epg-image.zdf.de/fotobase-webdelivery/images/1ef16089-2155-46ae-9de2-84ab911cad47?layout=768x432"},
{"3sat.html", "https://www.3sat.de/x",
"https://epg-image.zdf.de/fotobase-webdelivery/images/6e64eef6-8a8f-42d4-9318-0c4bd5e96f2f?layout=768x432"},
{"arte.html", "https://www.arte.tv/x",
"https://api-cdn.arte.tv/img/v2/image/LRZURxwZsfBHnihqU5KJUY/640x360?type=TEXT&watermark=true"},
{"orf.html", "https://on.orf.at/x",
"https://api-tvthek.orf.at/assets/segments/0235/86/thumb_23485208_segments_highlight_teaser.jpg"},
{"dw.html", "https://p.dw.com/p/5Mg4X", "https://static.dw.com/image/79227800_6.jpg"},
// http in the page, upgraded
{"sr.html", "https://www.sr-mediathek.de/x",
"https://www.sr-mediathek.de/mediathek-images/thumb512/1790355185_wettersponsorfrei20260925_35775080_35775240_10030320.orig.jpg"},
// a moved ZDF page without an image
{"zdf-old.html", "https://www.zdf.de/dokumentation/x", ""},
} {
body, err := os.ReadFile("testdata/thumbs/" + c.file)
if err != nil {
t.Fatal(err)
}
page, _ := url.Parse(c.page)
src := thumbHosts[page.Host]
img, err := src.extract(body)
if err != nil {
t.Errorf("%s: %v", c.file, err)
continue
}
if got := cleanThumb(img, page); got != c.want {
t.Errorf("%s:\n got %s\n want %s", c.file, got, c.want)
}
}
// These only need to find *an* image; their URLs carry a version stamp.
for _, f := range []string{"srf.html", "kika.html", "ndr.html"} {
body, _ := os.ReadFile("testdata/thumbs/" + f)
img, _ := ogSource{}.extract(body)
if got := cleanThumb(img, &url.URL{Scheme: "https", Host: "x"}); !strings.HasPrefix(got, "https://") {
t.Errorf("%s: %q", f, got)
}
}
// SRF's image service is asked for a thumbnail-sized image.
body, _ := os.ReadFile("testdata/thumbs/srf.html")
img, _ := ogSource{}.extract(body)
if got := cleanThumb(img, &url.URL{Scheme: "https", Host: "www.srf.ch"}); !strings.Contains(got, "width=480") {
t.Errorf("srf width: %s", got)
}
}
func TestThumbPlaceholders(t *testing.T) {
base := &url.URL{Scheme: "https", Host: "x"}
for _, u := range []string{
"https://www.ardmediathek.de/sharing-icon-1280x720.1788868624859.png",
"https://www.zdf.de/_next/static/media/zdf_logo_1920x1080.3pjn65g-tnlye.png",
"https://www.zdf.de/assets/mainzelmaennchen-fragezeichen-v02-100~1280x720",
"{{ meta.image || config.imageurl+'/pics/facebook.jpg' }}",
"javascript:alert(1)",
"",
} {
if got := cleanThumb(u, base); got != "" {
t.Errorf("cleanThumb(%q) = %q, want rejected", u, got)
}
}
}
func TestThumbRequests(t *testing.T) {
for _, c := range []struct{ page, want string }{
{"https://www.ardmediathek.de/video/Y3JpZDovL2Rhcw", "https://api.ardmediathek.de/page-gateway/pages/ard/item/Y3JpZDovL2Rhcw"},
{"https://www.ardmediathek.de/video/show/title/ch/Y3JpZDovL2Rhcw", "https://api.ardmediathek.de/page-gateway/pages/ard/item/Y3JpZDovL2Rhcw"},
{"https://www.phoenix.de/sendungen/x/y-a-5227157.html", "https://www.phoenix.de/response/id/5227157"},
{"http://p.dw.com/p/5Mg4X", "https://p.dw.com/p/5Mg4X"},
} {
page, _ := url.Parse(c.page)
got, ok := thumbHosts[page.Host].request(page)
if !ok || got != c.want {
t.Errorf("request(%s) = %q, %v", c.page, got, ok)
}
}
// ARD has a second form to fall back to.
page, _ := url.Parse("https://www.ardmediathek.de/video/Y3JpZDovL2Rhcw")
if fb := (ardSource{}).fallbacks(page); len(fb) != 1 || fb[0] != "https://api.ardmediathek.de/page-gateway/pages/ard/item/Y3JpZDovL2Rhcw?devicetype=pc&embedded=true" {
t.Errorf("ard fallbacks: %v", fb)
}
// Unknown hosts are never fetched.
if _, err := lookupThumb(context.Background(), http.DefaultClient, "http://169.254.169.254/latest"); err != errNoSource {
t.Errorf("unknown host: %v", err)
}
}
// The handler against a real (small) generation, with the lookup replaced.
func TestThumbHandler(t *testing.T) {
s, ts := newTestServer(t)
zdf := videoUUID("ZDF", "Maybrit Illner", "https://nrodlzdf-a.akamaihd.net/none/zdf/26/09/f1/1/f1_3360k_p36v17.mp4")
neo := videoUUID("ZDFneo", "Maybrit Illner", "https://nrodlzdf-a.akamaihd.net/none/zdf/26/09/f1neo/1/f1neo_808k_p11v17.mp4")
srf := videoUUID("SRF", "Tagesschau", "https://srf-vod-amd.akamaized.net/world/hls/x/y_,q40,q10,q20,q30,q50,q60,.mp4.csmil/index-f1-v1-a1.m3u8")
var calls atomic.Int32
release := make(chan struct{})
s.thumbs.lookup = func(ctx context.Context, website string) (string, error) {
calls.Add(1)
if website == "https://www.zdf.de/f1" {
<-release // slower than thumbWait
return "https://img.example/f1.jpg", nil
}
return "", nil
}
c := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
get := func(id string) *http.Response {
resp, err := c.Get(ts.URL + "/thumb/" + id + ".jpg")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
return resp
}
// Ten concurrent first requests: one lookup, all give up after thumbWait.
start := time.Now()
var wg sync.WaitGroup
for range 10 {
wg.Add(1)
go func() {
defer wg.Done()
if r := get(zdf); r.StatusCode != 404 || r.Header.Get("Cache-Control") != "no-store" {
t.Errorf("slow lookup: %d %s", r.StatusCode, r.Header.Get("Cache-Control"))
}
}()
}
wg.Wait()
if d := time.Since(start); d < thumbWait || d > thumbWait+time.Second {
t.Errorf("waited %s, want about %s", d, thumbWait)
}
close(release)
deadline := time.Now().Add(2 * time.Second)
for {
if st, _, _ := s.thumbs.get(context.Background(), zdf); st == thumbFound || time.Now().After(deadline) {
break
}
time.Sleep(10 * time.Millisecond)
}
// The lookup went on and filled the cache: now a redirect, no new lookup.
if r := get(zdf); r.StatusCode != 302 || r.Header.Get("Location") != "https://img.example/f1.jpg" {
t.Errorf("after lookup: %d %s", r.StatusCode, r.Header.Get("Location"))
}
// A dedup alias shares the kept video's thumbnail.
if r := get(neo); r.StatusCode != 302 {
t.Errorf("alias: %d", r.StatusCode)
}
if n := calls.Load(); n != 1 {
t.Errorf("%d lookups, want 1", n)
}
// A video without a website: 404, cacheable, no lookup at all.
if r := get(srf); r.StatusCode != 404 || r.Header.Get("Cache-Control") != "public, max-age=86400" {
t.Errorf("no website: %d %s", r.StatusCode, r.Header.Get("Cache-Control"))
}
if n := calls.Load(); n != 1 {
t.Errorf("%d lookups after no-website video, want 1", n)
}
// The video JSON points at it, and accounts/channels carry a logo.
var v map[string]any
getJSON(t, ts, "/api/v1/videos/"+zdf, &v)
if v["thumbnailPath"] != "/thumb/"+zdf+".jpg" {
t.Errorf("thumbnailPath %v", v["thumbnailPath"])
}
av := v["account"].(map[string]any)["avatars"].([]any)
if len(av) != 1 || av[0].(map[string]any)["path"] != "/logos/zdf.png" {
t.Errorf("account avatars %v", av)
}
if r, _ := http.Get(ts.URL + "/logos/zdf.png"); r.StatusCode != 200 || r.Header.Get("Content-Type") != "image/png" {
t.Errorf("logo: %d %s", r.StatusCode, r.Header.Get("Content-Type"))
}
}
func TestThumbRetry(t *testing.T) {
db, err := openDB(t.TempDir()+"/t.db", writeBusyTimeout)
if err != nil {
t.Fatal(err)
}
defer db.Close()
now := time.Unix(1800000000, 0)
th := &thumbs{db: db, now: func() time.Time { return now }}
for _, r := range []struct {
uuid string
url string
age time.Duration
failed bool
want thumbState
}{
{"found", "https://i/x.jpg", 400 * 24 * time.Hour, false, thumbFound},
{"none-fresh", "", 6 * 24 * time.Hour, false, thumbNone},
{"none-old", "", 8 * 24 * time.Hour, false, thumbUnknown},
{"err-fresh", "", 23 * time.Hour, true, thumbNone},
{"err-old", "", 25 * time.Hour, true, thumbUnknown},
} {
db.Exec(`INSERT INTO thumb VALUES (?,?,?,?)`, r.uuid, r.url, now.Add(-r.age).Unix(), r.failed)
if st, _, _ := th.get(context.Background(), r.uuid); st != r.want {
t.Errorf("%s: state %d, want %d", r.uuid, st, r.want)
}
}
if st, _, _ := th.get(context.Background(), "never"); st != thumbUnknown {
t.Error("never looked up")
}
}
|