From a92ea82090ad41c930840ef2423b601a371e7c2b Mon Sep 17 00:00:00 2001 From: Emmm Monster <58943012+emmmx@users.noreply.github.com> Date: Sat, 26 Dec 2020 02:53:28 +0800 Subject: [PATCH] Update NCM, QMC Decoder --- algo/common/common.go | 2 +- algo/ncm/ncm.go | 42 +++++++++++++++++++++--------------------- algo/qmc/qmc.go | 15 +++++++-------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/algo/common/common.go b/algo/common/common.go index 1b8bea9..c25f2f5 100644 --- a/algo/common/common.go +++ b/algo/common/common.go @@ -1,7 +1,7 @@ package common type Decoder interface { - Validate() bool + Validate() error Decode() error GetCoverImage() []byte GetAudioData() []byte diff --git a/algo/ncm/ncm.go b/algo/ncm/ncm.go index 2687bc6..07592e0 100644 --- a/algo/ncm/ncm.go +++ b/algo/ncm/ncm.go @@ -42,10 +42,10 @@ type Decoder struct { metaRaw []byte metaType string - Meta RawMeta + meta RawMeta - Cover []byte - Audio []byte + cover []byte + audio []byte offsetKey uint32 offsetMeta uint32 @@ -53,16 +53,16 @@ type Decoder struct { offsetAudio uint32 } -func (d *Decoder) Validate() bool { +func (d *Decoder) Validate() error { if !bytes.Equal(magicHeader, d.file[:len(magicHeader)]) { - return false + return errors.New("ncm magic header not match") } /*if status.IsDebug { logging.Log().Info("the unknown field of the header is: \n" + spew.Sdump(d.file[8:10])) }*/ d.offsetKey = 8 + 2 - return true + return nil } //todo: 读取前进行检查长度,防止越界 @@ -141,11 +141,11 @@ func (d *Decoder) buildKeyBox() { func (d *Decoder) parseMeta() error { switch d.metaType { case "music": - d.Meta = new(RawMetaMusic) - return json.Unmarshal(d.metaRaw, d.Meta) + d.meta = new(RawMetaMusic) + return json.Unmarshal(d.metaRaw, d.meta) case "dj": - d.Meta = new(RawMetaDJ) - return json.Unmarshal(d.metaRaw, d.Meta) + d.meta = new(RawMetaDJ) + return json.Unmarshal(d.metaRaw, d.meta) default: return errors.New("unknown ncm meta type: " + d.metaType) } @@ -173,7 +173,7 @@ func (d *Decoder) readCoverData() error { if iCoverLen == 0 { return errors.New("no any cover file found") } - d.Cover = d.file[coverLenStart+4 : 4+coverLenStart+iCoverLen] + d.cover = d.file[coverLenStart+4 : 4+coverLenStart+iCoverLen] return nil } @@ -183,9 +183,9 @@ func (d *Decoder) readAudioData() error { } audioRaw := d.file[d.offsetAudio:] audioLen := len(audioRaw) - d.Audio = make([]byte, audioLen) + d.audio = make([]byte, audioLen) for i := uint32(0); i < uint32(audioLen); i++ { - d.Audio[i] = d.box[i&0xff] ^ audioRaw[i] + d.audio[i] = d.box[i&0xff] ^ audioRaw[i] } return nil } @@ -213,23 +213,23 @@ func (d *Decoder) Decode() error { } func (d Decoder) GetAudioExt() string { - if d.Meta != nil { - return d.Meta.GetFormat() + if d.meta != nil { + return d.meta.GetFormat() } return "" } func (d Decoder) GetAudioData() []byte { - return d.Audio + return d.audio } func (d Decoder) GetCoverImage() []byte { - if d.Cover != nil { - return d.Cover + if d.cover != nil { + return d.cover } { - imgURL := d.Meta.GetAlbumImageURL() - if d.Meta != nil && !strings.HasPrefix(imgURL, "http") { + imgURL := d.meta.GetAlbumImageURL() + if d.meta != nil && !strings.HasPrefix(imgURL, "http") { return nil } resp, err := http.Get(imgURL) @@ -253,5 +253,5 @@ func (d Decoder) GetCoverImage() []byte { } func (d Decoder) GetMeta() common.Meta { - return d.Meta + return d.meta } diff --git a/algo/qmc/qmc.go b/algo/qmc/qmc.go index 45da8a8..a5d88f1 100644 --- a/algo/qmc/qmc.go +++ b/algo/qmc/qmc.go @@ -5,8 +5,6 @@ import ( "encoding/binary" "errors" "github.com/umlock-music/cli/algo/common" - "github.com/umlock-music/cli/internal/logging" - "go.uber.org/zap" ) var ( @@ -36,18 +34,19 @@ func NewMgg256Decoder(data []byte) *Decoder { return &Decoder{file: data, maskDetector: detectMgg256Mask, audioExt: "ogg"} } -func (d *Decoder) Validate() bool { +func (d *Decoder) Validate() error { if nil != d.mask { - return true + return nil } if nil != d.maskDetector { if err := d.validateKey(); err != nil { - logging.Log().Error("detect file failed", zap.Error(err)) - return false + return err } - d.mask, _ = d.maskDetector(d.file) + var err error + d.mask, err = d.maskDetector(d.file) + return err } - return d.mask != nil + return errors.New("no mask or mask detector found") } func (d *Decoder) validateKey() error {