diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index 6f08724..00efcc8 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -62,6 +62,7 @@ func main() { var confMusicPaths pathAliases flag.Var(&confMusicPaths, "music-path", "path to music") + defaultMusicPath := flag.String("default-music-path", "", "interval (in minutes) to automatically scan music (optional)") confPlaylistsPath := flag.String("playlists-path", "", "path to your list of new or existing m3u playlists that gonic can manage") @@ -256,7 +257,7 @@ func main() { if err != nil { log.Panicf("error creating admin controller: %v\n", err) } - ctrlSubsonic, err := ctrlsubsonic.New(dbc, scannr, musicPaths, *confPodcastPath, cacheDirAudio, cacheDirCovers, jukebx, playlistStore, scrobblers, podcast, transcoder, lastfmClient, artistInfoCache, albumInfoCache, tagReader, resolveProxyPath) + ctrlSubsonic, err := ctrlsubsonic.New(dbc, scannr, musicPaths, *defaultMusicPath, *confPodcastPath, cacheDirAudio, cacheDirCovers, jukebx, playlistStore, scrobblers, podcast, transcoder, lastfmClient, artistInfoCache, albumInfoCache, tagReader, resolveProxyPath) if err != nil { log.Panicf("error creating subsonic controller: %v\n", err) } diff --git a/server/ctrlsubsonic/ctrl.go b/server/ctrlsubsonic/ctrl.go index a548909..57571d1 100644 --- a/server/ctrlsubsonic/ctrl.go +++ b/server/ctrlsubsonic/ctrl.go @@ -53,44 +53,46 @@ type ProxyPathResolver func(in string) string type Controller struct { *http.ServeMux - dbc *db.DB - scanner *scanner.Scanner - musicPaths []MusicPath - podcastsPath string - cacheAudioPath string - cacheCoverPath string - jukebox *jukebox.Jukebox - playlistStore *playlist.Store - scrobblers []scrobble.Scrobbler - podcasts *podcast.Podcasts - transcoder transcode.Transcoder - lastFMClient *lastfm.Client - artistInfoCache *artistinfocache.ArtistInfoCache - albumInfoCache *albuminfocache.AlbumInfoCache - tagReader tags.Reader + dbc *db.DB + scanner *scanner.Scanner + musicPaths []MusicPath + defaultMusicPath string + podcastsPath string + cacheAudioPath string + cacheCoverPath string + jukebox *jukebox.Jukebox + playlistStore *playlist.Store + scrobblers []scrobble.Scrobbler + podcasts *podcast.Podcasts + transcoder transcode.Transcoder + lastFMClient *lastfm.Client + artistInfoCache *artistinfocache.ArtistInfoCache + albumInfoCache *albuminfocache.AlbumInfoCache + tagReader tags.Reader resolveProxyPath ProxyPathResolver } -func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, podcastsPath string, cacheAudioPath string, cacheCoverPath string, jukebox *jukebox.Jukebox, playlistStore *playlist.Store, scrobblers []scrobble.Scrobbler, podcasts *podcast.Podcasts, transcoder transcode.Transcoder, lastFMClient *lastfm.Client, artistInfoCache *artistinfocache.ArtistInfoCache, albumInfoCache *albuminfocache.AlbumInfoCache, tagReader tags.Reader, resolveProxyPath ProxyPathResolver) (*Controller, error) { +func New(dbc *db.DB, scannr *scanner.Scanner, musicPaths []MusicPath, defaultMusicPath string, podcastsPath string, cacheAudioPath string, cacheCoverPath string, jukebox *jukebox.Jukebox, playlistStore *playlist.Store, scrobblers []scrobble.Scrobbler, podcasts *podcast.Podcasts, transcoder transcode.Transcoder, lastFMClient *lastfm.Client, artistInfoCache *artistinfocache.ArtistInfoCache, albumInfoCache *albuminfocache.AlbumInfoCache, tagReader tags.Reader, resolveProxyPath ProxyPathResolver) (*Controller, error) { c := Controller{ ServeMux: http.NewServeMux(), - dbc: dbc, - scanner: scannr, - musicPaths: musicPaths, - podcastsPath: podcastsPath, - cacheAudioPath: cacheAudioPath, - cacheCoverPath: cacheCoverPath, - jukebox: jukebox, - playlistStore: playlistStore, - scrobblers: scrobblers, - podcasts: podcasts, - transcoder: transcoder, - lastFMClient: lastFMClient, - artistInfoCache: artistInfoCache, - albumInfoCache: albumInfoCache, - tagReader: tagReader, + dbc: dbc, + scanner: scannr, + musicPaths: musicPaths, + defaultMusicPath: defaultMusicPath, + podcastsPath: podcastsPath, + cacheAudioPath: cacheAudioPath, + cacheCoverPath: cacheCoverPath, + jukebox: jukebox, + playlistStore: playlistStore, + scrobblers: scrobblers, + podcasts: podcasts, + transcoder: transcoder, + lastFMClient: lastFMClient, + artistInfoCache: artistInfoCache, + albumInfoCache: albumInfoCache, + tagReader: tagReader, resolveProxyPath: resolveProxyPath, } diff --git a/server/ctrlsubsonic/handlers_by_folder.go b/server/ctrlsubsonic/handlers_by_folder.go index b6efc54..a1be750 100644 --- a/server/ctrlsubsonic/handlers_by_folder.go +++ b/server/ctrlsubsonic/handlers_by_folder.go @@ -25,7 +25,7 @@ func (c *Controller) ServeGetIndexes(r *http.Request) *spec.Response { Select("id"). Model(&db.Album{}). Where("parent_id IS NULL") - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { rootQ = rootQ. Where("root_dir=?", m) } @@ -170,7 +170,7 @@ func (c *Controller) ServeGetAlbumList(r *http.Request) *spec.Response { return spec.NewError(10, "unknown value %q for parameter 'type'", v) } - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("root_dir=?", m) } var folders []*db.Album @@ -220,7 +220,7 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response { Select("id"). Model(&db.Album{}). Where("parent_id IS NULL") - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { rootQ = rootQ.Where("root_dir=?", m) } @@ -261,7 +261,7 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response { Preload("AlbumRating", "user_id=?", user.ID). Offset(params.GetOrInt("albumOffset", 0)). Limit(params.GetOrInt("albumCount", 20)) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("root_dir=?", m) } if err := q.Find(&albums).Error; err != nil { @@ -287,7 +287,7 @@ func (c *Controller) ServeSearchTwo(r *http.Request) *spec.Response { Preload("TrackRating", "user_id=?", user.ID). Offset(params.GetOrInt("songOffset", 0)). Limit(params.GetOrInt("songCount", 20)) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q. Joins("JOIN albums ON albums.id=tracks.album_id"). Where("albums.root_dir=?", m) @@ -320,44 +320,56 @@ func (c *Controller) ServeGetStarred(r *http.Request) *spec.Response { results := &spec.Starred{} // "artists" - rootQ := c.dbc. - Select("id"). - Model(&db.Album{}). - Where("parent_id IS NULL") - if m := getMusicFolder(c.musicPaths, params); m != "" { - rootQ = rootQ.Where("root_dir=?", m) - } - - var artists []*db.Album + var artists []*db.Artist q := c.dbc. - Where(`parent_id IN ?`, rootQ.SubQuery()). - Joins("JOIN album_stars ON albums.id=album_stars.album_id"). - Where("album_stars.user_id=?", user.ID). - Preload("AlbumStar", "user_id=?", user.ID). - Preload("AlbumRating", "user_id=?", user.ID) + Select("artists.*, count(albums.id) album_count"). + Joins("JOIN artist_stars ON artist_stars.artist_id=artists.id"). + Where("artist_stars.user_id=?", user.ID). + Joins("JOIN artist_appearances ON artist_appearances.artist_id=artists.id"). + Joins("JOIN albums ON albums.id=artist_appearances.album_id"). + Order("artist_stars.star_date DESC"). + Preload("ArtistStar", "user_id=?", user.ID). + Preload("ArtistRating", "user_id=?", user.ID). + Preload("Info"). + Group("artists.id") + if m := getMusicFolder(c, params); m != "" { + q = q.Where("albums.root_dir=?", m) + } if err := q.Find(&artists).Error; err != nil { return spec.NewError(0, "find artists: %v", err) } for _, a := range artists { - results.Artists = append(results.Artists, spec.NewDirectoryByFolder(a, nil)) + results.Artists = append(results.Artists, spec.NewArtistByTags(a)) } // "albums" var albums []*db.Album - q = c.dbc. - Joins("JOIN album_artists ON album_artists.album_id=albums.id"). - Joins("JOIN album_stars ON albums.id=album_stars.album_id"). - Where("album_stars.user_id=?", user.ID). + q = c.dbc.Where("parent_id IS NOT NULL"). + Joins("JOIN album_stars ON albums.id=album_stars.album_id AND album_stars.user_id=?", user.ID) + q = q.Order("album_stars.star_date DESC") + + q = q. Preload("AlbumStar", "user_id=?", user.ID). Preload("AlbumRating", "user_id=?", user.ID) - if m := getMusicFolder(c.musicPaths, params); m != "" { + + if m := getMusicFolder(c, params); m != "" { q = q.Where("root_dir=?", m) } + + q. + Select("albums.*, count(tracks.id) child_count, sum(tracks.length) duration"). + Joins("LEFT JOIN tracks ON tracks.album_id=albums.id"). + Group("albums.id"). + Joins("JOIN album_artists ON album_artists.album_id=albums.id"). + Preload("Parent"). + Preload("AlbumStar", "user_id=?", user.ID). + Preload("AlbumRating", "user_id=?", user.ID) + if err := q.Find(&albums).Error; err != nil { return spec.NewError(0, "find albums: %v", err) } for _, a := range albums { - results.Albums = append(results.Albums, spec.NewTCAlbumByFolder(a)) + results.Albums = append(results.Albums, spec.NewAlbumByFolder(a)) } // tracks @@ -369,7 +381,7 @@ func (c *Controller) ServeGetStarred(r *http.Request) *spec.Response { Preload("Artists"). Preload("TrackStar", "user_id=?", user.ID). Preload("TrackRating", "user_id=?", user.ID) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q. Joins("JOIN albums ON albums.id=tracks.album_id"). Where("albums.root_dir=?", m) diff --git a/server/ctrlsubsonic/handlers_by_tags.go b/server/ctrlsubsonic/handlers_by_tags.go index 5781a34..1111555 100644 --- a/server/ctrlsubsonic/handlers_by_tags.go +++ b/server/ctrlsubsonic/handlers_by_tags.go @@ -28,12 +28,13 @@ func (c *Controller) ServeGetArtists(r *http.Request) *spec.Response { q := c.dbc. Select("*, count(album_artists.album_id) album_count"). Joins("JOIN album_artists ON album_artists.artist_id=artists.id"). + Joins("LEFT JOIN artist_stars ON artist_stars.artist_id=artists.id"). Preload("ArtistStar", "user_id=?", user.ID). Preload("ArtistRating", "user_id=?", user.ID). Preload("Info"). Group("artists.id"). Order("artists.name COLLATE NOCASE") - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q. Joins("JOIN albums ON albums.id=album_artists.album_id"). Where("albums.root_dir=?", m) @@ -186,7 +187,7 @@ func (c *Controller) ServeGetAlbumListTwo(r *http.Request) *spec.Response { default: return spec.NewError(10, "unknown value %q for parameter 'type'", listType) } - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("root_dir=?", m) } var albums []*db.Album @@ -254,7 +255,7 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response { Preload("Info"). Offset(params.GetOrInt("artistOffset", 0)). Limit(params.GetOrInt("artistCount", 20)) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("albums.root_dir=?", m) } if err := q.Find(&artists).Error; err != nil { @@ -283,7 +284,7 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response { q = q. Offset(params.GetOrInt("albumOffset", 0)). Limit(params.GetOrInt("albumCount", 20)) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("root_dir=?", m) } if err := q.Find(&albums).Error; err != nil { @@ -311,7 +312,7 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response { } q = q.Offset(params.GetOrInt("songOffset", 0)). Limit(params.GetOrInt("songCount", 20)) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q. Joins("JOIN albums ON albums.id=tracks.album_id"). Where("albums.root_dir=?", m) @@ -482,7 +483,7 @@ func (c *Controller) ServeGetSongsByGenre(r *http.Request) *spec.Response { Preload("TrackRating", "user_id=?", user.ID). Offset(params.GetOrInt("offset", 0)). Limit(params.GetOrInt("count", 10)) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("albums.root_dir=?", m) } q = q.Group("tracks.id") @@ -523,7 +524,7 @@ func (c *Controller) ServeGetStarredTwo(r *http.Request) *spec.Response { Preload("ArtistRating", "user_id=?", user.ID). Preload("Info"). Group("artists.id") - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("albums.root_dir=?", m) } if err := q.Find(&artists).Error; err != nil { @@ -544,7 +545,7 @@ func (c *Controller) ServeGetStarredTwo(r *http.Request) *spec.Response { Preload("AlbumStar", "user_id=?", user.ID). Preload("AlbumRating", "user_id=?", user.ID). Preload("Play", "user_id=?", user.ID) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("albums.root_dir=?", m) } if err := q.Find(&albums).Error; err != nil { @@ -565,7 +566,7 @@ func (c *Controller) ServeGetStarredTwo(r *http.Request) *spec.Response { Preload("Artists"). Preload("TrackStar", "user_id=?", user.ID). Preload("TrackRating", "user_id=?", user.ID) - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q. Joins("JOIN albums ON albums.id=tracks.album_id"). Where("albums.root_dir=?", m) diff --git a/server/ctrlsubsonic/handlers_common.go b/server/ctrlsubsonic/handlers_common.go index 7448128..df43439 100644 --- a/server/ctrlsubsonic/handlers_common.go +++ b/server/ctrlsubsonic/handlers_common.go @@ -334,7 +334,7 @@ func (c *Controller) ServeGetRandomSongs(r *http.Request) *spec.Response { q = q.Joins("JOIN track_genres ON track_genres.track_id=tracks.id") q = q.Joins("JOIN genres ON genres.id=track_genres.genre_id AND genres.name=?", genre) } - if m := getMusicFolder(c.musicPaths, params); m != "" { + if m := getMusicFolder(c, params); m != "" { q = q.Where("albums.root_dir=?", m) } if err := q.Find(&tracks).Error; err != nil { @@ -777,10 +777,11 @@ func scrobbleStatsUpdatePodcastEpisode(dbc *db.DB, peID int) error { return nil } -func getMusicFolder(musicPaths []MusicPath, p params.Params) string { +func getMusicFolder(c *Controller, p params.Params) string { + musicPaths := c.musicPaths idx, err := p.GetInt("musicFolderId") if err != nil { - return "" + return c.defaultMusicPath } if idx < 0 || idx >= len(musicPaths) { return os.DevNull diff --git a/server/ctrlsubsonic/spec/construct_by_folder.go b/server/ctrlsubsonic/spec/construct_by_folder.go index aaba0ab..99c1295 100644 --- a/server/ctrlsubsonic/spec/construct_by_folder.go +++ b/server/ctrlsubsonic/spec/construct_by_folder.go @@ -6,14 +6,22 @@ import ( "sort" "go.senan.xyz/gonic/db" + "go.senan.xyz/gonic/server/ctrlsubsonic/specid" ) func NewAlbumByFolder(f *db.Album) *Album { + artist := "" + var parentSID *specid.ID + if f.Parent != nil { + artist = f.Parent.RightPath + parentSID = f.ParentSID() + } + a := &Album{ - Artist: f.Parent.RightPath, + Artist: artist, ID: f.SID(), IsDir: true, - ParentID: f.ParentSID(), + ParentID: parentSID, Album: f.RightPath, Name: f.RightPath, Title: f.RightPath, diff --git a/server/ctrlsubsonic/spec/spec.go b/server/ctrlsubsonic/spec/spec.go index 27fe1ca..e70e47f 100644 --- a/server/ctrlsubsonic/spec/spec.go +++ b/server/ctrlsubsonic/spec/spec.go @@ -442,8 +442,8 @@ type Bookmark struct { } type Starred struct { - Artists []*Directory `xml:"artist,omitempty" json:"artist,omitempty"` - Albums []*TrackChild `xml:"album,omitempty" json:"album,omitempty"` + Artists []*Artist `xml:"artist,omitempty" json:"artist,omitempty"` + Albums []*Album `xml:"album,omitempty" json:"album,omitempty"` Tracks []*TrackChild `xml:"song,omitempty" json:"song,omitempty"` }