refactor(coverart): one image Content-Type table for /api and Subsonic (#8002)
api/media.go and subsonic/stream.go each had imageContentType, the same map in a different order, serving the same files. coverart.ImageContentType is now the one copy, with its test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+1
-17
@@ -63,22 +63,6 @@ func audioContentType(format string) string {
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
// imageContentType maps a file extension to a MIME type for cover art.
|
||||
// Unknown extensions fall back to octet-stream.
|
||||
func imageContentType(path string) string {
|
||||
switch strings.ToLower(filepath.Ext(path)) {
|
||||
case ".jpg", ".jpeg":
|
||||
return "image/jpeg"
|
||||
case ".png":
|
||||
return "image/png"
|
||||
case ".webp":
|
||||
return "image/webp"
|
||||
case ".gif":
|
||||
return "image/gif"
|
||||
}
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
// handleGetCover implements GET /api/albums/{id}/cover. Resolves the cover
|
||||
// path (explicit column, else sidecar next to first track), then delegates
|
||||
// byte-serving to http.ServeContent so clients get Range / If-Modified-Since
|
||||
@@ -131,7 +115,7 @@ func (h *handlers) serveArt(w http.ResponseWriter, r *http.Request, path string)
|
||||
writeErr(w, apierror.InternalMsg("server error", err))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", imageContentType(path))
|
||||
w.Header().Set("Content-Type", coverart.ImageContentType(path))
|
||||
// Art changes rarely (cover-source enrichment, manual rescan, rare
|
||||
// MBID-driven re-fetch). One-day max-age + must-revalidate means
|
||||
// clients skip the conditional GET for the bulk of a session, but
|
||||
|
||||
@@ -38,24 +38,6 @@ func TestAudioContentType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageContentType(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"/a/cover.jpg": "image/jpeg",
|
||||
"/a/cover.JPG": "image/jpeg",
|
||||
"/a/cover.jpeg": "image/jpeg",
|
||||
"/a/cover.png": "image/png",
|
||||
"/a/cover.webp": "image/webp",
|
||||
"/a/cover.gif": "image/gif",
|
||||
"/a/cover.bmp": "application/octet-stream",
|
||||
"/a/cover": "application/octet-stream",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := imageContentType(in); got != want {
|
||||
t.Errorf("imageContentType(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindSidecarCover_PrefersCoverOverFolder(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "cover.jpg"), []byte("c"), 0o644); err != nil {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package coverart
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ImageContentType is the Content-Type to serve a cover or artist image
|
||||
// with, from its file extension. The native API and Subsonic both serve
|
||||
// the same files, so they answer from this one table. An extension it
|
||||
// doesn't know is application/octet-stream.
|
||||
func ImageContentType(path string) string {
|
||||
switch strings.ToLower(filepath.Ext(path)) {
|
||||
case ".jpg", ".jpeg":
|
||||
return "image/jpeg"
|
||||
case ".png":
|
||||
return "image/png"
|
||||
case ".webp":
|
||||
return "image/webp"
|
||||
case ".gif":
|
||||
return "image/gif"
|
||||
}
|
||||
return "application/octet-stream"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package coverart
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestImageContentType(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"/a/cover.jpg": "image/jpeg",
|
||||
"/a/cover.JPG": "image/jpeg",
|
||||
"/a/cover.jpeg": "image/jpeg",
|
||||
"/a/cover.png": "image/png",
|
||||
"/a/cover.webp": "image/webp",
|
||||
"/a/cover.gif": "image/gif",
|
||||
"/a/cover.bmp": "application/octet-stream",
|
||||
"/a/cover": "application/octet-stream",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := ImageContentType(in); got != want {
|
||||
t.Errorf("ImageContentType(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -157,25 +157,10 @@ func serveImage(w http.ResponseWriter, r *http.Request, path string) {
|
||||
WriteFail(w, r, ErrGeneric, "Failed to stat cover art")
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", imageContentType(path))
|
||||
w.Header().Set("Content-Type", coverart.ImageContentType(path))
|
||||
http.ServeContent(w, r, filepath.Base(path), info.ModTime(), f)
|
||||
}
|
||||
|
||||
func imageContentType(path string) string {
|
||||
switch strings.ToLower(filepath.Ext(path)) {
|
||||
case ".jpg", ".jpeg":
|
||||
return "image/jpeg"
|
||||
case ".png":
|
||||
return "image/png"
|
||||
case ".gif":
|
||||
return "image/gif"
|
||||
case ".webp":
|
||||
return "image/webp"
|
||||
default:
|
||||
return "application/octet-stream"
|
||||
}
|
||||
}
|
||||
|
||||
// handleScrobble translates Subsonic scrobble calls into native play_events.
|
||||
//
|
||||
// submission=false (now playing) opens a play, leaving ended_at NULL.
|
||||
|
||||
@@ -37,19 +37,3 @@ func TestFindSidecarCover(t *testing.T) {
|
||||
t.Errorf("cover.jpg priority = %q, want %q", got, cover)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageContentType(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"cover.jpg": "image/jpeg",
|
||||
"cover.JPEG": "image/jpeg",
|
||||
"art.png": "image/png",
|
||||
"thumb.gif": "image/gif",
|
||||
"square.webp": "image/webp",
|
||||
"unknown.tiff": "application/octet-stream",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := imageContentType(in); got != want {
|
||||
t.Errorf("imageContentType(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user