fix #78 #106: app crash due to imcompatible ncm metadata json

main
鲁树人 2 months ago
parent 8829a3b3ba
commit f753b9c67d

@ -1,6 +1,7 @@
package ncm
import (
"go.uber.org/zap"
"strings"
"unlock-music.dev/cli/algo/common"
@ -17,9 +18,11 @@ type ncmMeta interface {
}
type ncmMetaMusic struct {
logger *zap.Logger
Format string `json:"format"`
MusicName string `json:"musicName"`
Artist [][]interface{} `json:"artist"`
Artist interface{} `json:"artist"`
Album string `json:"album"`
AlbumPicDocID interface{} `json:"albumPicDocId"`
AlbumPic string `json:"albumPic"`
@ -30,20 +33,34 @@ type ncmMetaMusic struct {
TransNames []interface{} `json:"transNames"`
}
func newNcmMetaMusic(logger *zap.Logger) *ncmMetaMusic {
ncm := new(ncmMetaMusic)
ncm.logger = logger.With(zap.String("module", "ncmMetaMusic"))
return ncm
}
func (m *ncmMetaMusic) GetAlbumImageURL() string {
return m.AlbumPic
}
func (m *ncmMetaMusic) GetArtists() (artists []string) {
for _, artist := range m.Artist {
for _, item := range artist {
name, ok := item.(string)
if ok {
func (m *ncmMetaMusic) GetArtists() []string {
m.logger.Debug("ncm artists", zap.Any("artists", m.Artist))
var artists []string = nil
if jsonArtists, ok := m.Artist.([][]string); ok {
for _, artist := range jsonArtists {
for _, name := range artist {
artists = append(artists, name)
}
}
} else if artist, ok := m.Artist.(string); ok {
// #78: artist is a string type.
// https://git.unlock-music.dev/um/cli/issues/78
artists = []string{artist}
} else {
m.logger.Warn("unexpected artist type", zap.Any("artists", m.Artist))
}
return
return artists
}
func (m *ncmMetaMusic) GetTitle() string {

@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"go.uber.org/zap"
"io"
"net/http"
"strings"
@ -30,10 +31,11 @@ var (
)
func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{rd: p.Reader}
return &Decoder{rd: p.Reader, logger: p.Logger.With(zap.String("module", "ncm"))}
}
type Decoder struct {
logger *zap.Logger
rd io.ReadSeeker // rd is the original file reader
offset int
@ -74,7 +76,7 @@ func (d *Decoder) Validate() error {
}
if err := d.parseMeta(); err != nil {
return fmt.Errorf("parse meta failed: %w", err)
return fmt.Errorf("parse meta failed: %w (raw json=%s)", err, string(d.metaRaw))
}
d.cipher = newNcmCipher(keyData)
@ -181,7 +183,7 @@ func (d *Decoder) readCoverData() error {
func (d *Decoder) parseMeta() error {
switch d.metaType {
case "music":
d.meta = new(ncmMetaMusic)
d.meta = newNcmMetaMusic(d.logger)
return json.Unmarshal(d.metaRaw, d.meta)
case "dj":
d.meta = new(ncmMetaDJ)

Loading…
Cancel
Save