Replace all utils.Param* with req.Params
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ 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
|
||||
@@ -21,7 +21,9 @@ func (p *Router) handleImages(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
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
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
@@ -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 {
|
||||
|
||||
@@ -10,12 +10,13 @@ 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)
|
||||
@@ -23,7 +24,7 @@ func (p *Router) handleStream(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user