@@ -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