Add separate "Download" controller

This commit is contained in:
Serge Tkatchouk
2020-02-13 23:06:59 +08:00
committed by sentriz
parent 9f61e92487
commit 01b620cffb
2 changed files with 30 additions and 1 deletions

View File

@@ -84,3 +84,32 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
c.DB.Save(&play) c.DB.Save(&play)
return nil return nil
} }
func (c *Controller) ServeDownload(w http.ResponseWriter, r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetInt("id")
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
track := &model.Track{}
err = c.DB.
Preload("Album").
First(track, id).
Error
if gorm.IsRecordNotFoundError(err) {
return spec.NewError(70, "media with id `%d` was not found", id)
}
absPath := path.Join(
c.MusicPath,
track.Album.LeftPath,
track.Album.RightPath,
track.Filename,
)
http.ServeFile(w, r, absPath)
//
// We don't need to mark album/track as played
// if user just downloads a track, so bail out here:
return nil
}

View File

@@ -150,7 +150,7 @@ func setupSubsonic(r *mux.Router, ctrl *ctrlsubsonic.Controller) {
r.Handle("/getRandomSongs{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetRandomSongs)) r.Handle("/getRandomSongs{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetRandomSongs))
r.Handle("/getSongsByGenre{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetSongsByGenre)) r.Handle("/getSongsByGenre{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetSongsByGenre))
// ** begin raw // ** begin raw
r.Handle("/download{_:(?:\\.view)?}", ctrl.HR(ctrl.ServeStream)) r.Handle("/download{_:(?:\\.view)?}", ctrl.HR(ctrl.ServeDownload))
r.Handle("/getCoverArt{_:(?:\\.view)?}", ctrl.HR(ctrl.ServeGetCoverArt)) r.Handle("/getCoverArt{_:(?:\\.view)?}", ctrl.HR(ctrl.ServeGetCoverArt))
r.Handle("/stream{_:(?:\\.view)?}", ctrl.HR(ctrl.ServeStream)) r.Handle("/stream{_:(?:\\.view)?}", ctrl.HR(ctrl.ServeStream))
// ** begin browse by tag // ** begin browse by tag