feat(qmc): use editorial distance to find the key
This commit is contained in:
@ -8,7 +8,10 @@ import (
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/hbollon/go-edlib"
|
||||
"github.com/samber/lo"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/exp/slices"
|
||||
"unlock-music.dev/mmkv"
|
||||
)
|
||||
|
||||
@ -47,11 +50,13 @@ func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) {
|
||||
logger.Debug("mmkv vault opened", zap.Strings("keys", streamKeyVault.Keys()))
|
||||
}
|
||||
|
||||
_, partName := filepath.Split(file)
|
||||
buf, err := streamKeyVault.GetBytes(file)
|
||||
if err != nil { // fallback match filename only
|
||||
_, partName := filepath.Split(file)
|
||||
keys := streamKeyVault.Keys()
|
||||
for _, key := range keys {
|
||||
|
||||
if buf == nil {
|
||||
filePaths := streamKeyVault.Keys()
|
||||
|
||||
for _, key := range filePaths { // fallback 1: match filename only
|
||||
if !strings.HasSuffix(key, partName) {
|
||||
continue
|
||||
}
|
||||
@ -60,9 +65,31 @@ func readKeyFromMMKV(file string, logger *zap.Logger) ([]byte, error) {
|
||||
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 buf == nil { // fallback 2: match filename with edit distance
|
||||
// use editorial judgement to select the best match
|
||||
// since macOS may change some characters in the file name.
|
||||
// e.g. "ぜ"(e3 81 9c) -> "ぜ"(e3 81 9b e3 82 99)
|
||||
fileNames := lo.Map(filePaths, func(filePath string, _ int) string {
|
||||
_, name := filepath.Split(filePath)
|
||||
return name
|
||||
})
|
||||
|
||||
minDisStr, err := edlib.FuzzySearch(partName, fileNames, edlib.Levenshtein)
|
||||
if err != nil {
|
||||
logger.Warn("fuzzy search failed", zap.Error(err))
|
||||
}
|
||||
|
||||
// TODO: make distance configurable
|
||||
// for now, assume only 1 character changed to 2 characters
|
||||
if edlib.LevenshteinDistance(partName, minDisStr) < 3 {
|
||||
idx := slices.Index(fileNames, minDisStr)
|
||||
buf, err = streamKeyVault.GetBytes(filePaths[idx])
|
||||
if err != nil {
|
||||
logger.Warn("read key from mmkv", zap.String("key", minDisStr), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(buf) == 0 {
|
||||
|
@ -111,7 +111,7 @@ func (d *Decoder) searchKey() (err error) {
|
||||
fileSize := int(fileSizeM4) + 4
|
||||
|
||||
//goland:noinspection GoBoolExpressions
|
||||
if runtime.GOOS == "darwin" {
|
||||
if runtime.GOOS == "darwin" && !strings.HasPrefix(d.params.Extension, ".qmc") {
|
||||
d.decodedKey, err = readKeyFromMMKV(d.params.FilePath, d.logger)
|
||||
if err == nil {
|
||||
d.audioLen = fileSize
|
||||
|
Reference in New Issue
Block a user