Files
minstrel/internal/subsonic/stream.go
T
bvandeusenandClaude Opus 5.5 1965e82013
release / go (push) Successful in 1m47s
release / web (push) Successful in 1m10s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m3s
release / android (push) Successful in 4m27s
release / Build signed APK (push) Successful in 5m2s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(subsonic): artist pictures in Subsonic apps
getCoverArt now answers an artist's id: the artist picture Minstrel
fetched, when its file is there, or else the cover of the artist's most
recently added album that has one, which is what Minstrel's own artist
cards show. Every artist entry carries its id as coverArt, so apps ask.

The lookup is coverart.ResolveArtistPath, beside the album one, and looks
through at most ten of an artist's albums for a cover.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 18:20:30 -05:00

229 lines
7.0 KiB
Go

package subsonic
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/coverart"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
)
// mediaHandlers serves the bytes-on-the-wire endpoints: stream, download,
// getCoverArt, scrobble. Scrobble routes through playevents.Writer so
// Subsonic clients feed the same play_events table as the native /api/events.
type mediaHandlers struct {
pool *pgxpool.Pool
events *playevents.Writer
logger *slog.Logger
}
func newMediaHandlers(pool *pgxpool.Pool, events *playevents.Writer, logger *slog.Logger) *mediaHandlers {
return &mediaHandlers{pool: pool, events: events, logger: logger}
}
// handleStream serves the raw track bytes. http.ServeContent gives us Range,
// If-Modified-Since, and ETag handling for free — the Subsonic spec requires
// Range support so clients can seek.
func (m *mediaHandlers) handleStream(w http.ResponseWriter, r *http.Request) {
m.serveTrack(w, r, false)
}
// handleDownload is stream with a filename and attachment disposition so
// browsers save rather than stream.
func (m *mediaHandlers) handleDownload(w http.ResponseWriter, r *http.Request) {
m.serveTrack(w, r, true)
}
func (m *mediaHandlers) serveTrack(w http.ResponseWriter, r *http.Request, attachment bool) {
idStr := r.URL.Query().Get("id")
if idStr == "" {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: id")
return
}
id, ok := parseUUID(idStr)
if !ok {
WriteFail(w, r, ErrDataNotFound, "Track not found")
return
}
q := dbq.New(m.pool)
track, err := q.GetTrackByID(r.Context(), id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
WriteFail(w, r, ErrDataNotFound, "Track not found")
return
}
WriteFail(w, r, ErrGeneric, "Failed to load track")
return
}
f, err := os.Open(track.FilePath)
if err != nil {
// File vanished between scan and now — treat as not found so clients
// can recover gracefully rather than assume a 5xx network blip.
WriteFail(w, r, ErrDataNotFound, "Track file not found")
return
}
defer func() { _ = f.Close() }()
info, err := f.Stat()
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to stat track file")
return
}
w.Header().Set("Content-Type", contentTypeForFormat(track.FileFormat))
w.Header().Set("Accept-Ranges", "bytes")
if attachment {
w.Header().Set("Content-Disposition",
fmt.Sprintf(`attachment; filename=%q`, filepath.Base(track.FilePath)))
}
http.ServeContent(w, r, filepath.Base(track.FilePath), info.ModTime(), f)
}
// handleGetCoverArt resolves an id (album, track, or cover-art id — all of
// which we key off the album UUID) to an image file. Scanner does not yet
// populate albums.cover_art_path, so the common path is the sidecar lookup.
func (m *mediaHandlers) handleGetCoverArt(w http.ResponseWriter, r *http.Request) {
idStr := r.URL.Query().Get("id")
if idStr == "" {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: id")
return
}
id, ok := parseUUID(idStr)
if !ok {
WriteFail(w, r, ErrDataNotFound, "Cover art not found")
return
}
q := dbq.New(m.pool)
if path := coverArtPath(r.Context(), q, id); path != "" {
serveImage(w, r, path)
return
}
WriteFail(w, r, ErrDataNotFound, "Cover art not found")
}
// coverArtPath is the image for a cover-art id: an album's cover, a song's
// album cover, or an artist's picture (coverart.ResolveArtistPath). "" when
// the id names none of them or there is no image.
func coverArtPath(ctx context.Context, q *dbq.Queries, id pgtype.UUID) string {
if album, err := q.GetAlbumByID(ctx, id); err == nil {
return resolveAlbumCoverPath(ctx, q, album)
}
if track, err := q.GetTrackByID(ctx, id); err == nil {
album, err := q.GetAlbumByID(ctx, track.AlbumID)
if err != nil {
return ""
}
return resolveAlbumCoverPath(ctx, q, album)
}
if artist, err := q.GetArtistByID(ctx, id); err == nil {
return coverart.ResolveArtistPath(ctx, q, artist)
}
return ""
}
// resolveAlbumCoverPath delegates to coverart.ResolveAlbumPath; kept as a
// local alias so the call sites in this file read naturally.
func resolveAlbumCoverPath(ctx context.Context, q *dbq.Queries, album dbq.Album) string {
return coverart.ResolveAlbumPath(ctx, q, album)
}
func serveImage(w http.ResponseWriter, r *http.Request, path string) {
f, err := os.Open(path)
if err != nil {
WriteFail(w, r, ErrDataNotFound, "Cover art not found")
return
}
defer func() { _ = f.Close() }()
info, err := f.Stat()
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to stat cover art")
return
}
w.Header().Set("Content-Type", 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): writes a play_started, leaves ended_at NULL.
// submission=true (completed): writes a synthetic completed play (start+end
// in one transaction). play_events WHERE ended_at IS NULL is the source of
// truth for "currently playing"; the prior in-memory nowPlayingMap is gone.
func (m *mediaHandlers) handleScrobble(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
idStr := params.Get("id")
if idStr == "" {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: id")
return
}
trackID, ok := parseUUID(idStr)
if !ok {
WriteFail(w, r, ErrDataNotFound, "Track not found")
return
}
user, ok := UserFromContext(r.Context())
if !ok {
WriteFail(w, r, ErrGeneric, "Unauthenticated")
return
}
at := time.Now().UTC()
if t := params.Get("time"); t != "" {
// Subsonic spec uses ms-since-epoch; some clients send ISO 8601.
// Accept both for resilience.
if ms, err := strconv.ParseInt(t, 10, 64); err == nil {
at = time.UnixMilli(ms).UTC()
} else if parsed, err := time.Parse(time.RFC3339, t); err == nil {
at = parsed
}
}
clientID := params.Get("c")
// Subsonic default is submission=true (completed play).
submission := strings.ToLower(params.Get("submission"))
switch submission {
case "true", "":
if err := m.events.RecordSyntheticCompletedPlay(r.Context(), user.ID, trackID, clientID, at); err != nil {
WriteFail(w, r, ErrGeneric, "Could not record play")
return
}
case "false":
if _, err := m.events.RecordPlayStarted(r.Context(), user.ID, trackID, clientID, at); err != nil {
WriteFail(w, r, ErrGeneric, "Could not record now-playing")
return
}
default:
// Unknown submission values: ack and ignore.
}
Write(w, r, struct{ Envelope }{Envelope: NewEnvelope("ok")})
}