fix: harden discover artwork caching

This commit is contained in:
ShukeBta
2026-06-24 19:26:32 +08:00
parent d93a28f18f
commit aa506d6566
17 changed files with 658 additions and 104 deletions
+7 -4
View File
@@ -1,6 +1,7 @@
package handler
import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
@@ -14,6 +15,8 @@ import (
"github.com/ShukeBta/MediaStationGo/internal/service/cloud"
)
var handlerTestJPEG = []byte{0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 'J', 'F', 'I', 'F', 0x00, 0xff, 0xd9}
func TestCloudMountLibraryNameDefaultsToDirectoryBaseName(t *testing.T) {
tests := []struct {
name string
@@ -96,7 +99,7 @@ func TestCloudArtworkProxyServesCachedImageWithoutCloudResolve(t *testing.T) {
gin.SetMode(gin.TestMode)
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/jpeg")
_, _ = w.Write([]byte("cached-cloud-poster"))
_, _ = w.Write(handlerTestJPEG)
}))
defer upstream.Close()
@@ -116,7 +119,7 @@ func TestCloudArtworkProxyServesCachedImageWithoutCloudResolve(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("status = %d body=%s, want 200", w.Code, w.Body.String())
}
if got := w.Body.String(); got != "cached-cloud-poster" {
if got := w.Body.Bytes(); !bytes.Equal(got, handlerTestJPEG) {
t.Fatalf("body = %q, want cached poster", got)
}
if got := w.Header().Get("Cache-Control"); !strings.Contains(got, "max-age=2592000") {
@@ -128,7 +131,7 @@ func TestCloudArtworkProxyAcceptsCachedTBNImage(t *testing.T) {
gin.SetMode(gin.TestMode)
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/jpeg")
_, _ = w.Write([]byte("cached-tbn-poster"))
_, _ = w.Write(handlerTestJPEG)
}))
defer upstream.Close()
@@ -148,7 +151,7 @@ func TestCloudArtworkProxyAcceptsCachedTBNImage(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("status = %d body=%s, want 200", w.Code, w.Body.String())
}
if got := w.Body.String(); got != "cached-tbn-poster" {
if got := w.Body.Bytes(); !bytes.Equal(got, handlerTestJPEG) {
t.Fatalf("body = %q, want cached tbn poster", got)
}
}
+3 -2
View File
@@ -1,6 +1,7 @@
package handler
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -95,7 +96,7 @@ func TestEmbyItemImageServesCachedCloudArtworkWithoutResolve(t *testing.T) {
gin.SetMode(gin.TestMode)
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/jpeg")
_, _ = w.Write([]byte("emby-cached-cloud-poster"))
_, _ = w.Write(handlerTestJPEG)
}))
defer upstream.Close()
@@ -137,7 +138,7 @@ func TestEmbyItemImageServesCachedCloudArtworkWithoutResolve(t *testing.T) {
if w.Code != http.StatusOK {
t.Fatalf("unexpected status: %d body=%s", w.Code, w.Body.String())
}
if got := w.Body.String(); got != "emby-cached-cloud-poster" {
if got := w.Body.Bytes(); !bytes.Equal(got, handlerTestJPEG) {
t.Fatalf("body = %q, want cached cloud poster", got)
}
if location := w.Header().Get("Location"); location != "" {
+3 -1
View File
@@ -70,7 +70,9 @@ func stopTranscodeHandler(svc *service.Container) gin.HandlerFunc {
func imageProxyHandler(svc *service.Container) gin.HandlerFunc {
return func(c *gin.Context) {
raw := c.Query("url")
if c.Query("retry") != "" || c.Query("refresh") != "" {
if c.Query("refresh") != "" {
_ = svc.ImageProxy.RemoveFailed(raw)
} else if c.Query("retry") != "" {
_ = svc.ImageProxy.RemoveFailed(raw)
}
// Serve handles upstream errors internally by returning a 1×1 PNG