Browse Source

Improved getters ᗜˬᗜ

main
Unbewohnte 3 years ago
parent
commit
a564353374
  1. 5
      v2/read_test.go
  2. 60
      v2/v2tag.go

5
v2/read_test.go

@ -29,4 +29,9 @@ func TestReadV2Tag(t *testing.T) {
t.Errorf("ReadV2Tag failed: expected contents of the album frame to be %s; got %s", t.Errorf("ReadV2Tag failed: expected contents of the album frame to be %s; got %s",
"album", album) "album", album)
} }
picture := tag.Picture()
if picture != nil {
t.Errorf("ReadV2Tag failed: expected it not to have a picture")
}
} }

60
v2/v2tag.go

@ -1,5 +1,7 @@
package v2 package v2
import "strings"
type ID3v2Tag struct { type ID3v2Tag struct {
Header Header Header Header
Frames []Frame Frames []Frame
@ -9,19 +11,35 @@ type ID3v2Tag struct {
// returns &it if found // returns &it if found
func (tag *ID3v2Tag) GetFrame(id string) *Frame { func (tag *ID3v2Tag) GetFrame(id string) *Frame {
for _, frame := range tag.Frames { for _, frame := range tag.Frames {
if frame.Header.ID == id { if strings.EqualFold(frame.Header.ID, id) {
return &frame return &frame
} }
} }
return nil return nil
} }
// Checks if a frame with given id exists
func (tag *ID3v2Tag) FrameExists(id string) bool {
for _, frame := range tag.Frames {
if strings.EqualFold(frame.Header.ID, id) {
return true
}
}
return false
}
// Returns the contents for the title frame // Returns the contents for the title frame
func (tag *ID3v2Tag) Title() string { func (tag *ID3v2Tag) Title() string {
switch tag.Header.Version { switch tag.Header.Version {
case V2_2: case V2_2:
if !tag.FrameExists("TT2") {
return ""
}
return tag.GetFrame("TT2").Text() return tag.GetFrame("TT2").Text()
default: default:
if !tag.FrameExists("TIT2") {
return ""
}
return tag.GetFrame("TIT2").Text() return tag.GetFrame("TIT2").Text()
} }
} }
@ -30,8 +48,14 @@ func (tag *ID3v2Tag) Title() string {
func (tag *ID3v2Tag) Album() string { func (tag *ID3v2Tag) Album() string {
switch tag.Header.Version { switch tag.Header.Version {
case V2_2: case V2_2:
if !tag.FrameExists("TAL") {
return ""
}
return tag.GetFrame("TAL").Text() return tag.GetFrame("TAL").Text()
default: default:
if !tag.FrameExists("TALB") {
return ""
}
return tag.GetFrame("TALB").Text() return tag.GetFrame("TALB").Text()
} }
} }
@ -40,8 +64,14 @@ func (tag *ID3v2Tag) Album() string {
func (tag *ID3v2Tag) Artist() string { func (tag *ID3v2Tag) Artist() string {
switch tag.Header.Version { switch tag.Header.Version {
case V2_2: case V2_2:
if !tag.FrameExists("TP1") {
return ""
}
return tag.GetFrame("TP1").Text() return tag.GetFrame("TP1").Text()
default: default:
if !tag.FrameExists("TPE1") {
return ""
}
return tag.GetFrame("TPE1").Text() return tag.GetFrame("TPE1").Text()
} }
} }
@ -50,8 +80,14 @@ func (tag *ID3v2Tag) Artist() string {
func (tag *ID3v2Tag) Year() string { func (tag *ID3v2Tag) Year() string {
switch tag.Header.Version { switch tag.Header.Version {
case V2_2: case V2_2:
if !tag.FrameExists("TYE") {
return ""
}
return tag.GetFrame("TYE").Text() return tag.GetFrame("TYE").Text()
default: default:
if !tag.FrameExists("TYER") {
return ""
}
return tag.GetFrame("TYER").Text() return tag.GetFrame("TYER").Text()
} }
} }
@ -60,8 +96,30 @@ func (tag *ID3v2Tag) Year() string {
func (tag *ID3v2Tag) Comment() string { func (tag *ID3v2Tag) Comment() string {
switch tag.Header.Version { switch tag.Header.Version {
case V2_2: case V2_2:
if !tag.FrameExists("COM") {
return ""
}
return tag.GetFrame("COM").Text() return tag.GetFrame("COM").Text()
default: default:
if !tag.FrameExists("COMM") {
return ""
}
return tag.GetFrame("COMM").Text() return tag.GetFrame("COMM").Text()
} }
} }
// Returns raw bytes of embed picture
func (tag *ID3v2Tag) Picture() []byte {
switch tag.Header.Version {
case V2_2:
if !tag.FrameExists("PIC") {
return nil
}
return tag.GetFrame("PIC").Contents
default:
if !tag.FrameExists("APIC") {
return nil
}
return tag.GetFrame("APIC").Contents
}
}

Loading…
Cancel
Save