refactor: move audio sniffer to internal package

This commit is contained in:
Unlock Music Dev
2022-11-22 06:16:40 +08:00
parent 62a38d5ab4
commit 6c168ee536
8 changed files with 86 additions and 72 deletions

View File

@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"io"
"unlock-music.dev/cli/internal/sniff"
)
type RawDecoder struct {
@ -26,7 +28,7 @@ func (d *RawDecoder) Validate() error {
}
var ok bool
d.audioExt, ok = SniffAll(header)
d.audioExt, ok = sniff.AudioExtension(header)
if !ok {
return errors.New("raw: sniff audio type failed")
}

View File

@ -1,55 +0,0 @@
package common
import "bytes"
type Sniffer func(header []byte) bool
var snifferRegistry = map[string]Sniffer{
".mp3": SnifferMP3,
".flac": SnifferFLAC,
".ogg": SnifferOGG,
".m4a": SnifferM4A,
".wav": SnifferWAV,
".wma": SnifferWMA,
".aac": SnifferAAC,
".dff": SnifferDFF,
}
func SniffAll(header []byte) (string, bool) {
for ext, sniffer := range snifferRegistry {
if sniffer(header) {
return ext, true
}
}
return "", false
}
func SnifferM4A(header []byte) bool {
return len(header) >= 8 && bytes.Equal([]byte("ftyp"), header[4:8])
}
func SnifferOGG(header []byte) bool {
return bytes.HasPrefix(header, []byte("OggS"))
}
func SnifferFLAC(header []byte) bool {
return bytes.HasPrefix(header, []byte("fLaC"))
}
func SnifferMP3(header []byte) bool {
return bytes.HasPrefix(header, []byte("ID3"))
}
func SnifferWAV(header []byte) bool {
return bytes.HasPrefix(header, []byte("RIFF"))
}
func SnifferWMA(header []byte) bool {
return bytes.HasPrefix(header, []byte("\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00\xaa\x00\x62\xce\x6c"))
}
func SnifferAAC(header []byte) bool {
return bytes.HasPrefix(header, []byte{0xFF, 0xF1})
}
// SnifferDFF sniff a DSDIFF format
// reference to: https://www.sonicstudio.com/pdf/dsd/DSDIFF_1.5_Spec.pdf
func SnifferDFF(header []byte) bool {
return bytes.HasPrefix(header, []byte("FRM8"))
}

View File

@ -206,8 +206,12 @@ func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
if d.cover != nil {
return d.cover, nil
}
if d.meta == nil {
return nil, errors.New("ncm meta not found")
}
imgURL := d.meta.GetAlbumImageURL()
if d.meta != nil && !strings.HasPrefix(imgURL, "http") {
if !strings.HasPrefix(imgURL, "http") {
return nil, nil // no cover image
}
@ -215,18 +219,19 @@ func (d *Decoder) GetCoverImage(ctx context.Context) ([]byte, error) {
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)
return nil, fmt.Errorf("ncm 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)
return nil, fmt.Errorf("ncm download image failed: unexpected http status %s", resp.Status)
}
data, err := io.ReadAll(resp.Body)
d.cover, err = io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("download image failed: %w", err)
return nil, fmt.Errorf("ncm download image failed: %w", err)
}
return data, nil
return d.cover, nil
}
func (d *Decoder) GetMeta() common.Meta {

View File

@ -10,6 +10,7 @@ import (
"strings"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff"
)
type Decoder struct {
@ -89,7 +90,7 @@ func (d *Decoder) validateDecode() error {
}
d.cipher.Decrypt(buf, 0)
_, ok := common.SniffAll(buf)
_, ok := sniff.AudioExtension(buf)
if !ok {
return errors.New("qmc: detect file type failed")
}

View File

@ -7,6 +7,7 @@ import (
"io"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff"
)
var replaceHeader = []byte{0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70}
@ -30,7 +31,7 @@ func (d *Decoder) Validate() error {
return nil
}
if _, ok := common.SniffAll(header); ok { // not encrypted
if _, ok := sniff.AudioExtension(header); ok { // not encrypted
d.audio = io.MultiReader(bytes.NewReader(header), d.raw)
return nil
}

View File

@ -6,6 +6,7 @@ import (
"io"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff"
)
type Decoder struct {
@ -27,7 +28,7 @@ func (d *Decoder) Validate() error {
{ // try to decode with x2m
header := decryptX2MHeader(encryptedHeader)
if _, ok := common.SniffAll(header); ok {
if _, ok := sniff.AudioExtension(header); ok {
d.audio = io.MultiReader(bytes.NewReader(header), d.rd)
return nil
}
@ -36,7 +37,7 @@ func (d *Decoder) Validate() error {
{ // try to decode with x3m
// not read file again, since x2m and x3m have the same header size
header := decryptX3MHeader(encryptedHeader)
if _, ok := common.SniffAll(header); ok {
if _, ok := sniff.AudioExtension(header); ok {
d.audio = io.MultiReader(bytes.NewReader(header), d.rd)
return nil
}