Skip to content

Commit

Permalink
Replace all utils.Param* with req.Params
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Dec 21, 2023
1 parent 00597e0 commit dfcc189
Show file tree
Hide file tree
Showing 27 changed files with 269 additions and 513 deletions.
13 changes: 7 additions & 6 deletions core/agents/lastfm/auth_router.go
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/req"
)

//go:embed token_received.html
Expand Down Expand Up @@ -89,21 +89,22 @@ func (s *Router) unlink(w http.ResponseWriter, r *http.Request) {
}

func (s *Router) callback(w http.ResponseWriter, r *http.Request) {
token := utils.ParamString(r, "token")
if token == "" {
p := req.Params(r)
token, err := p.String("token")
if err != nil {
_ = rest.RespondWithError(w, http.StatusBadRequest, "token not received")
return
}
uid := utils.ParamString(r, "uid")
if uid == "" {
uid, err := p.String("uid")
if err != nil {
_ = rest.RespondWithError(w, http.StatusBadRequest, "uid not received")
return
}

// Need to add user to context, as this is a non-authenticated endpoint, so it does not
// automatically contain any user info
ctx := request.WithUser(r.Context(), model.User{ID: uid})
err := s.fetchSessionKey(ctx, uid, token)
err = s.fetchSessionKey(ctx, uid, token)
if err != nil {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
Expand Down
15 changes: 9 additions & 6 deletions server/nativeapi/playlists.go
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/req"
)

type restHandler = func(rest.RepositoryConstructor, ...rest.Logger) http.HandlerFunc
Expand Down Expand Up @@ -95,8 +95,9 @@ func handleExportPlaylist(ds model.DataStore) http.HandlerFunc {

func deleteFromPlaylist(ds model.DataStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
playlistId := utils.ParamString(r, ":playlistId")
ids := r.URL.Query()["id"]
p := req.Params(r)
playlistId, _ := p.String(":playlistId")
ids, _ := p.Strings("id")
err := ds.WithTx(func(tx model.DataStore) error {
tracksRepo := tx.Playlist(r.Context()).Tracks(playlistId, true)
return tracksRepo.Delete(ids...)
Expand Down Expand Up @@ -139,7 +140,8 @@ func addToPlaylist(ds model.DataStore) http.HandlerFunc {
}

return func(w http.ResponseWriter, r *http.Request) {
playlistId := utils.ParamString(r, ":playlistId")
p := req.Params(r)
playlistId, _ := p.String(":playlistId")
var payload addTracksPayload
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
Expand Down Expand Up @@ -183,8 +185,9 @@ func reorderItem(ds model.DataStore) http.HandlerFunc {
}

return func(w http.ResponseWriter, r *http.Request) {
playlistId := utils.ParamString(r, ":playlistId")
id := utils.ParamInt(r, ":id", 0)
p := req.Params(r)
playlistId, _ := p.String(":playlistId")
id := p.IntOr(":id", 0)
if id == 0 {
http.Error(w, "invalid id", http.StatusBadRequest)
return
Expand Down
12 changes: 7 additions & 5 deletions server/public/handle_downloads.go
Expand Up @@ -2,15 +2,17 @@ package public

import (
"net/http"

"github.com/navidrome/navidrome/utils/req"
)

func (p *Router) handleDownloads(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get(":id")
if id == "" {
http.Error(w, "invalid id", http.StatusBadRequest)
func (pub *Router) handleDownloads(w http.ResponseWriter, r *http.Request) {
id, err := req.Params(r).String(":id")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

err := p.archiver.ZipShare(r.Context(), id, w)
err = pub.archiver.ZipShare(r.Context(), id, w)
checkShareError(r.Context(), w, err, id)
}
12 changes: 7 additions & 5 deletions server/public/handle_images.go
Expand Up @@ -10,18 +10,20 @@ import (
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/req"
)

func (p *Router) handleImages(w http.ResponseWriter, r *http.Request) {
func (pub *Router) handleImages(w http.ResponseWriter, r *http.Request) {
// If context is already canceled, discard request without further processing
if r.Context().Err() != nil {
return
}

ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
id := r.URL.Query().Get(":id")

p := req.Params(r)
id, _ := p.String(":id")
if id == "" {
http.Error(w, "invalid id", http.StatusBadRequest)
return
Expand All @@ -32,9 +34,9 @@ func (p *Router) handleImages(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
size := utils.ParamInt(r, "size", 0)
size := p.IntOr("size", 0)

imgReader, lastUpdate, err := p.artwork.Get(ctx, artId, size)
imgReader, lastUpdate, err := pub.artwork.Get(ctx, artId, size)
switch {
case errors.Is(err, context.Canceled):
return
Expand Down
21 changes: 11 additions & 10 deletions server/public/handle_shares.go
Expand Up @@ -10,31 +10,32 @@ import (
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/ui"
"github.com/navidrome/navidrome/utils/req"
)

func (p *Router) handleShares(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get(":id")
if id == "" {
http.Error(w, "invalid id", http.StatusBadRequest)
func (pub *Router) handleShares(w http.ResponseWriter, r *http.Request) {
id, err := req.Params(r).String(":id")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

// If requested file is a UI asset, just serve it
_, err := ui.BuildAssets().Open(id)
_, err = ui.BuildAssets().Open(id)
if err == nil {
p.assetsHandler.ServeHTTP(w, r)
pub.assetsHandler.ServeHTTP(w, r)
return
}

// If it is not, consider it a share ID
s, err := p.share.Load(r.Context(), id)
s, err := pub.share.Load(r.Context(), id)
if err != nil {
checkShareError(r.Context(), w, err, id)
return
}

s = p.mapShareInfo(r, *s)
server.IndexWithShare(p.ds, ui.BuildAssets(), s)(w, r)
s = pub.mapShareInfo(r, *s)
server.IndexWithShare(pub.ds, ui.BuildAssets(), s)(w, r)
}

func checkShareError(ctx context.Context, w http.ResponseWriter, err error, id string) {
Expand All @@ -54,7 +55,7 @@ func checkShareError(ctx context.Context, w http.ResponseWriter, err error, id s
}
}

func (p *Router) mapShareInfo(r *http.Request, s model.Share) *model.Share {
func (pub *Router) mapShareInfo(r *http.Request, s model.Share) *model.Share {
s.URL = ShareURL(r, s.ID)
s.ImageURL = ImageURL(r, s.CoverArtID(), consts.UICoverArtSize)
for i := range s.Tracks {
Expand Down
11 changes: 6 additions & 5 deletions server/public/handle_streams.go
Expand Up @@ -10,20 +10,21 @@ import (
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/req"
)

func (p *Router) handleStream(w http.ResponseWriter, r *http.Request) {
func (pub *Router) handleStream(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tokenId := r.URL.Query().Get(":id")
p := req.Params(r)
tokenId, _ := p.String(":id")
info, err := decodeStreamInfo(tokenId)
if err != nil {
log.Error(ctx, "Error parsing shared stream info", err)
http.Error(w, "invalid request", http.StatusBadRequest)
return
}

stream, err := p.streamer.NewStream(ctx, info.id, info.format, info.bitrate, 0)
stream, err := pub.streamer.NewStream(ctx, info.id, info.format, info.bitrate, 0)
if err != nil {
log.Error(ctx, "Error starting shared stream", err)
http.Error(w, "invalid request", http.StatusInternalServerError)
Expand All @@ -46,7 +47,7 @@ func (p *Router) handleStream(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Accept-Ranges", "none")
w.Header().Set("Content-Type", stream.ContentType())

estimateContentLength := utils.ParamBool(r, "estimateContentLength", false)
estimateContentLength := p.BoolOr("estimateContentLength", false)

// if Client requests the estimated content-length, send it
if estimateContentLength {
Expand Down
14 changes: 7 additions & 7 deletions server/public/public.go
Expand Up @@ -35,7 +35,7 @@ func New(ds model.DataStore, artwork artwork.Artwork, streamer core.MediaStreame
return p
}

func (p *Router) routes() http.Handler {
func (pub *Router) routes() http.Handler {
r := chi.NewRouter()

r.Group(func(r chi.Router) {
Expand All @@ -48,16 +48,16 @@ func (p *Router) routes() http.Handler {
r.Use(middleware.ThrottleBacklog(conf.Server.DevArtworkMaxRequests, conf.Server.DevArtworkThrottleBacklogLimit,
conf.Server.DevArtworkThrottleBacklogTimeout))
}
r.HandleFunc("/img/{id}", p.handleImages)
r.HandleFunc("/img/{id}", pub.handleImages)
})
if conf.Server.EnableSharing {
r.HandleFunc("/s/{id}", p.handleStream)
r.HandleFunc("/s/{id}", pub.handleStream)
if conf.Server.EnableDownloads {
r.HandleFunc("/d/{id}", p.handleDownloads)
r.HandleFunc("/d/{id}", pub.handleDownloads)
}
r.HandleFunc("/{id}", p.handleShares)
r.HandleFunc("/", p.handleShares)
r.Handle("/*", p.assetsHandler)
r.HandleFunc("/{id}", pub.handleShares)
r.HandleFunc("/", pub.handleShares)
r.Handle("/*", pub.assetsHandler)
}
})
return r
Expand Down
31 changes: 17 additions & 14 deletions server/subsonic/album_lists.go
Expand Up @@ -10,12 +10,13 @@ import (
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/subsonic/filter"
"github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/number"
"github.com/navidrome/navidrome/utils/req"
)

func (api *Router) getAlbumList(r *http.Request) (model.Albums, int64, error) {
typ, err := requiredParamString(r, "type")
p := req.Params(r)
typ, err := p.String("type")
if err != nil {
return nil, 0, err
}
Expand All @@ -39,17 +40,17 @@ func (api *Router) getAlbumList(r *http.Request) (model.Albums, int64, error) {
case "highest":
opts = filter.AlbumsByRating()
case "byGenre":
genre, err := requiredParamString(r, "genre")
genre, err := p.String("genre")
if err != nil {
return nil, 0, err
}
opts = filter.AlbumsByGenre(genre)
case "byYear":
fromYear, err := requiredParamInt(r, "fromYear")
fromYear, err := p.Int("fromYear")
if err != nil {
return nil, 0, err
}
toYear, err := requiredParamInt(r, "toYear")
toYear, err := p.Int("toYear")
if err != nil {
return nil, 0, err
}
Expand All @@ -59,8 +60,8 @@ func (api *Router) getAlbumList(r *http.Request) (model.Albums, int64, error) {
return nil, 0, newError(responses.ErrorGeneric, "type '%s' not implemented", typ)
}

opts.Offset = utils.ParamInt(r, "offset", 0)
opts.Max = number.Min(utils.ParamInt(r, "size", 10), 500)
opts.Offset = p.IntOr("offset", 0)
opts.Max = number.Min(p.IntOr("size", 10), 500)
albums, err := api.ds.Album(r.Context()).GetAllWithoutGenres(opts)

if err != nil {
Expand Down Expand Up @@ -163,10 +164,11 @@ func (api *Router) GetNowPlaying(r *http.Request) (*responses.Subsonic, error) {
}

func (api *Router) GetRandomSongs(r *http.Request) (*responses.Subsonic, error) {
size := number.Min(utils.ParamInt(r, "size", 10), 500)
genre := utils.ParamString(r, "genre")
fromYear := utils.ParamInt(r, "fromYear", 0)
toYear := utils.ParamInt(r, "toYear", 0)
p := req.Params(r)
size := number.Min(p.IntOr("size", 10), 500)
genre, _ := p.String("genre")
fromYear := p.IntOr("fromYear", 0)
toYear := p.IntOr("toYear", 0)

songs, err := api.getSongs(r.Context(), 0, size, filter.SongsByRandom(genre, fromYear, toYear))
if err != nil {
Expand All @@ -181,9 +183,10 @@ func (api *Router) GetRandomSongs(r *http.Request) (*responses.Subsonic, error)
}

func (api *Router) GetSongsByGenre(r *http.Request) (*responses.Subsonic, error) {
count := number.Min(utils.ParamInt(r, "count", 10), 500)
offset := utils.ParamInt(r, "offset", 0)
genre := utils.ParamString(r, "genre")
p := req.Params(r)
count := number.Min(p.IntOr("count", 10), 500)
offset := p.IntOr("offset", 0)
genre, _ := p.String("genre")

songs, err := api.getSongs(r.Context(), offset, count, filter.SongsByGenre(genre))
if err != nil {
Expand Down

0 comments on commit dfcc189

Please sign in to comment.