From 01b620cffbc43c22a654ad9eae0ee9e7bd7feab3 Mon Sep 17 00:00:00 2001 From: Serge Tkatchouk Date: Thu, 13 Feb 2020 23:06:59 +0800 Subject: [PATCH] Add separate "Download" controller --- server/ctrlsubsonic/handlers_raw.go | 29 +++++++++++++++++++++++++++++ server/server.go | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/server/ctrlsubsonic/handlers_raw.go b/server/ctrlsubsonic/handlers_raw.go index 280473c..3698a0e 100644 --- a/server/ctrlsubsonic/handlers_raw.go +++ b/server/ctrlsubsonic/handlers_raw.go @@ -84,3 +84,32 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R c.DB.Save(&play) 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 +} diff --git a/server/server.go b/server/server.go index 0111e5c..dba73d8 100644 --- a/server/server.go +++ b/server/server.go @@ -150,7 +150,7 @@ func setupSubsonic(r *mux.Router, ctrl *ctrlsubsonic.Controller) { r.Handle("/getRandomSongs{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetRandomSongs)) r.Handle("/getSongsByGenre{_:(?:\\.view)?}", ctrl.H(ctrl.ServeGetSongsByGenre)) // ** 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("/stream{_:(?:\\.view)?}", ctrl.HR(ctrl.ServeStream)) // ** begin browse by tag