fix(qmc): fix key from mmkv
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Unlock Music Dev 2022-12-05 08:54:40 +08:00
parent a9c976f47d
commit 423767ba63
Signed by: um-dev
GPG Key ID: 95202E10D3413A1D
4 changed files with 36 additions and 11 deletions

View File

@ -4,6 +4,8 @@ import (
"io"
"path/filepath"
"strings"
"go.uber.org/zap"
)
type DecoderParams struct {
@ -11,6 +13,8 @@ type DecoderParams struct {
Extension string // required, source extension, eg. ".mp3"
FilePath string // optional, source file path
Logger *zap.Logger // required
}
type NewDecoderFunc func(p *DecoderParams) Decoder

View File

@ -8,12 +8,14 @@ import (
"runtime"
"strings"
"go.uber.org/zap"
"unlock-music.dev/mmkv"
)
var streamKeyVault mmkv.Vault
func readKeyFromMMKV(file string) ([]byte, error) {
// TODO: move to factory
func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) {
if file == "" {
return nil, errors.New("file path is required while reading key from mmkv")
}
@ -31,6 +33,7 @@ func readKeyFromMMKV(file string) ([]byte, error) {
return nil, fmt.Errorf("mmkv key valut not found: %w", err)
}
}
mgr, err := mmkv.NewManager(mmkvDir)
if err != nil {
return nil, fmt.Errorf("init mmkv manager: %w", err)
@ -40,6 +43,8 @@ func readKeyFromMMKV(file string) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("open mmkv vault: %w", err)
}
logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys()))
}
buf, err := streamKeyVault.GetBytes(file)
@ -52,9 +57,12 @@ func readKeyFromMMKV(file string) ([]byte, error) {
}
buf, err = streamKeyVault.GetBytes(key)
if err != nil {
// TODO: logger
logger.Warn("read key from mmkv", zap.String("key", key), zap.Error(err))
}
}
// TODO: use editorial judgement to select the best match
// since MacOS may change some characters in the file name.
// eg. "ぜ" -> "ぜ"
}
if len(buf) == 0 {

View File

@ -6,9 +6,12 @@ import (
"errors"
"fmt"
"io"
"runtime"
"strconv"
"strings"
"go.uber.org/zap"
"unlock-music.dev/cli/algo/common"
"unlock-music.dev/cli/internal/sniff"
)
@ -26,6 +29,8 @@ type Decoder struct {
rawMetaExtra1 int
rawMetaExtra2 int
logger *zap.Logger
}
// Read implements io.Reader, offer the decrypted audio data.
@ -40,7 +45,7 @@ func (d *Decoder) Read(p []byte) (int, error) {
}
func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{raw: p.Reader, params: p}
return &Decoder{raw: p.Reader, params: p, logger: p.Logger}
}
func (d *Decoder) Validate() error {
@ -99,15 +104,21 @@ func (d *Decoder) validateDecode() error {
}
func (d *Decoder) searchKey() (err error) {
if d.params.Extension == ".mflach" {
d.decodedKey, err = readKeyFromMMKV(d.params.FilePath)
return err
}
fileSizeM4, err := d.raw.Seek(-4, io.SeekEnd)
if err != nil {
return err
}
fileSize := int(fileSizeM4) + 4
//goland:noinspection GoBoolExpressions
if runtime.GOOS == "darwin" {
d.decodedKey, err = readKeyFromMMKV(d.params.FilePath, d.logger)
if err == nil {
d.audioLen = fileSize
return
}
d.logger.Warn("read key from mmkv failed", zap.Error(err))
}
suffixBuf := make([]byte, 4)
if _, err := io.ReadFull(d.raw, suffixBuf); err != nil {
@ -127,7 +138,7 @@ func (d *Decoder) searchKey() (err error) {
}
// try to use default static cipher
d.audioLen = int(fileSizeM4 + 4)
d.audioLen = fileSize
return nil
}

View File

@ -148,9 +148,10 @@ func dealDirectory(inputDir string, outputDir string, skipNoop bool, removeSourc
continue
}
err := tryDecFile(filepath.Join(inputDir, item.Name()), outputDir, allDec, removeSource)
filePath := filepath.Join(inputDir, item.Name())
err := tryDecFile(filePath, outputDir, allDec, removeSource)
if err != nil {
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
logger.Error("conversion failed", zap.String("source", filePath), zap.Error(err))
}
}
return nil
@ -167,6 +168,7 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
Reader: file,
Extension: filepath.Ext(inputFile),
FilePath: inputFile,
Logger: logger.With(zap.String("source", inputFile)),
}
var dec common.Decoder