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:
2026-11-26 12:45:50 -05:00
co-authored by Claude Opus 5.5
parent 074d9ffb0e
commit 55a290cf4d
6 changed files with 47 additions and 67 deletions
+24
View File
@@ -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"
}
+21
View File
@@ -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)
}
}
}