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

This commit is contained in:
鲁树人 2024-11-02 13:44:29 +09:00
parent 8829a3b3ba
commit f753b9c67d
2 changed files with 40 additions and 21 deletions

View File

@ -1,6 +1,7 @@
package ncm package ncm
import ( import (
"go.uber.org/zap"
"strings" "strings"
"unlock-music.dev/cli/algo/common" "unlock-music.dev/cli/algo/common"
@ -17,33 +18,49 @@ type ncmMeta interface {
} }
type ncmMetaMusic struct { type ncmMetaMusic struct {
Format string `json:"format"` logger *zap.Logger
MusicName string `json:"musicName"`
Artist [][]interface{} `json:"artist"` Format string `json:"format"`
Album string `json:"album"` MusicName string `json:"musicName"`
AlbumPicDocID interface{} `json:"albumPicDocId"` Artist interface{} `json:"artist"`
AlbumPic string `json:"albumPic"` Album string `json:"album"`
Flag int `json:"flag"` AlbumPicDocID interface{} `json:"albumPicDocId"`
Bitrate int `json:"bitrate"` AlbumPic string `json:"albumPic"`
Duration int `json:"duration"` Flag int `json:"flag"`
Alias []interface{} `json:"alias"` Bitrate int `json:"bitrate"`
TransNames []interface{} `json:"transNames"` Duration int `json:"duration"`
Alias []interface{} `json:"alias"`
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 { func (m *ncmMetaMusic) GetAlbumImageURL() string {
return m.AlbumPic return m.AlbumPic
} }
func (m *ncmMetaMusic) GetArtists() (artists []string) { func (m *ncmMetaMusic) GetArtists() []string {
for _, artist := range m.Artist { m.logger.Debug("ncm artists", zap.Any("artists", m.Artist))
for _, item := range artist {
name, ok := item.(string) var artists []string = nil
if ok { if jsonArtists, ok := m.Artist.([][]string); ok {
for _, artist := range jsonArtists {
for _, name := range artist {
artists = append(artists, name) 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 { func (m *ncmMetaMusic) GetTitle() string {

View File

@ -8,6 +8,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"go.uber.org/zap"
"io" "io"
"net/http" "net/http"
"strings" "strings"
@ -30,11 +31,12 @@ var (
) )
func NewDecoder(p *common.DecoderParams) common.Decoder { 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 { type Decoder struct {
rd io.ReadSeeker // rd is the original file reader logger *zap.Logger
rd io.ReadSeeker // rd is the original file reader
offset int offset int
cipher common.StreamDecoder cipher common.StreamDecoder
@ -74,7 +76,7 @@ func (d *Decoder) Validate() error {
} }
if err := d.parseMeta(); err != nil { 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) d.cipher = newNcmCipher(keyData)
@ -181,7 +183,7 @@ func (d *Decoder) readCoverData() error {
func (d *Decoder) parseMeta() error { func (d *Decoder) parseMeta() error {
switch d.metaType { switch d.metaType {
case "music": case "music":
d.meta = new(ncmMetaMusic) d.meta = newNcmMetaMusic(d.logger)
return json.Unmarshal(d.metaRaw, d.meta) return json.Unmarshal(d.metaRaw, d.meta)
case "dj": case "dj":
d.meta = new(ncmMetaDJ) d.meta = new(ncmMetaDJ)