Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decode multi valued frames #92

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions v2/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ func decodeText(src []byte, from Encoding) string {
return string(result)
}

// decodeMulti decodes multi valued src from "from" encoding to UTF-8.
func decodeMulti(src []byte, from Encoding) []string {
src = bytes.TrimSuffix(src, from.TerminationBytes) // See https://github.com/bogem/id3v2/issues/41
splitted := bytes.Split(src, from.TerminationBytes)

var res []string
for _, s := range splitted {
res = append(res, decodeText(s, from))
}
return res
}

// encodeWriteText encodes src from UTF-8 to "to" encoding and writes to bw.
func encodeWriteText(bw *bufWriter, src string, to Encoding) error {
if to.Equals(EncodingUTF8) {
Expand Down
60 changes: 60 additions & 0 deletions v2/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,45 @@ func TestParse(t *testing.T) {
testUnknownFrames(t, tag)
}

// TestParse compares parsed frames with expected frames.
func TestMultiParse(t *testing.T) {
tag, err := Open(multiMp3Path, parseOpts)
if tag == nil || err != nil {
t.Error("Error while opening mp3 file:", err)
}
defer tag.Close()

testMultiTextFrames(t, tag)
testMultiTXXXFrames(t, tag)
}

func testMultiTextFrames(t *testing.T, tag *Tag) {
if err := compareTwoStrings(tag.Artist(), "artist1"); err != nil {
t.Error(err)
}
artistFrames := tag.GetFrames(tag.CommonID("Artist"))
if len(artistFrames) != 1 {
t.Fatalf("Expected artist frames: %v, got %v", 1, len(artistFrames))
}

artistFrame, ok := artistFrames[0].(TextFrame)
if !ok {
t.Fatal("Couldn't assert artist frame")
}

if len(artistFrame.Multi) != 2 {
t.Fatalf("Expected artist values: %v, got %v", 2, len(artistFrame.Multi))
}

if err := compareTwoStrings(artistFrame.Multi[0], "artist1"); err != nil {
t.Error(err)
}

if err := compareTwoStrings(artistFrame.Multi[1], "artist2"); err != nil {
t.Error(err)
}
}

func testTextFrames(t *testing.T, tag *Tag) {
if err := compareTwoStrings(tag.Artist(), "Artist"); err != nil {
t.Error(err)
Expand Down Expand Up @@ -173,6 +212,27 @@ func testTXXXFrames(t *testing.T, tag *Tag) {
}
}

func testMultiTXXXFrames(t *testing.T, tag *Tag) {
txxxFrames := tag.GetFrames(tag.CommonID("User defined text information frame"))
if len(txxxFrames) != 1 {
t.Fatalf("Expected TXXX frames: %v, got %v", 1, len(txxxFrames))
}

var parsedUserDefinedTextFrame UserDefinedTextFrame
for _, f := range txxxFrames {
txxx, ok := f.(UserDefinedTextFrame)
if !ok {
t.Fatal("Couldn't assert TXXX frame")
}
parsedUserDefinedTextFrame = txxx
fmt.Printf("%s\n", txxx.Description)
}

if err := compareTXXXFrames(parsedUserDefinedTextFrame, multiUDTF); err != nil {
t.Error(err)
}
}

func compareTXXXFrames(actual, expected UserDefinedTextFrame) error {
if err := compareTwoBytes(actual.Encoding.Key, expected.Encoding.Key); err != nil {
return err
Expand Down
11 changes: 11 additions & 0 deletions v2/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

const (
mp3Path = "testdata/test.mp3"
multiMp3Path = "testdata/test_multi.mp3"
frontCoverPath = "testdata/front_cover.jpg"
backCoverPath = "testdata/back_cover.jpg"

Expand Down Expand Up @@ -62,6 +63,16 @@ var (
Value: "fbd94fb6-2a74-42d0-acbc-81caf8b84984",
}

multiUDTF = UserDefinedTextFrame{
Encoding: EncodingUTF8,
Description: "multi",
Value: "val1",
Multi: []string{
"val1",
"val2",
},
}

musicBrainzUF = UFIDFrame{
OwnerIdentifier: "https://musicbrainz.org",
Identifier: []byte("fbd94fb6-2a74-42d0-acbc-81caf8b84984"),
Expand Down
Binary file modified v2/testdata/test.mp3
Binary file not shown.
Binary file added v2/testdata/test_multi.mp3
Binary file not shown.
10 changes: 9 additions & 1 deletion v2/text_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import "io"
type TextFrame struct {
Encoding Encoding
Text string
Multi []string
}

func (tf TextFrame) Size() int {
Expand Down Expand Up @@ -45,9 +46,16 @@ func parseTextFrame(br *bufReader) (Framer, error) {
return nil, err
}

values := decodeMulti(buf.Bytes(), encoding)
var first string
if len(values) > 0 {
first = values[0]
}

tf := TextFrame{
Encoding: encoding,
Text: decodeText(buf.Bytes(), encoding),
Text: first,
Multi: values,
}

return tf, nil
Expand Down
10 changes: 9 additions & 1 deletion v2/user_defined_text_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type UserDefinedTextFrame struct {
Encoding Encoding
Description string
Value string
Multi []string
}

func (udtf UserDefinedTextFrame) Size() int {
Expand Down Expand Up @@ -42,10 +43,17 @@ func parseUserDefinedTextFrame(br *bufReader, version byte) (Framer, error) {
return nil, err
}

values := decodeMulti(value.Bytes(), encoding)
var first string
if len(values) > 0 {
first = values[0]
}

udtf := UserDefinedTextFrame{
Encoding: encoding,
Description: decodeText(description, encoding),
Value: decodeText(value.Bytes(), encoding),
Value: first,
Multi: values,
}

return udtf, nil
Expand Down
Loading