Expose mediafile.AbsolutePath to remove dependency of the persistence layer in the API layer

This commit is contained in:
Deluan 2020-01-11 14:08:53 -05:00 committed by Deluan Quintão
parent 28ee1f195c
commit 52fc580a2b
6 changed files with 31 additions and 31 deletions

View file

@ -8,7 +8,6 @@ import (
"github.com/cloudsonic/sonic-server/api/responses"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/engine"
"github.com/go-chi/chi"
)
@ -25,17 +24,15 @@ type Router struct {
Ratings engine.Ratings
Scrobbler engine.Scrobbler
Search engine.Search
MediaFileRepository domain.MediaFileRepository
mux http.Handler
}
func NewRouter(browser engine.Browser, cover engine.Cover, listGenerator engine.ListGenerator,
playlists engine.Playlists, ratings engine.Ratings, scrobbler engine.Scrobbler, search engine.Search,
mediaFileRepository domain.MediaFileRepository) *Router {
playlists engine.Playlists, ratings engine.Ratings, scrobbler engine.Scrobbler, search engine.Search) *Router {
r := &Router{Browser: browser, Cover: cover, ListGenerator: listGenerator, Playlists: playlists,
Ratings: ratings, Scrobbler: scrobbler, Search: search, MediaFileRepository: mediaFileRepository}
Ratings: ratings, Scrobbler: scrobbler, Search: search}
r.mux = r.routes()
return r
}

View file

@ -11,20 +11,20 @@ import (
)
type StreamController struct {
repo domain.MediaFileRepository
browser engine.Browser
}
func NewStreamController(repo domain.MediaFileRepository) *StreamController {
return &StreamController{repo: repo}
func NewStreamController(browser engine.Browser) *StreamController {
return &StreamController{browser: browser}
}
func (c *StreamController) Prepare(r *http.Request) (id string, mf *domain.MediaFile, err error) {
func (c *StreamController) Prepare(r *http.Request) (id string, mf *engine.Entry, err error) {
id, err = RequiredParamString(r, "id", "id parameter required")
if err != nil {
return "", nil, err
}
mf, err = c.repo.Get(id)
mf, err = c.browser.GetSong(id)
switch {
case err == domain.ErrNotFound:
log.Error(r, "Mediafile not found", "id", id)
@ -46,7 +46,7 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp
maxBitRate := ParamInt(r, "maxBitRate", 0)
maxBitRate = utils.MinInt(mf.BitRate, maxBitRate)
log.Debug(r, "Streaming file", "id", id, "path", mf.Path, "bitrate", mf.BitRate, "maxBitRate", maxBitRate)
log.Debug(r, "Streaming file", "id", id, "path", mf.AbsolutePath, "bitrate", mf.BitRate, "maxBitRate", maxBitRate)
// TODO Send proper estimated content-length
//contentLength := mf.Size
@ -61,16 +61,16 @@ func (c *StreamController) Stream(w http.ResponseWriter, r *http.Request) (*resp
h.Set("Pragma", "public")
if r.Method == "HEAD" {
log.Debug(r, "Just a HEAD. Not streaming", "path", mf.Path)
log.Debug(r, "Just a HEAD. Not streaming", "path", mf.AbsolutePath)
return nil, nil
}
err = engine.Stream(r.Context(), mf.Path, mf.BitRate, maxBitRate, w)
err = engine.Stream(r.Context(), mf.AbsolutePath, mf.BitRate, maxBitRate, w)
if err != nil {
log.Error(r, "Error streaming file", "id", id, err)
}
log.Debug(r, "Finished streaming", "path", mf.Path)
log.Debug(r, "Finished streaming", "path", mf.AbsolutePath)
return nil, nil
}
@ -79,14 +79,14 @@ func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*re
if err != nil {
return nil, err
}
log.Debug(r, "Sending file", "path", mf.Path)
log.Debug(r, "Sending file", "path", mf.AbsolutePath)
err = engine.Stream(r.Context(), mf.Path, 0, 0, w)
err = engine.Stream(r.Context(), mf.AbsolutePath, 0, 0, w)
if err != nil {
log.Error(r, "Error downloading file", "path", mf.Path, err)
log.Error(r, "Error downloading file", "path", mf.AbsolutePath, err)
}
log.Debug(r, "Finished sending", "path", mf.Path)
log.Debug(r, "Finished sending", "path", mf.AbsolutePath)
return nil, nil
}

View file

@ -63,8 +63,8 @@ func initMediaRetrievalController(router *Router) *MediaRetrievalController {
}
func initStreamController(router *Router) *StreamController {
mediaFileRepository := router.MediaFileRepository
streamController := NewStreamController(mediaFileRepository)
browser := router.Browser
streamController := NewStreamController(browser)
return streamController
}
@ -79,7 +79,7 @@ var allProviders = wire.NewSet(itunesbridge.NewItunesControl, NewSystemControlle
NewUsersController,
NewMediaRetrievalController,
NewStreamController,
newDB, wire.FieldsOf(new(*Router), "Browser", "Cover", "ListGenerator", "Playlists", "Ratings", "Scrobbler", "Search", "MediaFileRepository"),
newDB, wire.FieldsOf(new(*Router), "Browser", "Cover", "ListGenerator", "Playlists", "Ratings", "Scrobbler", "Search"),
)
func newDB() gomate.DB {

View file

@ -22,7 +22,7 @@ var allProviders = wire.NewSet(
NewMediaRetrievalController,
NewStreamController,
newDB,
wire.FieldsOf(new(*Router), "Browser", "Cover", "ListGenerator", "Playlists", "Ratings", "Scrobbler", "Search", "MediaFileRepository"),
wire.FieldsOf(new(*Router), "Browser", "Cover", "ListGenerator", "Playlists", "Ratings", "Scrobbler", "Search"),
)
func initSystemController(router *Router) *SystemController {

View file

@ -39,6 +39,8 @@ type Entry struct {
PlayerId int
PlayerName string
AlbumCount int
AbsolutePath string
}
type Entries []Entry
@ -94,7 +96,8 @@ func FromMediaFile(mf *domain.MediaFile) Entry {
e.CoverArt = mf.ID
}
e.ContentType = mf.ContentType()
// Creates a "pseudo" path, to avoid sending absolute paths to the client
e.AbsolutePath = mf.Path
// Creates a "pseudo" Path, to avoid sending absolute paths to the client
if mf.Path != "" {
e.Path = fmt.Sprintf("%s/%s/%s.%s", realArtistName(mf), mf.Album, mf.Title, mf.Suffix)
}

View file

@ -54,7 +54,7 @@ func initRouter() *api.Router {
scrobbler := engine.NewScrobbler(itunesControl, mediaFileRepository, nowPlayingRepository)
db := newDB()
search := engine.NewSearch(artistRepository, albumRepository, mediaFileRepository, db)
router := api.NewRouter(browser, cover, listGenerator, playlists, ratings, scrobbler, search, mediaFileRepository)
router := api.NewRouter(browser, cover, listGenerator, playlists, ratings, scrobbler, search)
return router
}