|
|
@ -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, nil // no cover image
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get(imgURL)
|
|
|
|
// fetch cover image
|
|
|
|
if err != nil {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, imgURL, nil)
|
|
|
|
logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
return nil
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("download image failed: %w", err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
logging.Log().Warn("download image failed", zap.String("http", resp.Status),
|
|
|
|
|
|
|
|
zap.String("url", imgURL))
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil
|
|
|
|
return nil, fmt.Errorf("download image failed: unexpected http status %s", resp.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logging.Log().Warn("download image failed", zap.Error(err), zap.String("url", imgURL))
|
|
|
|
return nil, fmt.Errorf("download image failed: %w", err)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d Decoder) GetMeta() common.Meta {
|
|
|
|
func (d *Decoder) GetMeta() common.Meta {
|
|
|
|
return d.meta
|
|
|
|
return d.meta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|