refactor(specid): have Lookup return *specid.ID
This commit is contained in:
No files matched your search
@@ -42,6 +42,8 @@ func (c *Controller) ServeGetBookmarks(r *http.Request) *spec.Response {
|
||||
var track db.Track
|
||||
err := c.dbc.
|
||||
Preload("Album").
|
||||
Preload("Album.Artists").
|
||||
Preload("Artists").
|
||||
Find(&track, "id=?", bookmark.EntryID).
|
||||
Error
|
||||
if err != nil {
|
||||
|
||||
@@ -391,17 +391,29 @@ func (c *Controller) ServeJukebox(r *http.Request) *spec.Response { // nolint:go
|
||||
return nil, fmt.Errorf("get playlist: %w", err)
|
||||
}
|
||||
for _, path := range playlist {
|
||||
file, err := specidpaths.Lookup(c.dbc, MusicPaths(c.musicPaths), c.podcastsPath, path)
|
||||
id, err := specidpaths.Lookup(c.dbc, MusicPaths(c.musicPaths), c.podcastsPath, path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetch track: %w", err)
|
||||
}
|
||||
switch f := file.(type) {
|
||||
case *db.Track:
|
||||
ret = append(ret, spec.NewTrackByTags(f, f.Album))
|
||||
case *db.InternetRadioStation:
|
||||
ret = append(ret, spec.NewTCInternetRadioStation(f))
|
||||
case *db.PodcastEpisode:
|
||||
ret = append(ret, spec.NewTCPodcastEpisode(f))
|
||||
switch id.Type {
|
||||
case specid.Track:
|
||||
var track db.Track
|
||||
if err := c.dbc.Where("id=?", id.Value).Preload("Album").Preload("Album.Artists").Preload("Artists").Find(&track).Error; err != nil {
|
||||
return nil, fmt.Errorf("load track: %w", err)
|
||||
}
|
||||
ret = append(ret, spec.NewTrackByTags(&track, track.Album))
|
||||
case specid.InternetRadioStation:
|
||||
var irs db.InternetRadioStation
|
||||
if err := c.dbc.Where("id=?", id.Value).Find(&irs).Error; err != nil {
|
||||
return nil, fmt.Errorf("load internet radio station: %w", err)
|
||||
}
|
||||
ret = append(ret, spec.NewTCInternetRadioStation(&irs))
|
||||
case specid.PodcastEpisode:
|
||||
var pe db.PodcastEpisode
|
||||
if err := c.dbc.Preload("Podcast").Where("id=?", id.Value).Find(&pe).Error; err != nil {
|
||||
return nil, fmt.Errorf("load podcast episode: %w", err)
|
||||
}
|
||||
ret = append(ret, spec.NewTCPodcastEpisode(&pe))
|
||||
default:
|
||||
return nil, fmt.Errorf("%q: %w", path, errUnknownPlaylistEntry)
|
||||
}
|
||||
|
||||
@@ -221,14 +221,14 @@ func playlistRender(c *Controller, params params.Params, playlist *playlistp.Pla
|
||||
transcodeMeta := streamGetTranscodeMeta(c.dbc, user.ID, params.GetOr("c", ""))
|
||||
|
||||
for _, path := range playlist.Items {
|
||||
file, err := specidpaths.Lookup(c.dbc, MusicPaths(c.musicPaths), c.podcastsPath, path)
|
||||
id, err := specidpaths.Lookup(c.dbc, MusicPaths(c.musicPaths), c.podcastsPath, path)
|
||||
if err != nil {
|
||||
log.Printf("error looking up path %q: %s", path, err)
|
||||
continue
|
||||
}
|
||||
|
||||
var trch *spec.TrackChild
|
||||
switch id := file.SID(); id.Type {
|
||||
switch id.Type {
|
||||
case specid.Track:
|
||||
var track db.Track
|
||||
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) {
|
||||
|
||||
@@ -37,32 +37,40 @@ func Locate(dbc *db.DB, id specid.ID) (Result, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Locate maps a location on the filesystem to a specid
|
||||
func Lookup(dbc *db.DB, musicPaths []string, podcastsPath string, path string) (Result, error) {
|
||||
// Lookup maps a location on the filesystem to a specid
|
||||
func Lookup(dbc *db.DB, musicPaths []string, podcastsPath string, path string) (*specid.ID, error) {
|
||||
if !strings.HasPrefix(path, "http") && !filepath.IsAbs(path) {
|
||||
return nil, ErrNotAbs
|
||||
}
|
||||
|
||||
if strings.HasPrefix(path, podcastsPath) {
|
||||
podcastPath, episodeFilename := filepath.Split(path)
|
||||
|
||||
q := dbc.
|
||||
Select("podcast_episodes.id").
|
||||
Model(db.PodcastEpisode{}).
|
||||
Joins(`JOIN podcasts ON podcasts.id=podcast_episodes.podcast_id`).
|
||||
Where(`podcasts.root_dir=? AND podcast_episodes.filename=?`, filepath.Clean(podcastPath), filepath.Clean(episodeFilename))
|
||||
|
||||
var pe db.PodcastEpisode
|
||||
if err := q.First(&pe).Error; err == nil {
|
||||
return &pe, nil
|
||||
var id int
|
||||
if err := q.Row().Scan(&id); err != nil {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, ErrNotFound
|
||||
return &specid.ID{Type: specid.PodcastEpisode, Value: id}, nil
|
||||
}
|
||||
|
||||
// probably internet radio
|
||||
if strings.HasPrefix(path, "http") {
|
||||
var irs db.InternetRadioStation
|
||||
if err := dbc.First(&irs, "stream_url=?", path).Error; err == nil {
|
||||
return &irs, nil
|
||||
q := dbc.
|
||||
Select("internet_radio_stations.id").
|
||||
Model(db.InternetRadioStation{}).
|
||||
Where("stream_url=?", path)
|
||||
|
||||
var id int
|
||||
if err := q.Row().Scan(&id); err != nil {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, ErrNotFound
|
||||
return &specid.ID{Type: specid.InternetRadioStation, Value: id}, nil
|
||||
}
|
||||
|
||||
var musicPath string
|
||||
@@ -81,13 +89,14 @@ func Lookup(dbc *db.DB, musicPaths []string, podcastsPath string, path string) (
|
||||
leftPath, rightPath := filepath.Split(filepath.Clean(relDir))
|
||||
|
||||
q := dbc.
|
||||
Select("tracks.id").
|
||||
Model(db.Track{}).
|
||||
Where(`albums.root_dir=? AND albums.left_path=? AND albums.right_path=? AND tracks.filename=?`, musicPath, leftPath, rightPath, filename).
|
||||
Joins(`JOIN albums ON tracks.album_id=albums.id`).
|
||||
Preload("Album")
|
||||
Joins(`JOIN albums ON tracks.album_id=albums.id`)
|
||||
|
||||
var track db.Track
|
||||
if err := q.First(&track).Error; err == nil {
|
||||
return &track, nil
|
||||
var id int
|
||||
if err := q.Row().Scan(&id); err != nil {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, ErrNotFound
|
||||
return &specid.ID{Type: specid.Track, Value: id}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user