refactor: remove logging in algorithms

This commit is contained in:
Unlock Music Dev 2022-11-19 07:25:40 +08:00
parent a2c55721cc
commit e2fc56ddb2
Signed by: um-dev
GPG Key ID: 95202E10D3413A1D
8 changed files with 50 additions and 74 deletions

View File

@ -1,14 +1,19 @@
package common package common
import "context"
type Decoder interface { type Decoder interface {
Validate() error Validate() error
Decode() error Decode() error
GetCoverImage() []byte
GetAudioData() []byte GetAudioData() []byte
GetAudioExt() string GetAudioExt() string
GetMeta() Meta GetMeta() Meta
} }
type CoverImageGetter interface {
GetCoverImage(ctx context.Context) ([]byte, error)
}
type Meta interface { type Meta interface {
GetArtists() []string GetArtists() []string
GetTitle() string GetTitle() string

View File

@ -28,10 +28,6 @@ func (d RawDecoder) Decode() error {
return nil return nil
} }
func (d RawDecoder) GetCoverImage() []byte {
return nil
}
func (d RawDecoder) GetAudioData() []byte { func (d RawDecoder) GetAudioData() []byte {
return d.file return d.file
} }

View File

@ -31,10 +31,6 @@ func NewDecoder(file []byte) common.Decoder {
} }
} }
func (d *Decoder) GetCoverImage() []byte {
return nil
}
func (d *Decoder) GetAudioData() []byte { func (d *Decoder) GetAudioData() []byte {
return d.audio return d.audio
} }

View File

@ -4,10 +4,11 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors" "errors"
"github.com/unlock-music/cli/algo/common"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
"github.com/unlock-music/cli/algo/common"
) )
var ( var (
@ -31,10 +32,6 @@ type Decoder struct {
audio []byte audio []byte
} }
func (d *Decoder) GetCoverImage() []byte {
return nil
}
func (d *Decoder) GetAudioData() []byte { func (d *Decoder) GetAudioData() []byte {
return d.audio return d.audio
} }

View File

@ -2,17 +2,18 @@ package ncm
import ( import (
"bytes" "bytes"
"context"
"encoding/base64" "encoding/base64"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/unlock-music/cli/algo/common" "fmt"
"github.com/unlock-music/cli/internal/logging" "io"
"github.com/unlock-music/cli/internal/utils"
"go.uber.org/zap"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
"github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/utils"
) )
var ( var (
@ -157,9 +158,9 @@ func (d *Decoder) readCoverData() error {
iCoverLen := binary.LittleEndian.Uint32(bCoverLen) iCoverLen := binary.LittleEndian.Uint32(bCoverLen)
d.offsetAudio = coverLenStart + 4 + iCoverLen d.offsetAudio = coverLenStart + 4 + iCoverLen
if iCoverLen == 0 { if iCoverLen == 0 {
return errors.New("no any cover file found") return nil
} }
d.cover = d.file[coverLenStart+4 : 4+coverLenStart+iCoverLen] d.cover = d.file[coverLenStart+4 : coverLenStart+4+iCoverLen]
return nil return nil
} }
@ -178,27 +179,25 @@ func (d *Decoder) readAudioData() error {
func (d *Decoder) Decode() error { func (d *Decoder) Decode() error {
if err := d.readKeyData(); err != nil { if err := d.readKeyData(); err != nil {
return err return fmt.Errorf("read key data failed: %w", err)
} }
d.buildKeyBox() d.buildKeyBox()
err := d.readMetaData() if err := d.readMetaData(); err != nil {
if err == nil { return fmt.Errorf("read meta date failed: %w", err)
err = d.parseMeta()
} }
if err != nil { if err := d.parseMeta(); err != nil {
logging.Log().Warn("parse ncm meta file failed", zap.Error(err)) return fmt.Errorf("parse meta failed: %w", err)
} }
err = d.readCoverData() if err := d.readCoverData(); err != nil {
if err != nil { return fmt.Errorf("parse ncm cover file failed: %w", err)
logging.Log().Warn("parse ncm cover file failed", zap.Error(err))
} }
return d.readAudioData() return d.readAudioData()
} }
func (d Decoder) GetAudioExt() string { func (d *Decoder) GetAudioExt() string {
if d.meta != nil { if d.meta != nil {
if format := d.meta.GetFormat(); format != "" { if format := d.meta.GetFormat(); format != "" {
return "." + d.meta.GetFormat() return "." + d.meta.GetFormat()
@ -207,40 +206,38 @@ func (d Decoder) GetAudioExt() string {
return "" return ""
} }
func (d Decoder) GetAudioData() []byte { func (d *Decoder) GetAudioData() []byte {
return d.audio return d.audio
} }
func (d Decoder) GetCoverImage() []byte { func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
if d.cover != nil { if d.cover != nil {
return d.cover return d.cover, nil
} }
{
imgURL := d.meta.GetAlbumImageURL() imgURL := d.meta.GetAlbumImageURL()
if d.meta != nil && !strings.HasPrefix(imgURL, "http") { if d.meta != nil && !strings.HasPrefix(imgURL, "http") {
return nil return nil, nil // no cover image
}
resp, err := http.Get(imgURL)
if err != nil {
logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
return nil
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
logging.Log().Warn("download image failed", zap.String("http", resp.Status),
zap.String("url", imgURL))
return nil
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
return nil
}
return data
}
} }
func (d Decoder) GetMeta() common.Meta { // fetch cover image
req, err := http.NewRequestWithContext(ctx, http.MethodGet, imgURL, nil)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("download image failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("download image failed: unexpected http status %s", resp.Status)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("download image failed: %w", err)
}
return data, nil
}
func (d *Decoder) GetMeta() common.Meta {
return d.meta return d.meta
} }

View File

@ -244,10 +244,6 @@ func (c *compactDecoder) Decode() error {
return err return err
} }
func (c *compactDecoder) GetCoverImage() []byte {
return nil
}
func (c *compactDecoder) GetAudioData() []byte { func (c *compactDecoder) GetAudioData() []byte {
return c.buf.Bytes() return c.buf.Bytes()
} }

View File

@ -3,6 +3,7 @@ package tm
import ( import (
"bytes" "bytes"
"errors" "errors"
"github.com/unlock-music/cli/algo/common" "github.com/unlock-music/cli/algo/common"
) )
@ -16,10 +17,6 @@ type Decoder struct {
audioExt string audioExt string
} }
func (d *Decoder) GetCoverImage() []byte {
return nil
}
func (d *Decoder) GetAudioData() []byte { func (d *Decoder) GetAudioData() []byte {
return d.audio return d.audio
} }

View File

@ -3,9 +3,8 @@ package xm
import ( import (
"bytes" "bytes"
"errors" "errors"
"github.com/unlock-music/cli/algo/common" "github.com/unlock-music/cli/algo/common"
"github.com/unlock-music/cli/internal/logging"
"go.uber.org/zap"
) )
var ( var (
@ -29,10 +28,6 @@ type Decoder struct {
audio []byte audio []byte
} }
func (d *Decoder) GetCoverImage() []byte {
return nil
}
func (d *Decoder) GetAudioData() []byte { func (d *Decoder) GetAudioData() []byte {
return d.audio return d.audio
} }
@ -69,9 +64,6 @@ func (d *Decoder) Validate() error {
return errors.New("detect unknown xm file type: " + string(d.file[4:8])) return errors.New("detect unknown xm file type: " + string(d.file[4:8]))
} }
if d.file[14] != 0 {
logging.Log().Warn("not a simple xm file", zap.Uint8("b[14]", d.file[14]))
}
d.headerLen = uint32(d.file[12]) | uint32(d.file[13])<<8 | uint32(d.file[14])<<16 // LittleEndian Unit24 d.headerLen = uint32(d.file[12]) | uint32(d.file[13])<<8 | uint32(d.file[14])<<16 // LittleEndian Unit24
if d.headerLen+16 > uint32(lenData) { if d.headerLen+16 > uint32(lenData) {
return ErrFileSize return ErrFileSize