More work on Shares

This commit is contained in:
Deluan 2023-01-20 19:53:53 -05:00
parent ab04e33da6
commit 84aa094e56
19 changed files with 150 additions and 167 deletions

View file

@ -0,0 +1,53 @@
package public
import (
"context"
"errors"
"io"
"net/http"
"time"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
)
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")
if id == "" {
http.Error(w, "invalid id", http.StatusBadRequest)
return
}
artId, err := DecodeArtworkID(id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
size := utils.ParamInt(r, "size", 0)
imgReader, lastUpdate, err := p.artwork.Get(ctx, artId.String(), size)
switch {
case errors.Is(err, context.Canceled):
return
case errors.Is(err, model.ErrNotFound):
log.Error(r, "Couldn't find coverArt", "id", id, err)
http.Error(w, "Artwork not found", http.StatusNotFound)
return
case err != nil:
log.Error(r, "Error retrieving coverArt", "id", id, err)
http.Error(w, "Error retrieving coverArt", http.StatusInternalServerError)
return
}
defer imgReader.Close()
w.Header().Set("Cache-Control", "public, max-age=315360000")
w.Header().Set("Last-Modified", lastUpdate.Format(time.RFC1123))
cnt, err := io.Copy(w, imgReader)
if err != nil {
log.Warn(ctx, "Error sending image", "count", cnt, err)
}
}

View file

@ -1,14 +1,9 @@
package shares
package public
import (
"errors"
"net/http"
"path"
"github.com/go-chi/chi/v5"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
@ -16,34 +11,6 @@ import (
"github.com/navidrome/navidrome/ui"
)
type Router struct {
http.Handler
ds model.DataStore
share core.Share
assetsHandler http.Handler
streamer core.MediaStreamer
}
func New(ds model.DataStore, share core.Share) *Router {
p := &Router{ds: ds, share: share}
shareRoot := path.Join(conf.Server.BaseURL, consts.URLPathShares)
p.assetsHandler = http.StripPrefix(shareRoot, http.FileServer(http.FS(ui.BuildAssets())))
p.Handler = p.routes()
return p
}
func (p *Router) routes() http.Handler {
r := chi.NewRouter()
r.Group(func(r chi.Router) {
r.Use(server.URLParamsMiddleware)
r.HandleFunc("/{id}", p.handleShares)
r.Handle("/*", p.assetsHandler)
})
return r
}
func (p *Router) handleShares(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get(":id")
if id == "" {
@ -82,6 +49,7 @@ func (p *Router) mapShareInfo(s *model.Share) *model.Share {
Tracks: s.Tracks,
}
for i := range s.Tracks {
// TODO Use Encode(Artwork)ID?
claims := map[string]any{"id": s.Tracks[i].ID}
if s.Format != "" {
claims["f"] = s.Format
@ -89,7 +57,7 @@ func (p *Router) mapShareInfo(s *model.Share) *model.Share {
if s.MaxBitRate != 0 {
claims["b"] = s.MaxBitRate
}
id, _ := auth.CreateExpiringPublicToken(*s.ExpiresAt, claims)
id, _ := auth.CreateExpiringPublicToken(s.ExpiresAt, claims)
mapped.Tracks[i].ID = id
}
return mapped

View file

@ -1,29 +1,32 @@
package public
import (
"context"
"errors"
"io"
"net/http"
"time"
"path"
"github.com/go-chi/chi/v5"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/ui"
)
type Router struct {
http.Handler
artwork artwork.Artwork
streamer core.MediaStreamer
artwork artwork.Artwork
streamer core.MediaStreamer
share core.Share
assetsHandler http.Handler
ds model.DataStore
}
func New(artwork artwork.Artwork, streamer core.MediaStreamer) *Router {
p := &Router{artwork: artwork, streamer: streamer}
func New(ds model.DataStore, artwork artwork.Artwork, streamer core.MediaStreamer, share core.Share) *Router {
p := &Router{ds: ds, artwork: artwork, streamer: streamer, share: share}
shareRoot := path.Join(conf.Server.BaseURL, consts.URLPathPublic)
p.assetsHandler = http.StripPrefix(shareRoot, http.FileServer(http.FS(ui.BuildAssets())))
p.Handler = p.routes()
return p
@ -34,48 +37,12 @@ func (p *Router) routes() http.Handler {
r.Group(func(r chi.Router) {
r.Use(server.URLParamsMiddleware)
r.HandleFunc("/s/{id}", p.handleStream)
r.HandleFunc("/img/{id}", p.handleImages)
if conf.Server.DevEnableShare {
r.HandleFunc("/s/{id}", p.handleStream)
r.HandleFunc("/{id}", p.handleShares)
r.Handle("/*", p.assetsHandler)
}
})
return r
}
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")
if id == "" {
http.Error(w, "invalid id", http.StatusBadRequest)
return
}
artId, err := DecodeArtworkID(id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
size := utils.ParamInt(r, "size", 0)
imgReader, lastUpdate, err := p.artwork.Get(ctx, artId.String(), size)
switch {
case errors.Is(err, context.Canceled):
return
case errors.Is(err, model.ErrNotFound):
log.Error(r, "Couldn't find coverArt", "id", id, err)
http.Error(w, "Artwork not found", http.StatusNotFound)
return
case err != nil:
log.Error(r, "Error retrieving coverArt", "id", id, err)
http.Error(w, "Error retrieving coverArt", http.StatusInternalServerError)
return
}
defer imgReader.Close()
w.Header().Set("Cache-Control", "public, max-age=315360000")
w.Header().Set("Last-Modified", lastUpdate.Format(time.RFC1123))
cnt, err := io.Copy(w, imgReader)
if err != nil {
log.Warn(ctx, "Error sending image", "count", cnt, err)
}
}