mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-01 19:47:37 +03:00
Fix tests and lint errors, plus a bit of refactor
This commit is contained in:
parent
72a12e344e
commit
94cc2b2ac5
14 changed files with 114 additions and 98 deletions
|
@ -74,7 +74,7 @@ func (s *shareService) loadMediafiles(ctx context.Context, filter squirrel.Eq, s
|
|||
|
||||
func (s *shareService) loadPlaylistTracks(ctx context.Context, id string) (model.MediaFiles, error) {
|
||||
// Create a context with a fake admin user, to be able to access playlists
|
||||
ctx = request.WithUser(context.TODO(), model.User{IsAdmin: true})
|
||||
ctx = request.WithUser(ctx, model.User{IsAdmin: true})
|
||||
|
||||
tracks, err := s.ds.Playlist(ctx).Tracks(id, true).GetAll(model.QueryOptions{Sort: "id"})
|
||||
if err != nil {
|
||||
|
|
|
@ -29,7 +29,7 @@ var _ = Describe("Share", func() {
|
|||
})
|
||||
|
||||
Describe("Save", func() {
|
||||
It("it adds a random name", func() {
|
||||
It("it sets a random ID", func() {
|
||||
entity := &model.Share{Description: "test"}
|
||||
id, err := repo.Save(entity)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
@ -44,7 +44,7 @@ var _ = Describe("Share", func() {
|
|||
err := repo.Update("id", entity)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(mockedRepo.(*tests.MockShareRepo).Entity).To(Equal("entity"))
|
||||
Expect(mockedRepo.(*tests.MockShareRepo).Cols).To(ConsistOf("description"))
|
||||
Expect(mockedRepo.(*tests.MockShareRepo).Cols).To(ConsistOf("description", "expires_at"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
package public
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v2/jwt"
|
||||
"github.com/navidrome/navidrome/core/auth"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
)
|
||||
|
||||
func EncodeArtworkID(artID model.ArtworkID) string {
|
||||
token, _ := auth.CreatePublicToken(map[string]any{"id": artID.String()})
|
||||
return token
|
||||
}
|
||||
|
||||
func DecodeArtworkID(tokenString string) (model.ArtworkID, error) {
|
||||
token, err := auth.TokenAuth.Decode(tokenString)
|
||||
if err != nil {
|
||||
return model.ArtworkID{}, err
|
||||
}
|
||||
if token == nil {
|
||||
return model.ArtworkID{}, errors.New("unauthorized")
|
||||
}
|
||||
err = jwt.Validate(token, jwt.WithRequiredClaim("id"))
|
||||
if err != nil {
|
||||
return model.ArtworkID{}, err
|
||||
}
|
||||
claims, err := token.AsMap(context.Background())
|
||||
if err != nil {
|
||||
return model.ArtworkID{}, err
|
||||
}
|
||||
id, ok := claims["id"].(string)
|
||||
if !ok {
|
||||
return model.ArtworkID{}, errors.New("invalid id type")
|
||||
}
|
||||
artID, err := model.ParseArtworkID(id)
|
||||
if err == nil {
|
||||
return artID, nil
|
||||
}
|
||||
// Try to default to mediafile artworkId
|
||||
return model.ParseArtworkID("mf-" + id)
|
||||
}
|
71
server/public/encode_id.go
Normal file
71
server/public/encode_id.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package public
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/lestrrat-go/jwx/v2/jwt"
|
||||
"github.com/navidrome/navidrome/consts"
|
||||
"github.com/navidrome/navidrome/core/auth"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/server"
|
||||
)
|
||||
|
||||
func ImageURL(r *http.Request, artID model.ArtworkID, size int) string {
|
||||
link := encodeArtworkID(artID)
|
||||
path := filepath.Join(consts.URLPathPublicImages, link)
|
||||
params := url.Values{}
|
||||
if size > 0 {
|
||||
params.Add("size", strconv.Itoa(size))
|
||||
}
|
||||
return server.AbsoluteURL(r, path, params)
|
||||
}
|
||||
|
||||
func encodeArtworkID(artID model.ArtworkID) string {
|
||||
token, _ := auth.CreatePublicToken(map[string]any{"id": artID.String()})
|
||||
return token
|
||||
}
|
||||
|
||||
func decodeArtworkID(tokenString string) (model.ArtworkID, error) {
|
||||
token, err := auth.TokenAuth.Decode(tokenString)
|
||||
if err != nil {
|
||||
return model.ArtworkID{}, err
|
||||
}
|
||||
if token == nil {
|
||||
return model.ArtworkID{}, errors.New("unauthorized")
|
||||
}
|
||||
err = jwt.Validate(token, jwt.WithRequiredClaim("id"))
|
||||
if err != nil {
|
||||
return model.ArtworkID{}, err
|
||||
}
|
||||
claims, err := token.AsMap(context.Background())
|
||||
if err != nil {
|
||||
return model.ArtworkID{}, err
|
||||
}
|
||||
id, ok := claims["id"].(string)
|
||||
if !ok {
|
||||
return model.ArtworkID{}, errors.New("invalid id type")
|
||||
}
|
||||
artID, err := model.ParseArtworkID(id)
|
||||
if err == nil {
|
||||
return artID, nil
|
||||
}
|
||||
// Try to default to mediafile artworkId (if used with a mediafileShare token)
|
||||
return model.ParseArtworkID("mf-" + id)
|
||||
}
|
||||
|
||||
func encodeMediafileShare(s model.Share, id string) string {
|
||||
claims := map[string]any{"id": id}
|
||||
if s.Format != "" {
|
||||
claims["f"] = s.Format
|
||||
}
|
||||
if s.MaxBitRate != 0 {
|
||||
claims["b"] = s.MaxBitRate
|
||||
}
|
||||
token, _ := auth.CreateExpiringPublicToken(s.ExpiresAt, claims)
|
||||
return token
|
||||
}
|
|
@ -1,38 +1,38 @@
|
|||
package public_test
|
||||
package public
|
||||
|
||||
import (
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"github.com/navidrome/navidrome/core/auth"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/server/public"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("EncodeArtworkID", func() {
|
||||
var _ = Describe("encodeArtworkID", func() {
|
||||
Context("Public ID Encoding", func() {
|
||||
BeforeEach(func() {
|
||||
auth.TokenAuth = jwtauth.New("HS256", []byte("super secret"), nil)
|
||||
})
|
||||
It("returns a reversible string representation", func() {
|
||||
id := model.NewArtworkID(model.KindArtistArtwork, "1234")
|
||||
encoded := public.EncodeArtworkID(id)
|
||||
decoded, err := public.DecodeArtworkID(encoded)
|
||||
encoded := encodeArtworkID(id)
|
||||
decoded, err := decodeArtworkID(encoded)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(decoded).To(Equal(id))
|
||||
})
|
||||
It("fails to decode an invalid token", func() {
|
||||
_, err := public.DecodeArtworkID("xx-123")
|
||||
_, err := decodeArtworkID("xx-123")
|
||||
Expect(err).To(MatchError("invalid JWT"))
|
||||
})
|
||||
It("fails to decode an invalid id", func() {
|
||||
encoded := public.EncodeArtworkID(model.ArtworkID{})
|
||||
_, err := public.DecodeArtworkID(encoded)
|
||||
Expect(err).To(MatchError("invalid artwork id"))
|
||||
It("defaults to kind mediafile", func() {
|
||||
encoded := encodeArtworkID(model.ArtworkID{})
|
||||
id, err := decodeArtworkID(encoded)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(id.Kind).To(Equal(model.KindMediaFileArtwork))
|
||||
})
|
||||
It("fails to decode a token without an id", func() {
|
||||
token, _ := auth.CreatePublicToken(map[string]any{})
|
||||
_, err := public.DecodeArtworkID(token)
|
||||
_, err := decodeArtworkID(token)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
|
@ -21,7 +21,7 @@ func (p *Router) handleImages(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
artId, err := DecodeArtworkID(id)
|
||||
artId, err := decodeArtworkID(id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/navidrome/navidrome/core/auth"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/server"
|
||||
|
@ -39,26 +38,17 @@ func (p *Router) handleShares(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
s = p.mapShareInfo(s)
|
||||
s = p.mapShareInfo(*s)
|
||||
server.IndexWithShare(p.ds, ui.BuildAssets(), s)(w, r)
|
||||
}
|
||||
|
||||
func (p *Router) mapShareInfo(s *model.Share) *model.Share {
|
||||
func (p *Router) mapShareInfo(s model.Share) *model.Share {
|
||||
mapped := &model.Share{
|
||||
Description: s.Description,
|
||||
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
|
||||
}
|
||||
if s.MaxBitRate != 0 {
|
||||
claims["b"] = s.MaxBitRate
|
||||
}
|
||||
id, _ := auth.CreateExpiringPublicToken(s.ExpiresAt, claims)
|
||||
mapped.Tracks[i].ID = id
|
||||
mapped.Tracks[i].ID = encodeMediafileShare(s, s.Tracks[i].ID)
|
||||
}
|
||||
return mapped
|
||||
}
|
||||
|
|
|
@ -98,7 +98,7 @@ func decodeStreamInfo(tokenString string) (shareTrackInfo, error) {
|
|||
}
|
||||
resp := shareTrackInfo{}
|
||||
resp.id = id
|
||||
resp.format, ok = claims["f"].(string)
|
||||
resp.bitrate, ok = claims["b"].(int)
|
||||
resp.format, _ = claims["f"].(string)
|
||||
resp.bitrate, _ = claims["b"].(int)
|
||||
return resp, nil
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/navidrome/navidrome/conf"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/server/public"
|
||||
"github.com/navidrome/navidrome/server/subsonic/filter"
|
||||
"github.com/navidrome/navidrome/server/subsonic/responses"
|
||||
"github.com/navidrome/navidrome/utils"
|
||||
|
@ -256,9 +257,9 @@ func (api *Router) GetArtistInfo(r *http.Request) (*responses.Subsonic, error) {
|
|||
response := newResponse()
|
||||
response.ArtistInfo = &responses.ArtistInfo{}
|
||||
response.ArtistInfo.Biography = artist.Biography
|
||||
response.ArtistInfo.SmallImageUrl = publicImageURL(r, artist.CoverArtID(), 160)
|
||||
response.ArtistInfo.MediumImageUrl = publicImageURL(r, artist.CoverArtID(), 320)
|
||||
response.ArtistInfo.LargeImageUrl = publicImageURL(r, artist.CoverArtID(), 0)
|
||||
response.ArtistInfo.SmallImageUrl = public.ImageURL(r, artist.CoverArtID(), 160)
|
||||
response.ArtistInfo.MediumImageUrl = public.ImageURL(r, artist.CoverArtID(), 320)
|
||||
response.ArtistInfo.LargeImageUrl = public.ImageURL(r, artist.CoverArtID(), 0)
|
||||
response.ArtistInfo.LastFmUrl = artist.ExternalUrl
|
||||
response.ArtistInfo.MusicBrainzID = artist.MbzArtistID
|
||||
for _, s := range artist.SimilarArtists {
|
||||
|
|
|
@ -5,15 +5,11 @@ import (
|
|||
"fmt"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/navidrome/navidrome/consts"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/model/request"
|
||||
"github.com/navidrome/navidrome/server"
|
||||
"github.com/navidrome/navidrome/server/public"
|
||||
"github.com/navidrome/navidrome/server/subsonic/responses"
|
||||
"github.com/navidrome/navidrome/utils"
|
||||
|
@ -92,7 +88,7 @@ func toArtist(r *http.Request, a model.Artist) responses.Artist {
|
|||
AlbumCount: a.AlbumCount,
|
||||
UserRating: a.Rating,
|
||||
CoverArt: a.CoverArtID().String(),
|
||||
ArtistImageUrl: publicImageURL(r, a.CoverArtID(), 0),
|
||||
ArtistImageUrl: public.ImageURL(r, a.CoverArtID(), 0),
|
||||
}
|
||||
if a.Starred {
|
||||
artist.Starred = &a.StarredAt
|
||||
|
@ -106,7 +102,7 @@ func toArtistID3(r *http.Request, a model.Artist) responses.ArtistID3 {
|
|||
Name: a.Name,
|
||||
AlbumCount: a.AlbumCount,
|
||||
CoverArt: a.CoverArtID().String(),
|
||||
ArtistImageUrl: publicImageURL(r, a.CoverArtID(), 0),
|
||||
ArtistImageUrl: public.ImageURL(r, a.CoverArtID(), 0),
|
||||
UserRating: a.Rating,
|
||||
}
|
||||
if a.Starred {
|
||||
|
@ -115,16 +111,6 @@ func toArtistID3(r *http.Request, a model.Artist) responses.ArtistID3 {
|
|||
return artist
|
||||
}
|
||||
|
||||
func publicImageURL(r *http.Request, artID model.ArtworkID, size int) string {
|
||||
link := public.EncodeArtworkID(artID)
|
||||
path := filepath.Join(consts.URLPathPublicImages, link)
|
||||
params := url.Values{}
|
||||
if size > 0 {
|
||||
params.Add("size", strconv.Itoa(size))
|
||||
}
|
||||
return server.AbsoluteURL(r, path, params)
|
||||
}
|
||||
|
||||
func toGenres(genres model.Genres) *responses.Genres {
|
||||
response := make([]responses.Genre, len(genres))
|
||||
for i, g := range genres {
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/deluan/sanitize"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/server/public"
|
||||
"github.com/navidrome/navidrome/server/subsonic/responses"
|
||||
"github.com/navidrome/navidrome/utils"
|
||||
)
|
||||
|
@ -112,7 +113,7 @@ func (api *Router) Search2(r *http.Request) (*responses.Subsonic, error) {
|
|||
AlbumCount: artist.AlbumCount,
|
||||
UserRating: artist.Rating,
|
||||
CoverArt: artist.CoverArtID().String(),
|
||||
ArtistImageUrl: publicImageURL(r, artist.CoverArtID(), 0),
|
||||
ArtistImageUrl: public.ImageURL(r, artist.CoverArtID(), 0),
|
||||
}
|
||||
if artist.Starred {
|
||||
searchResult2.Artist[i].Starred = &as[i].StarredAt
|
||||
|
|
|
@ -20,8 +20,12 @@ func (m *MockShareRepo) Save(entity interface{}) (string, error) {
|
|||
if m.Error != nil {
|
||||
return "", m.Error
|
||||
}
|
||||
m.Entity = entity
|
||||
return "id", nil
|
||||
s := entity.(*model.Share)
|
||||
if s.ID == "" {
|
||||
s.ID = "id"
|
||||
}
|
||||
m.Entity = s
|
||||
return s.ID, nil
|
||||
}
|
||||
|
||||
func (m *MockShareRepo) Update(id string, entity interface{}, cols ...string) error {
|
||||
|
@ -33,3 +37,10 @@ func (m *MockShareRepo) Update(id string, entity interface{}, cols ...string) er
|
|||
m.Cols = cols
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockShareRepo) Exists(id string) (bool, error) {
|
||||
if m.Error != nil {
|
||||
return false, m.Error
|
||||
}
|
||||
return id == m.ID, nil
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,6 @@
|
|||
import ReactJkMusicPlayer from 'navidrome-music-player'
|
||||
import config, { shareInfo } from './config'
|
||||
import { baseUrl, shareCoverUrl, shareStreamUrl } from './utils'
|
||||
import { shareInfo } from './config'
|
||||
import { shareCoverUrl, shareStreamUrl } from './utils'
|
||||
|
||||
const SharePlayer = () => {
|
||||
const list = shareInfo?.tracks.map((s) => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue