sonda/internal/opensubsonic/response.go
tgiachi 1ae8e77d81 feat: OpenSubsonic core, login with a device password
/rest speaks the OpenSubsonic API (Subsonic 1.16.1) so that other apps can browse
the music of a server. A call logs in with the password of a device: an apiKey,
Subsonic's token (u, t, s) or the password itself (u, p). The account password is
never accepted there.

- devices: ByKey, ByToken, ByPassword and the note of last use
- opensubsonic package: JSON and XML answers, ping, getLicense,
  getOpenSubsonicExtensions, tokenInfo, getMusicFolders, getArtists, getArtist,
  getAlbum, getSong, getGenres
- storage.SubsonicRepo: year, genres, size, bitrate, suffix and added date
2026-11-16 09:32:11 +01:00

188 lines
7.9 KiB
Go

package opensubsonic
import "encoding/xml"
// The answer of every call is one Response, written as JSON or as XML: the same tree has the tags of both. In XML
// the plain values are attributes and the lists are child elements; in JSON everything is a field and the lists are
// arrays. A field that is not set is left out of both.
// APIVersion is the Subsonic API that is spoken.
const APIVersion = "1.16.1"
// Response is the envelope of an answer.
type Response struct {
XMLName xml.Name `xml:"subsonic-response" json:"-"`
Xmlns string `xml:"xmlns,attr" json:"-"`
Status string `xml:"status,attr" json:"status"`
Version string `xml:"version,attr" json:"version"`
Type string `xml:"type,attr" json:"type"`
ServerVersion string `xml:"serverVersion,attr" json:"serverVersion"`
OpenSubsonic bool `xml:"openSubsonic,attr" json:"openSubsonic"`
Error *Error `xml:"error" json:"error,omitempty"`
License *License `xml:"license" json:"license,omitempty"`
OpenSubsonicExtensions []Extension `xml:"openSubsonicExtensions" json:"openSubsonicExtensions,omitempty"`
TokenInfo *TokenInfo `xml:"tokenInfo" json:"tokenInfo,omitempty"`
MusicFolders *MusicFolders `xml:"musicFolders" json:"musicFolders,omitempty"`
Artists *ArtistsID3 `xml:"artists" json:"artists,omitempty"`
Artist *ArtistWithAlbums `xml:"artist" json:"artist,omitempty"`
Album *AlbumWithSongs `xml:"album" json:"album,omitempty"`
Song *Child `xml:"song" json:"song,omitempty"`
Genres *Genres `xml:"genres" json:"genres,omitempty"`
}
// Error codes of the Subsonic API.
const (
ErrGeneric = 0
ErrMissingParam = 10
ErrWrongLogin = 40
ErrAuthConflict = 43
ErrBadAPIKey = 44
ErrNotAuthorized = 50
ErrNotFound = 70
errUnsupportedAuth = 42
)
// Error is the reason a call failed.
type Error struct {
Code int `xml:"code,attr" json:"code"`
Message string `xml:"message,attr,omitempty" json:"message,omitempty"`
}
// License says the server is licensed: it always is.
type License struct {
Valid bool `xml:"valid,attr" json:"valid"`
}
// Extension is an OpenSubsonic extension that the server speaks.
type Extension struct {
Name string `xml:"name,attr" json:"name"`
Versions []int `xml:"versions" json:"versions"`
}
// TokenInfo says whose the login of the call is.
type TokenInfo struct {
Username string `xml:"username,attr" json:"username"`
}
// MusicFolders are the libraries.
type MusicFolders struct {
MusicFolder []MusicFolder `xml:"musicFolder" json:"musicFolder"`
}
// MusicFolder is one library.
type MusicFolder struct {
ID int64 `xml:"id,attr" json:"id"`
Name string `xml:"name,attr" json:"name"`
}
// ArtistsID3 are the artists, in groups by their first letter.
type ArtistsID3 struct {
IgnoredArticles string `xml:"ignoredArticles,attr" json:"ignoredArticles"`
Index []Index `xml:"index" json:"index"`
}
// Index is the artists whose name starts with one letter.
type Index struct {
Name string `xml:"name,attr" json:"name"`
Artist []ArtistID3 `xml:"artist" json:"artist"`
}
// ArtistID3 is an artist.
type ArtistID3 struct {
ID string `xml:"id,attr" json:"id"`
Name string `xml:"name,attr" json:"name"`
AlbumCount int `xml:"albumCount,attr" json:"albumCount"`
CoverArt string `xml:"coverArt,attr,omitempty" json:"coverArt,omitempty"`
}
// ArtistWithAlbums is an artist and its albums.
type ArtistWithAlbums struct {
ArtistID3
Album []AlbumID3 `xml:"album" json:"album,omitempty"`
}
// ArtistRef is an artist credited on a song or an album, in the OpenSubsonic fields.
type ArtistRef struct {
ID string `xml:"id,attr" json:"id"`
Name string `xml:"name,attr" json:"name"`
}
// ItemGenre is a genre of a song or an album, in the OpenSubsonic fields.
type ItemGenre struct {
Name string `xml:"name,attr" json:"name"`
}
// AlbumID3 is an album.
type AlbumID3 struct {
ID string `xml:"id,attr" json:"id"`
Name string `xml:"name,attr" json:"name"`
Artist string `xml:"artist,attr,omitempty" json:"artist,omitempty"`
ArtistID string `xml:"artistId,attr,omitempty" json:"artistId,omitempty"`
CoverArt string `xml:"coverArt,attr,omitempty" json:"coverArt,omitempty"`
SongCount int `xml:"songCount,attr" json:"songCount"`
Duration int `xml:"duration,attr" json:"duration"`
Created string `xml:"created,attr" json:"created"`
Year int `xml:"year,attr,omitempty" json:"year,omitempty"`
Genre string `xml:"genre,attr,omitempty" json:"genre,omitempty"`
DisplayArtist string `xml:"displayArtist,attr,omitempty" json:"displayArtist,omitempty"`
Artists []ArtistRef `xml:"artists" json:"artists,omitempty"`
Genres []ItemGenre `xml:"genres" json:"genres,omitempty"`
}
// AlbumWithSongs is an album and its songs.
type AlbumWithSongs struct {
AlbumID3
Song []Child `xml:"song" json:"song,omitempty"`
}
// ReplayGain is how loud a song is, as its file says.
type ReplayGain struct {
TrackGain *float64 `xml:"trackGain,attr,omitempty" json:"trackGain,omitempty"`
AlbumGain *float64 `xml:"albumGain,attr,omitempty" json:"albumGain,omitempty"`
TrackPeak *float64 `xml:"trackPeak,attr,omitempty" json:"trackPeak,omitempty"`
AlbumPeak *float64 `xml:"albumPeak,attr,omitempty" json:"albumPeak,omitempty"`
}
// Child is a song.
type Child struct {
ID string `xml:"id,attr" json:"id"`
Parent string `xml:"parent,attr,omitempty" json:"parent,omitempty"`
IsDir bool `xml:"isDir,attr" json:"isDir"`
Title string `xml:"title,attr" json:"title"`
Album string `xml:"album,attr,omitempty" json:"album,omitempty"`
Artist string `xml:"artist,attr,omitempty" json:"artist,omitempty"`
Track int `xml:"track,attr,omitempty" json:"track,omitempty"`
Year int `xml:"year,attr,omitempty" json:"year,omitempty"`
Genre string `xml:"genre,attr,omitempty" json:"genre,omitempty"`
CoverArt string `xml:"coverArt,attr,omitempty" json:"coverArt,omitempty"`
Size int64 `xml:"size,attr,omitempty" json:"size,omitempty"`
ContentType string `xml:"contentType,attr,omitempty" json:"contentType,omitempty"`
Suffix string `xml:"suffix,attr,omitempty" json:"suffix,omitempty"`
Duration int `xml:"duration,attr,omitempty" json:"duration,omitempty"`
BitRate int `xml:"bitRate,attr,omitempty" json:"bitRate,omitempty"`
Path string `xml:"path,attr,omitempty" json:"path,omitempty"`
DiscNumber int `xml:"discNumber,attr,omitempty" json:"discNumber,omitempty"`
Created string `xml:"created,attr,omitempty" json:"created,omitempty"`
AlbumID string `xml:"albumId,attr,omitempty" json:"albumId,omitempty"`
ArtistID string `xml:"artistId,attr,omitempty" json:"artistId,omitempty"`
Type string `xml:"type,attr,omitempty" json:"type,omitempty"`
MediaType string `xml:"mediaType,attr,omitempty" json:"mediaType,omitempty"`
IsVideo bool `xml:"isVideo,attr" json:"isVideo"`
DisplayArtist string `xml:"displayArtist,attr,omitempty" json:"displayArtist,omitempty"`
Artists []ArtistRef `xml:"artists" json:"artists,omitempty"`
Genres []ItemGenre `xml:"genres" json:"genres,omitempty"`
ReplayGain *ReplayGain `xml:"replayGain" json:"replayGain,omitempty"`
}
// Genres are the genres of the music.
type Genres struct {
Genre []GenreEntry `xml:"genre" json:"genre"`
}
// GenreEntry is a genre, with how many songs and albums have it.
type GenreEntry struct {
Value string `xml:",chardata" json:"value"`
SongCount int `xml:"songCount,attr" json:"songCount"`
AlbumCount int `xml:"albumCount,attr" json:"albumCount"`
}