kopia lustrzana https://github.com/jcs/id3-go
Move convenience methods to tag
rodzic
1c881033fc
commit
ddecd13c08
36
id3.go
36
id3.go
|
@ -12,9 +12,8 @@ import (
|
||||||
// File represents the tagged file
|
// File represents the tagged file
|
||||||
type File struct {
|
type File struct {
|
||||||
*Tag
|
*Tag
|
||||||
name string
|
name string
|
||||||
data []byte
|
data []byte
|
||||||
Title, Artist, Album, Genre string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opens a new tagged file
|
// Opens a new tagged file
|
||||||
|
@ -36,20 +35,11 @@ func Open(name string) (*File, error) {
|
||||||
tag,
|
tag,
|
||||||
name,
|
name,
|
||||||
data,
|
data,
|
||||||
tag.textFrame("TIT2"),
|
|
||||||
tag.textFrame("TPE1"),
|
|
||||||
tag.textFrame("TALB"),
|
|
||||||
tag.textFrame("TCON"),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Saves any edits to the tagged file
|
// Saves any edits to the tagged file
|
||||||
func (f *File) Close() {
|
func (f *File) Close() {
|
||||||
f.setTextFrame("TIT2", f.Title)
|
|
||||||
f.setTextFrame("TPE1", f.Artist)
|
|
||||||
f.setTextFrame("TALB", f.Album)
|
|
||||||
f.setTextFrame("TCON", f.Genre)
|
|
||||||
|
|
||||||
fi, err := os.Create(f.name)
|
fi, err := os.Create(f.name)
|
||||||
defer fi.Close()
|
defer fi.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -60,25 +50,3 @@ func (f *File) Close() {
|
||||||
wr.Write(f.Tag.Bytes())
|
wr.Write(f.Tag.Bytes())
|
||||||
wr.Write(f.data)
|
wr.Write(f.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tag) textFrame(id string) string {
|
|
||||||
if frame := t.Frame(id); frame != nil {
|
|
||||||
switch frame.(type) {
|
|
||||||
case (*TextFrame):
|
|
||||||
return frame.(*TextFrame).Text()
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Tag) setTextFrame(id, text string) {
|
|
||||||
if frame := t.Frame(id); frame != nil {
|
|
||||||
switch frame.(type) {
|
|
||||||
case (*TextFrame):
|
|
||||||
frame.(*TextFrame).SetText(text)
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
54
id3v2.go
54
id3v2.go
|
@ -142,6 +142,60 @@ type Header interface {
|
||||||
Bytes() []byte
|
Bytes() []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t Tag) Title() string {
|
||||||
|
return t.textFrameText("TIT2")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Tag) Artist() string {
|
||||||
|
return t.textFrameText("TPE1")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Tag) Album() string {
|
||||||
|
return t.textFrameText("TALB")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Tag) Genre() string {
|
||||||
|
return t.textFrameText("TCON")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tag) SetTitle(text string) {
|
||||||
|
t.setTextFrameText("TIT2", text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tag) SetArtist(text string) {
|
||||||
|
t.setTextFrameText("TPE1", text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tag) SetAlbum(text string) {
|
||||||
|
t.setTextFrameText("TALB", text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tag) SetGenre(text string) {
|
||||||
|
t.setTextFrameText("TCON", text)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Tag) textFrameText(id string) string {
|
||||||
|
if frame := t.Frame(id); frame != nil {
|
||||||
|
switch frame.(type) {
|
||||||
|
case (*TextFrame):
|
||||||
|
return frame.(*TextFrame).Text()
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tag) setTextFrameText(id, text string) {
|
||||||
|
if frame := t.Frame(id); frame != nil {
|
||||||
|
switch frame.(type) {
|
||||||
|
case (*TextFrame):
|
||||||
|
frame.(*TextFrame).SetText(text)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewHeader(reader io.Reader) Header {
|
func NewHeader(reader io.Reader) Header {
|
||||||
data := make([]byte, HeaderSize)
|
data := make([]byte, HeaderSize)
|
||||||
n, err := io.ReadFull(reader, data)
|
n, err := io.ReadFull(reader, data)
|
||||||
|
|
Ładowanie…
Reference in New Issue