package subsonic import ( "context" "errors" "net/http" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" ) // Folder browsing is the older half of the Subsonic API, which apps such as // DSub use unless told to browse by tags. Its folders are Minstrel's artists // and albums, not directories on disk: getIndexes lists the artists, // getMusicDirectory opens an artist to its albums and an album to its songs, // and getAlbumList and search2 show albums as folders to open. The ids are // the same as the ID3 calls use, so starring, streaming and cover art take // them unchanged. // Directory is the body of getMusicDirectory. type Directory struct { ID string `json:"id" xml:"id,attr"` Parent string `json:"parent,omitempty" xml:"parent,attr,omitempty"` Name string `json:"name" xml:"name,attr"` Children []SongRef `json:"child" xml:"child"` } type DirectoryResponse struct { Envelope Directory Directory `json:"directory" xml:"directory"` } // AlbumListContainer is the body of getAlbumList: albums as folders. type AlbumListContainer struct { Albums []SongRef `json:"album" xml:"album"` } type AlbumListResponse struct { Envelope AlbumList AlbumListContainer `json:"albumList" xml:"albumList"` } // SearchResult2Container is the body of search2: search3's hits, with the // albums as folders. type SearchResult2Container struct { Artists []ArtistRef `json:"artist" xml:"artist"` Albums []SongRef `json:"album" xml:"album"` Songs []SongRef `json:"song" xml:"song"` } type SearchResult2Response struct { Envelope SearchResult2 SearchResult2Container `json:"searchResult2" xml:"searchResult2"` } // albumFolder is an album as a folder inside its artist's. func albumFolder(a dbq.Album, artistName string) SongRef { return SongRef{ ID: uuidToID(a.ID), Parent: uuidToID(a.ArtistID), Title: a.Title, Album: a.Title, AlbumID: uuidToID(a.ID), Artist: artistName, ArtistID: uuidToID(a.ArtistID), CoverArt: coverArtID(a), Year: yearFromDate(a.ReleaseDate), Created: timestamptzToRFC3339(a.CreatedAt), IsDir: true, } } // albumFolders renders albums as folders, each under its own artist. func albumFolders(ctx context.Context, q *dbq.Queries, albums []dbq.Album) ([]SongRef, error) { out := make([]SongRef, 0, len(albums)) if len(albums) == 0 { return out, nil } ids := make([]pgtype.UUID, 0, len(albums)) for _, a := range albums { ids = append(ids, a.ArtistID) } names, err := resolveArtistNames(ctx, q, ids) if err != nil { return nil, err } for _, a := range albums { out = append(out, albumFolder(a, names[uuidToID(a.ArtistID)])) } return out, nil } // getMusicDirectory opens a folder: an artist id gives the artist's albums, // an album id gives the album's songs. func (b *browseHandlers) getMusicDirectory(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, "Directory not found") return } q := dbq.New(b.pool) ctx := r.Context() artist, err := q.GetArtistByID(ctx, id) switch { case err == nil: albums, err := q.ListAlbumsByArtist(ctx, artist.ID) if err != nil { WriteFail(w, r, ErrGeneric, "Failed to load albums") return } children := make([]SongRef, 0, len(albums)) for _, a := range albums { children = append(children, albumFolder(a, artist.Name)) } Write(w, r, DirectoryResponse{ Envelope: NewEnvelope("ok"), Directory: Directory{ID: uuidToID(artist.ID), Name: artist.Name, Children: children}, }) return case !errors.Is(err, pgx.ErrNoRows): WriteFail(w, r, ErrGeneric, "Failed to load directory") return } album, err := q.GetAlbumByID(ctx, id) if err != nil { if errors.Is(err, pgx.ErrNoRows) { WriteFail(w, r, ErrDataNotFound, "Directory not found") return } WriteFail(w, r, ErrGeneric, "Failed to load directory") return } albumArtist, err := q.GetArtistByID(ctx, album.ArtistID) if err != nil { WriteFail(w, r, ErrGeneric, "Failed to load album artist") return } songs, err := albumSongs(ctx, q, album, albumArtist.Name) if err != nil { WriteFail(w, r, ErrGeneric, "Failed to load tracks") return } Write(w, r, DirectoryResponse{ Envelope: NewEnvelope("ok"), Directory: Directory{ ID: uuidToID(album.ID), Parent: uuidToID(album.ArtistID), Name: album.Title, Children: songs, }, }) } // getAlbumList is getAlbumList2 with the albums as folders: the same list // types, paged the same way (listAlbums). func (b *browseHandlers) getAlbumList(w http.ResponseWriter, r *http.Request) { q := dbq.New(b.pool) albums, ok := b.listAlbums(w, r, q) if !ok { return } folders, err := albumFolders(r.Context(), q, albums) if err != nil { WriteFail(w, r, ErrGeneric, "Failed to resolve albums") return } Write(w, r, AlbumListResponse{ Envelope: NewEnvelope("ok"), AlbumList: AlbumListContainer{Albums: folders}, }) } // search2 is search3 with the albums as folders (search). func (b *browseHandlers) search2(w http.ResponseWriter, r *http.Request) { q := dbq.New(b.pool) hits, ok := b.search(w, r, q) if !ok { return } albums, err := albumFolders(r.Context(), q, hits.albums) if err != nil { WriteFail(w, r, ErrGeneric, "Album search failed") return } Write(w, r, SearchResult2Response{ Envelope: NewEnvelope("ok"), SearchResult2: SearchResult2Container{ Artists: hits.artists, Albums: albums, Songs: hits.songs, }, }) }