id3-go/id3.go

82 wiersze
1.4 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
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
2014-01-03 21:34:12 +00:00
Padding() uint
Size() int
2013-12-30 22:01:40 +00:00
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
originalSize int
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)
2014-01-04 18:48:26 +00:00
tag := ParseTag(rd)
2013-12-21 22:31:03 +00:00
data, err := ioutil.ReadAll(rd)
if err != nil {
return nil, err
}
2013-12-30 09:00:14 +00:00
return &File{
tag,
tag.Size(),
2013-12-30 09:00:14 +00:00
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.OpenFile(f.name, os.O_RDWR, 0666)
2013-12-21 22:31:03 +00:00
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())
if f.Size() > f.originalSize {
wr.Write(f.data)
}
2013-12-21 22:31:03 +00:00
}