id3-go/id3.go

73 wiersze
1.2 KiB
Go
Czysty Zwykły widok Historia

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
SetTitle(string)
SetArtist(string)
SetAlbum(string)
SetYear(string)
SetGenre(string)
Frame(string) Framer
Frames(string) []Framer
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)
}