id3-go/id3.go

148 wiersze
2.7 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 (
2014-01-08 05:18:21 +00:00
"errors"
v1 "github.com/mikkyang/id3-go/v1"
2014-01-08 04:43:27 +00:00
v2 "github.com/mikkyang/id3-go/v2"
"io"
2013-12-21 22:31:03 +00:00
"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-08 04:43:27 +00:00
AllFrames() []v2.Framer
Frames(string) []v2.Framer
Frame(string) v2.Framer
DeleteFrames(string) []v2.Framer
AddFrame(v2.Framer)
2013-12-30 22:01:40 +00:00
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
file *os.File
2013-12-22 06:37:15 +00:00
}
2014-01-08 05:18:21 +00:00
const (
fileEndFlag = 2
)
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) {
2014-01-09 05:45:33 +00:00
fi, err := os.OpenFile(name, os.O_RDWR, 0666)
2013-12-21 22:31:03 +00:00
if err != nil {
return nil, err
}
2014-01-08 05:18:21 +00:00
file := &File{file: fi}
2013-12-21 22:31:03 +00:00
2014-01-08 05:18:21 +00:00
if v2Tag := v2.ParseTag(fi); v2Tag != nil {
file.Tagger = v2Tag
file.originalSize = v2Tag.Size()
} else if v1Tag := v1.ParseTag(fi); v1Tag != nil {
file.Tagger = v1Tag
} else {
return nil, errors.New("Open: unknown tag format")
}
return file, nil
2013-12-21 22:31:03 +00:00
}
2013-12-30 09:11:19 +00:00
// Saves any edits to the tagged file
func (f *File) Close() error {
defer f.file.Close()
2014-01-08 05:18:21 +00:00
switch f.Tagger.(type) {
case (*v1.Tag):
f.file.Seek(-v1.TagSize, fileEndFlag)
f.file.Write(f.Tagger.Bytes())
case (*v2.Tag):
if f.Size() > f.originalSize {
stat, err := f.file.Stat()
if err != nil {
return err
}
start := f.originalSize + v2.HeaderSize
end := stat.Size()
offset := f.Tagger.Size() - f.originalSize
err = f.shiftBytesBack(int64(start), end, int64(offset))
if err != nil {
return err
}
}
2014-01-08 05:18:21 +00:00
if _, err := f.file.WriteAt(f.Tagger.Bytes(), 0); err != nil {
return err
}
2014-01-08 05:18:21 +00:00
default:
return errors.New("Close: unknown tag version")
}
return nil
}
func (f *File) shiftBytesBack(start, end, offset int64) error {
wrBuf := make([]byte, offset)
rdBuf := make([]byte, offset)
wrOffset := offset
rdOffset := start
rn, err := f.file.ReadAt(wrBuf, rdOffset)
if err != nil && err != io.EOF {
2013-12-21 22:31:03 +00:00
panic(err)
}
rdOffset += int64(rn)
2013-12-21 22:31:03 +00:00
for {
if rdOffset >= end {
break
}
n, err := f.file.ReadAt(rdBuf, rdOffset)
if err != nil && err != io.EOF {
return err
}
if rdOffset+int64(n) > end {
n = int(end - rdOffset)
}
if _, err := f.file.WriteAt(wrBuf[:rn], wrOffset); err != nil {
return err
}
rdOffset += int64(n)
wrOffset += int64(rn)
copy(wrBuf, rdBuf)
rn = n
}
if _, err := f.file.WriteAt(wrBuf[:rn], wrOffset); err != nil {
return err
}
return nil
2013-12-21 22:31:03 +00:00
}