2013-12-21 22:31:03 +00:00
|
|
|
// Copyright 2013 Michael Yang. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package id3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2013-12-30 22:01:40 +00:00
|
|
|
// Tagger represents the metadata of a tag
|
|
|
|
type Tagger interface {
|
|
|
|
Title() string
|
|
|
|
Artist() string
|
|
|
|
Album() string
|
|
|
|
Year() string
|
|
|
|
Genre() string
|
2013-12-31 18:48:00 +00:00
|
|
|
Comments() []string
|
2013-12-30 22:01:40 +00:00
|
|
|
SetTitle(string)
|
|
|
|
SetArtist(string)
|
|
|
|
SetAlbum(string)
|
|
|
|
SetYear(string)
|
|
|
|
SetGenre(string)
|
2014-01-02 00:13:25 +00:00
|
|
|
AllFrames() []Framer
|
2013-12-30 22:01:40 +00:00
|
|
|
Frames(string) []Framer
|
2014-01-02 00:13:25 +00:00
|
|
|
Frame(string) Framer
|
2013-12-30 22:01:40 +00:00
|
|
|
DeleteFrames(string) []Framer
|
|
|
|
AddFrame(Framer)
|
|
|
|
Bytes() []byte
|
|
|
|
Version() string
|
|
|
|
}
|
|
|
|
|
2013-12-30 09:11:19 +00:00
|
|
|
// File represents the tagged file
|
2013-12-21 22:31:03 +00:00
|
|
|
type File struct {
|
2013-12-30 22:01:40 +00:00
|
|
|
Tagger
|
2013-12-30 21:39:42 +00:00
|
|
|
name string
|
|
|
|
data []byte
|
2013-12-22 06:37:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 09:11:19 +00:00
|
|
|
// Opens a new tagged file
|
2013-12-21 22:31:03 +00:00
|
|
|
func Open(name string) (*File, error) {
|
|
|
|
fi, err := os.Open(name)
|
|
|
|
defer fi.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rd := bufio.NewReader(fi)
|
|
|
|
tag := NewTag(rd)
|
|
|
|
data, err := ioutil.ReadAll(rd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-30 09:00:14 +00:00
|
|
|
return &File{
|
|
|
|
tag,
|
|
|
|
name,
|
|
|
|
data,
|
|
|
|
}, nil
|
2013-12-21 22:31:03 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 09:11:19 +00:00
|
|
|
// Saves any edits to the tagged file
|
2013-12-21 22:31:03 +00:00
|
|
|
func (f *File) Close() {
|
|
|
|
fi, err := os.Create(f.name)
|
|
|
|
defer fi.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wr := bufio.NewWriter(fi)
|
2013-12-30 22:01:40 +00:00
|
|
|
wr.Write(f.Tagger.Bytes())
|
2013-12-21 22:31:03 +00:00
|
|
|
wr.Write(f.data)
|
|
|
|
}
|