Skip to content

Commit d69df71

Browse files
committed
refactor(specid): have Lookup return *specid.ID
1 parent 9407450 commit d69df71

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

server/ctrlsubsonic/handlers_bookmark.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func (c *Controller) ServeGetBookmarks(r *http.Request) *spec.Response {
4242
var track db.Track
4343
err := c.dbc.
4444
Preload("Album").
45+
Preload("Album.Artists").
46+
Preload("Artists").
4547
Find(&track, "id=?", bookmark.EntryID).
4648
Error
4749
if err != nil {

server/ctrlsubsonic/handlers_common.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,29 @@ func (c *Controller) ServeJukebox(r *http.Request) *spec.Response { // nolint:go
391391
return nil, fmt.Errorf("get playlist: %w", err)
392392
}
393393
for _, path := range playlist {
394-
file, err := specidpaths.Lookup(c.dbc, MusicPaths(c.musicPaths), c.podcastsPath, path)
394+
id, err := specidpaths.Lookup(c.dbc, MusicPaths(c.musicPaths), c.podcastsPath, path)
395395
if err != nil {
396396
return nil, fmt.Errorf("fetch track: %w", err)
397397
}
398-
switch f := file.(type) {
399-
case *db.Track:
400-
ret = append(ret, spec.NewTrackByTags(f, f.Album))
401-
case *db.InternetRadioStation:
402-
ret = append(ret, spec.NewTCInternetRadioStation(f))
403-
case *db.PodcastEpisode:
404-
ret = append(ret, spec.NewTCPodcastEpisode(f))
398+
switch id.Type {
399+
case specid.Track:
400+
var track db.Track
401+
if err := c.dbc.Where("id=?", id.Value).Preload("Album").Preload("Album.Artists").Preload("Artists").Find(&track).Error; err != nil {
402+
return nil, fmt.Errorf("load track: %w", err)
403+
}
404+
ret = append(ret, spec.NewTrackByTags(&track, track.Album))
405+
case specid.InternetRadioStation:
406+
var irs db.InternetRadioStation
407+
if err := c.dbc.Where("id=?", id.Value).Find(&irs).Error; err != nil {
408+
return nil, fmt.Errorf("load internet radio station: %w", err)
409+
}
410+
ret = append(ret, spec.NewTCInternetRadioStation(&irs))
411+
case specid.PodcastEpisode:
412+
var pe db.PodcastEpisode
413+
if err := c.dbc.Preload("Podcast").Where("id=?", id.Value).Find(&pe).Error; err != nil {
414+
return nil, fmt.Errorf("load podcast episode: %w", err)
415+
}
416+
ret = append(ret, spec.NewTCPodcastEpisode(&pe))
405417
default:
406418
return nil, fmt.Errorf("%q: %w", path, errUnknownPlaylistEntry)
407419
}

server/ctrlsubsonic/handlers_playlist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ func playlistRender(c *Controller, params params.Params, playlist *playlistp.Pla
221221
transcodeMeta := streamGetTranscodeMeta(c.dbc, user.ID, params.GetOr("c", ""))
222222

223223
for _, path := range playlist.Items {
224-
file, err := specidpaths.Lookup(c.dbc, MusicPaths(c.musicPaths), c.podcastsPath, path)
224+
id, err := specidpaths.Lookup(c.dbc, MusicPaths(c.musicPaths), c.podcastsPath, path)
225225
if err != nil {
226226
log.Printf("error looking up path %q: %s", path, err)
227227
continue
228228
}
229229

230230
var trch *spec.TrackChild
231-
switch id := file.SID(); id.Type {
231+
switch id.Type {
232232
case specid.Track:
233233
var track db.Track
234234
if err := c.dbc.Where("id=?", id.Value).Preload("Album").Preload("Album.Artists").Preload("Artists").Preload("TrackStar", "user_id=?", user.ID).Preload("TrackRating", "user_id=?", user.ID).Find(&track).Error; errors.Is(err, gorm.ErrRecordNotFound) {

server/ctrlsubsonic/specidpaths/specidpaths.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,40 @@ func Locate(dbc *db.DB, id specid.ID) (Result, error) {
3737
}
3838
}
3939

40-
// Locate maps a location on the filesystem to a specid
41-
func Lookup(dbc *db.DB, musicPaths []string, podcastsPath string, path string) (Result, error) {
40+
// Lookup maps a location on the filesystem to a specid
41+
func Lookup(dbc *db.DB, musicPaths []string, podcastsPath string, path string) (*specid.ID, error) {
4242
if !strings.HasPrefix(path, "http") && !filepath.IsAbs(path) {
4343
return nil, ErrNotAbs
4444
}
4545

4646
if strings.HasPrefix(path, podcastsPath) {
4747
podcastPath, episodeFilename := filepath.Split(path)
48+
4849
q := dbc.
50+
Select("podcast_episodes.id").
51+
Model(db.PodcastEpisode{}).
4952
Joins(`JOIN podcasts ON podcasts.id=podcast_episodes.podcast_id`).
5053
Where(`podcasts.root_dir=? AND podcast_episodes.filename=?`, filepath.Clean(podcastPath), filepath.Clean(episodeFilename))
5154

52-
var pe db.PodcastEpisode
53-
if err := q.First(&pe).Error; err == nil {
54-
return &pe, nil
55+
var id int
56+
if err := q.Scan(&id).Error; err != nil || id == 0 {
57+
return nil, ErrNotFound
5558
}
56-
return nil, ErrNotFound
59+
return &specid.ID{Type: specid.PodcastEpisode, Value: id}, nil
5760
}
5861

5962
// probably internet radio
6063
if strings.HasPrefix(path, "http") {
61-
var irs db.InternetRadioStation
62-
if err := dbc.First(&irs, "stream_url=?", path).Error; err == nil {
63-
return &irs, nil
64+
q := dbc.
65+
Select("internet_radio_stations.id").
66+
Model(db.InternetRadioStation{}).
67+
Where("stream_url=?", path)
68+
69+
var id int
70+
if err := q.Scan(&id).Error; err != nil || id == 0 {
71+
return nil, ErrNotFound
6472
}
65-
return nil, ErrNotFound
73+
return &specid.ID{Type: specid.InternetRadioStation, Value: id}, nil
6674
}
6775

6876
var musicPath string
@@ -81,13 +89,14 @@ func Lookup(dbc *db.DB, musicPaths []string, podcastsPath string, path string) (
8189
leftPath, rightPath := filepath.Split(filepath.Clean(relDir))
8290

8391
q := dbc.
92+
Select("tracks.id").
93+
Model(db.Track{}).
8494
Where(`albums.root_dir=? AND albums.left_path=? AND albums.right_path=? AND tracks.filename=?`, musicPath, leftPath, rightPath, filename).
85-
Joins(`JOIN albums ON tracks.album_id=albums.id`).
86-
Preload("Album")
95+
Joins(`JOIN albums ON tracks.album_id=albums.id`)
8796

88-
var track db.Track
89-
if err := q.First(&track).Error; err == nil {
90-
return &track, nil
97+
var id int
98+
if err := q.Scan(&id).Error; err != nil {
99+
return nil, ErrNotFound
91100
}
92-
return nil, ErrNotFound
101+
return &specid.ID{Type: specid.Track, Value: id}, nil
93102
}

0 commit comments

Comments
 (0)