Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
244 lines
6.3 KiB
Go
244 lines
6.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/catalog"
|
|
)
|
|
|
|
// catalogHandlers serve the read-only catalog to any signed-in user. No file
|
|
// path, library path or directory is ever part of these responses.
|
|
type catalogHandlers struct{ svc catalogService }
|
|
|
|
type artistRefJSON struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type albumRefJSON struct {
|
|
ID int64 `json:"id"`
|
|
Title string `json:"title"`
|
|
}
|
|
|
|
type artistJSON struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
AlbumCount int `json:"album_count"`
|
|
TrackCount int `json:"track_count"`
|
|
}
|
|
|
|
type albumJSON struct {
|
|
ID int64 `json:"id"`
|
|
Title string `json:"title"`
|
|
Artist *artistRefJSON `json:"artist"`
|
|
TrackCount int `json:"track_count"`
|
|
DurationMs int64 `json:"duration_ms"`
|
|
}
|
|
|
|
type trackJSON struct {
|
|
ID int64 `json:"id"`
|
|
Title string `json:"title"`
|
|
DurationMs int64 `json:"duration_ms"`
|
|
TrackNo int `json:"track_no"`
|
|
DiscNo int `json:"disc_no"`
|
|
Album *albumRefJSON `json:"album"`
|
|
Artists []artistRefJSON `json:"artists"`
|
|
Format string `json:"format"`
|
|
// What the first playable file says about its loudness: null when it says nothing.
|
|
ReplayGain *replayGainJSON `json:"replay_gain"`
|
|
}
|
|
|
|
// replayGainJSON is the ReplayGain of a track. A part the file does not say is null.
|
|
type replayGainJSON struct {
|
|
TrackGainDB *float64 `json:"track_gain_db"`
|
|
AlbumGainDB *float64 `json:"album_gain_db"`
|
|
TrackPeak *float64 `json:"track_peak"`
|
|
AlbumPeak *float64 `json:"album_peak"`
|
|
}
|
|
|
|
func toArtistJSON(a catalog.Artist) artistJSON {
|
|
return artistJSON{ID: a.ID, Name: a.Name, AlbumCount: a.AlbumCount, TrackCount: a.TrackCount}
|
|
}
|
|
|
|
func toAlbumJSON(a catalog.Album) albumJSON {
|
|
out := albumJSON{ID: a.ID, Title: a.Title, TrackCount: a.TrackCount, DurationMs: a.DurationMs}
|
|
if a.Artist != nil {
|
|
out.Artist = &artistRefJSON{ID: a.Artist.ID, Name: a.Artist.Name}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func toTrackJSON(t catalog.Track) trackJSON {
|
|
out := trackJSON{
|
|
ID: t.ID, Title: t.Title, DurationMs: t.DurationMs, TrackNo: t.TrackNo, DiscNo: t.DiscNo,
|
|
Format: t.Format, Artists: make([]artistRefJSON, len(t.Artists)),
|
|
}
|
|
if t.Album != nil {
|
|
out.Album = &albumRefJSON{ID: t.Album.ID, Title: t.Album.Title}
|
|
}
|
|
for i, a := range t.Artists {
|
|
out.Artists[i] = artistRefJSON{ID: a.ID, Name: a.Name}
|
|
}
|
|
if g := t.ReplayGain; g != nil {
|
|
out.ReplayGain = &replayGainJSON{TrackGainDB: g.TrackGain, AlbumGainDB: g.AlbumGain, TrackPeak: g.TrackPeak, AlbumPeak: g.AlbumPeak}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// parseFilter reads q, limit, offset and the id filters from the query string.
|
|
// Unset values stay zero so the service can apply its defaults.
|
|
func parseFilter(c *gin.Context) (catalog.Filter, bool) {
|
|
f := catalog.Filter{Query: c.Query("q"), Sort: c.Query("sort")}
|
|
for _, p := range []struct {
|
|
name string
|
|
dst *int64
|
|
}{
|
|
{"library_id", &f.LibraryID}, {"artist_id", &f.ArtistID}, {"album_id", &f.AlbumID},
|
|
} {
|
|
v, ok := queryInt(c, p.name)
|
|
if !ok {
|
|
return f, false
|
|
}
|
|
*p.dst = v
|
|
}
|
|
limit, ok := queryInt(c, "limit")
|
|
if !ok {
|
|
return f, false
|
|
}
|
|
offset, ok := queryInt(c, "offset")
|
|
if !ok {
|
|
return f, false
|
|
}
|
|
// Clamp before narrowing, so a huge number cannot wrap into a small one.
|
|
f.Limit, f.Offset = clampInt(limit), clampInt(offset)
|
|
return f, true
|
|
}
|
|
|
|
// queryInt reads an optional integer parameter; it answers 400 itself when the
|
|
// value is not a number.
|
|
func queryInt(c *gin.Context, name string) (int64, bool) {
|
|
raw := c.Query(name)
|
|
if raw == "" {
|
|
return 0, true
|
|
}
|
|
n, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil {
|
|
writeError(c, http.StatusBadRequest, "invalid_input", name+" must be a whole number")
|
|
return 0, false
|
|
}
|
|
return n, true
|
|
}
|
|
|
|
// clampInt narrows n to an int without wrapping, also on 32-bit systems.
|
|
func clampInt(n int64) int {
|
|
switch {
|
|
case n > math.MaxInt32:
|
|
return math.MaxInt32
|
|
case n < -math.MaxInt32:
|
|
return -math.MaxInt32
|
|
}
|
|
return int(n)
|
|
}
|
|
|
|
func writeCatalogError(c *gin.Context, err error, notFoundCode string) {
|
|
switch {
|
|
case errors.Is(err, catalog.ErrInvalidQuery):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
case errors.Is(err, catalog.ErrNotFound):
|
|
writeError(c, http.StatusNotFound, notFoundCode, "not found")
|
|
default:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
}
|
|
}
|
|
|
|
func writePage[T any, J any](c *gin.Context, p catalog.Page[T], conv func(T) J) {
|
|
items := make([]J, len(p.Items))
|
|
for i, it := range p.Items {
|
|
items[i] = conv(it)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"items": items, "total": p.Total, "limit": p.Limit, "offset": p.Offset})
|
|
}
|
|
|
|
func (h *catalogHandlers) listArtists(c *gin.Context) {
|
|
f, ok := parseFilter(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
p, err := h.svc.Artists(c.Request.Context(), f)
|
|
if err != nil {
|
|
writeCatalogError(c, err, "artist_not_found")
|
|
return
|
|
}
|
|
writePage(c, p, toArtistJSON)
|
|
}
|
|
|
|
func (h *catalogHandlers) listAlbums(c *gin.Context) {
|
|
f, ok := parseFilter(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
p, err := h.svc.Albums(c.Request.Context(), f)
|
|
if err != nil {
|
|
writeCatalogError(c, err, "album_not_found")
|
|
return
|
|
}
|
|
writePage(c, p, toAlbumJSON)
|
|
}
|
|
|
|
func (h *catalogHandlers) listTracks(c *gin.Context) {
|
|
f, ok := parseFilter(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
p, err := h.svc.Tracks(c.Request.Context(), f)
|
|
if err != nil {
|
|
writeCatalogError(c, err, "track_not_found")
|
|
return
|
|
}
|
|
writePage(c, p, toTrackJSON)
|
|
}
|
|
|
|
func (h *catalogHandlers) getArtist(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
a, err := h.svc.Artist(c.Request.Context(), id)
|
|
if err != nil {
|
|
writeCatalogError(c, err, "artist_not_found")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toArtistJSON(a))
|
|
}
|
|
|
|
func (h *catalogHandlers) getAlbum(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
a, err := h.svc.Album(c.Request.Context(), id)
|
|
if err != nil {
|
|
writeCatalogError(c, err, "album_not_found")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toAlbumJSON(a))
|
|
}
|
|
|
|
func (h *catalogHandlers) getTrack(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
t, err := h.svc.Track(c.Request.Context(), id)
|
|
if err != nil {
|
|
writeCatalogError(c, err, "track_not_found")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toTrackJSON(t))
|
|
}
|