From ddecd13c08472364eef9bd2e80bc7a35e6518cef Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Mon, 30 Dec 2013 16:39:42 -0500 Subject: [PATCH] Move convenience methods to tag --- id3.go | 36 ++---------------------------------- id3v2.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/id3.go b/id3.go index 2555c38..0fe58a8 100644 --- a/id3.go +++ b/id3.go @@ -12,9 +12,8 @@ import ( // File represents the tagged file type File struct { *Tag - name string - data []byte - Title, Artist, Album, Genre string + name string + data []byte } // Opens a new tagged file @@ -36,20 +35,11 @@ func Open(name string) (*File, error) { tag, name, data, - tag.textFrame("TIT2"), - tag.textFrame("TPE1"), - tag.textFrame("TALB"), - tag.textFrame("TCON"), }, nil } // Saves any edits to the tagged file 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) defer fi.Close() if err != nil { @@ -60,25 +50,3 @@ func (f *File) Close() { wr.Write(f.Tag.Bytes()) 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: - } - } -} diff --git a/id3v2.go b/id3v2.go index 2fa45fc..3e4a6e7 100644 --- a/id3v2.go +++ b/id3v2.go @@ -142,6 +142,60 @@ type Header interface { 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 { data := make([]byte, HeaderSize) n, err := io.ReadFull(reader, data)