Skip to content

Commit

Permalink
fix: avoid panicking when trying to parse invalid uri
Browse files Browse the repository at this point in the history
See #129
  • Loading branch information
devgianlu committed Oct 22, 2024
1 parent 078497a commit 6088a20
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
20 changes: 12 additions & 8 deletions cmd/daemon/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ func (p *AppPlayer) prefetchNext() {
return
}

nextId := librespot.SpotifyIdFromUri(next.Uri)
if p.secondaryStream != nil && p.secondaryStream.Is(nextId) {
nextId, err := librespot.SpotifyIdFromUri(next.Uri)
if err != nil {
log.WithError(err).WithField("uri", next.Uri).Warn("failed parsing prefetch uri")
return
} else if p.secondaryStream != nil && p.secondaryStream.Is(*nextId) {
return
}

log.WithField("uri", nextId.Uri()).Debugf("prefetching next %s", nextId.Type())

var err error
p.secondaryStream, err = p.player.NewStream(nextId, p.app.cfg.Bitrate, 0)
p.secondaryStream, err = p.player.NewStream(*nextId, p.app.cfg.Bitrate, 0)
if err != nil {
log.WithError(err).WithField("uri", nextId.String()).Warnf("failed prefetching %s stream", nextId.Type())
return
Expand Down Expand Up @@ -196,8 +198,10 @@ func (p *AppPlayer) loadContext(ctx *connectpb.Context, skipTo skipToFunc, pause
func (p *AppPlayer) loadCurrentTrack(paused, drop bool) error {
p.primaryStream = nil

spotId := librespot.SpotifyIdFromUri(p.state.player.Track.Uri)
if spotId.Type() != librespot.SpotifyIdTypeTrack && spotId.Type() != librespot.SpotifyIdTypeEpisode {
spotId, err := librespot.SpotifyIdFromUri(p.state.player.Track.Uri)
if err != nil {
return fmt.Errorf("failed parsing uri: %w", err)
} else if spotId.Type() != librespot.SpotifyIdTypeTrack && spotId.Type() != librespot.SpotifyIdTypeEpisode {
return fmt.Errorf("unsupported spotify type: %s", spotId.Type())
}

Expand All @@ -221,7 +225,7 @@ func (p *AppPlayer) loadCurrentTrack(paused, drop bool) error {
})

var prefetched bool
if p.secondaryStream != nil && p.secondaryStream.Is(spotId) {
if p.secondaryStream != nil && p.secondaryStream.Is(*spotId) {
p.primaryStream = p.secondaryStream
p.secondaryStream = nil
prefetched = true
Expand All @@ -230,7 +234,7 @@ func (p *AppPlayer) loadCurrentTrack(paused, drop bool) error {
prefetched = false

var err error
p.primaryStream, err = p.player.NewStream(spotId, p.app.cfg.Bitrate, trackPosition)
p.primaryStream, err = p.player.NewStream(*spotId, p.app.cfg.Bitrate, trackPosition)
if err != nil {
return fmt.Errorf("failed creating stream for %s: %w", spotId, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/daemon/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func (p *AppPlayer) handleApiRequest(req ApiRequest) (any, error) {

var skipTo skipToFunc
if len(data.SkipToUri) > 0 {
skipToId, err := librespot.SpotifyIdFromUriSafe(data.SkipToUri)
skipToId, err := librespot.SpotifyIdFromUri(data.SkipToUri)
if err != nil {
log.WithError(err).Warnf("trying to skip to invalid uri: %s", data.SkipToUri)
skipToId = nil
Expand Down
11 changes: 1 addition & 10 deletions ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func SpotifyIdFromGid(typ SpotifyIdType, id []byte) SpotifyId {
return SpotifyId{typ, id}
}

func SpotifyIdFromUriSafe(uri string) (_ *SpotifyId, err error) {
func SpotifyIdFromUri(uri string) (_ *SpotifyId, err error) {
matches := UriRegexp.FindStringSubmatch(uri)
if len(matches) == 0 {
return nil, fmt.Errorf("invalid uri: %s", uri)
Expand All @@ -107,12 +107,3 @@ func SpotifyIdFromUriSafe(uri string) (_ *SpotifyId, err error) {

return &SpotifyId{SpotifyIdType(matches[1]), i.FillBytes(make([]byte, 16))}, nil
}

func SpotifyIdFromUri(uri string) SpotifyId {
id, err := SpotifyIdFromUriSafe(uri)
if err != nil {
panic(err)
}

return *id
}

0 comments on commit 6088a20

Please sign in to comment.