mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-09-23 10:00:49 +02:00
perf(metadata): read complete SAF tags through descriptors
Add complete metadata reads with a display-name hint in Go and both native bridges. Read seekable SAF descriptors directly and use temporary copies only when direct reading fails. Add format parity tests for MP3, FLAC, M4A, and WAV plus a Dart bridge hint regression.
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -43,7 +42,13 @@ func successMethodJSON(method string) (string, error) {
|
||||
}
|
||||
|
||||
func ReadFileMetadata(filePath string) (string, error) {
|
||||
lower := strings.ToLower(filePath)
|
||||
return ReadFileMetadataWithHint(filePath, "")
|
||||
}
|
||||
|
||||
// ReadFileMetadataWithHint reads complete tags from extensionless descriptor
|
||||
// paths without changing their identity or requiring an audio-file copy.
|
||||
func ReadFileMetadataWithHint(filePath, displayNameHint string) (string, error) {
|
||||
lower := resolveLibraryAudioExt(filePath, displayNameHint)
|
||||
isFlac := strings.HasSuffix(lower, ".flac")
|
||||
isM4A := strings.HasSuffix(lower, ".m4a") || strings.HasSuffix(lower, ".mp4") || strings.HasSuffix(lower, ".aac")
|
||||
isMp3 := strings.HasSuffix(lower, ".mp3")
|
||||
@@ -202,7 +207,7 @@ func ReadFileMetadata(filePath string) (string, error) {
|
||||
}
|
||||
}
|
||||
} else if isApe || isWv || isMpc {
|
||||
result["format"] = strings.TrimPrefix(filepath.Ext(filePath), ".")
|
||||
result["format"] = strings.TrimPrefix(lower, ".")
|
||||
result["audio_codec"] = result["format"]
|
||||
apeTag, apeErr := ReadAPETags(filePath)
|
||||
if apeErr == nil && apeTag != nil {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package gobackend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompleteMetadataHintMatchesNamedFileAndDescriptor(t *testing.T) {
|
||||
for _, format := range []string{"mp3", "flac", "m4a", "wav"} {
|
||||
t.Run(format, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "track."+format)
|
||||
switch format {
|
||||
case "mp3":
|
||||
data := buildID3v23Tag(id3TextFrame("TIT2", "Song"), id3TextFrame("TPE1", "Artist"), id3TextFrame("TSRC", "USRC17607839"), id3CommentFrame("USLT", "Words"), id3UserTextFrame("TXXX", "REPLAYGAIN_TRACK_GAIN", "-6.00 dB"), id3UserTextFrame("TXXX", "REPLAYGAIN_ALBUM_GAIN", "-4.00 dB"))
|
||||
if err := os.WriteFile(path, data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "flac":
|
||||
writeSinglePassTestFlac(t, path, nil)
|
||||
case "wav":
|
||||
writeTestWAV(t, path)
|
||||
case "m4a":
|
||||
data, _ := buildTestM4A(t, buildM4ATextAtom("\xa9nam", "Song"), []byte("audio"))
|
||||
if err := os.WriteFile(path, data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
expected, err := ReadFileMetadata(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
extensionless := filepath.Join(dir, "descriptor")
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(extensionless, data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual, err := ReadFileMetadataWithHint(extensionless, "track."+format)
|
||||
if err != nil || actual != expected {
|
||||
t.Fatalf("hinted metadata=%s expected=%s err=%v", actual, expected, err)
|
||||
}
|
||||
// Android uses /proc, which reopens with an independent offset.
|
||||
// macOS /dev/fd duplicates the shared offset and is not that API.
|
||||
if runtime.GOOS != "linux" {
|
||||
return
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
prefix := "/proc/self/fd/"
|
||||
actual, err = ReadFileMetadataWithHint(fmt.Sprintf("%s%d", prefix, file.Fd()), "track."+format)
|
||||
if err != nil || actual != expected {
|
||||
t.Fatalf("descriptor metadata=%s expected=%s err=%v", actual, expected, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user