Posters: fall back to the provider's image when the instance has no copy

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 00:17:34 +02:00
co-authored by Claude Sonnet 5.5
parent 9edd62634f
commit 706a094b20
3 changed files with 41 additions and 3 deletions
+10 -2
View File
@@ -25,7 +25,7 @@ func (c *Client) Poster(images []Image, inLibrary bool) (key string, fetch func(
continue
}
if inLibrary && img.URL != "" {
local := img.URL
local, remote := img.URL, img.RemoteURL
base, _ := c.settings()
return base + "|" + local, func(ctx context.Context) ([]byte, error) {
// MediaCover has poster-250.jpg next to poster.jpg; fall back to the original.
@@ -34,7 +34,15 @@ func (c *Client) Poster(images []Image, inLibrary bool) (key string, fetch func(
return data, nil
}
}
return c.GetBytes(ctx, local)
data, err := c.GetBytes(ctx, local)
if err != nil && remote != "" {
// The instance has not cached (or has lost) its copy: use the
// metadata provider's, as for titles that are not in the library.
if remoteData, remoteErr := FetchURL(ctx, remote); remoteErr == nil {
return remoteData, nil
}
}
return data, err
}, true
}
if img.RemoteURL != "" {
+30
View File
@@ -94,3 +94,33 @@ func TestResolveURLBase(t *testing.T) {
t.Errorf("no url base: %q", got)
}
}
func TestPosterFallsBackToTheProviderWhenTheInstanceHasNoCopy(t *testing.T) {
var remoteKey string
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remoteKey = r.Header.Get("X-Api-Key")
w.Write([]byte("REMOTE"))
}))
defer remote.Close()
instance := httptest.NewServer(http.NotFoundHandler()) // MediaCover has nothing cached
defer instance.Close()
c := NewClient(instance.URL, "secret")
_, fetch, ok := c.Poster([]Image{{CoverType: "poster", URL: "/MediaCover/9/poster.jpg", RemoteURL: remote.URL + "/p.jpg"}}, true)
if !ok {
t.Fatal("no poster")
}
data, err := fetch(context.Background())
if err != nil || string(data) != "REMOTE" {
t.Fatalf("data=%q err=%v", data, err)
}
if remoteKey != "" {
t.Errorf("the API key went to the provider: %q", remoteKey)
}
// Without a remote address the local failure is reported.
_, fetch, _ = c.Poster([]Image{{CoverType: "poster", URL: "/MediaCover/9/poster.jpg"}}, true)
if _, err := fetch(context.Background()); err == nil {
t.Error("expected the local error")
}
}