|
|
@ -5,6 +5,7 @@ import ( |
|
|
|
"encoding/binary" |
|
|
|
"encoding/binary" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
|
|
|
|
"os" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -18,7 +19,7 @@ type ID3v11Tags struct { |
|
|
|
Genre string |
|
|
|
Genre string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Retrieves ID3v1.1 field values of provided io.ReadSeeker (usually a file)
|
|
|
|
// Retrieves ID3v1.1 field values of provided io.ReadSeeker
|
|
|
|
func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) { |
|
|
|
func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) { |
|
|
|
// set reader to the last 128 bytes
|
|
|
|
// set reader to the last 128 bytes
|
|
|
|
_, err := rs.Seek(-int64(ID3V1SIZE), io.SeekEnd) |
|
|
|
_, err := rs.Seek(-int64(ID3V1SIZE), io.SeekEnd) |
|
|
@ -106,7 +107,7 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Writes given ID3v1.1 tags to dst
|
|
|
|
// Writes given ID3v1.1 tags to dst
|
|
|
|
func WriteID3v11Tags(dst io.WriteSeeker, tags ID3v11Tags) error { |
|
|
|
func WriteID3v11Tags(dst io.WriteSeeker, tags *ID3v11Tags) error { |
|
|
|
dst.Seek(0, io.SeekEnd) |
|
|
|
dst.Seek(0, io.SeekEnd) |
|
|
|
|
|
|
|
|
|
|
|
// TAG
|
|
|
|
// TAG
|
|
|
@ -175,3 +176,45 @@ func WriteID3v11Tags(dst io.WriteSeeker, tags ID3v11Tags) error { |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Checks for existing ID3v1.1 tag in file, if present - removes it and replaces with provided tags
|
|
|
|
|
|
|
|
func WriteID3v11ToFile(f *os.File, tags *ID3v11Tags) error { |
|
|
|
|
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check for existing ID3v1.1 tag
|
|
|
|
|
|
|
|
f.Seek(-int64(ID3V1SIZE), io.SeekEnd) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tag, err := read(f, 3) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(tag, []byte("TAG")) { |
|
|
|
|
|
|
|
// no existing tag, just write given tags
|
|
|
|
|
|
|
|
err = WriteID3v11Tags(f, tags) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// does contain ID3v1.1 tag. Removing it
|
|
|
|
|
|
|
|
fStats, err := f.Stat() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = f.Truncate(fStats.Size() - int64(ID3V1SIZE)) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// writing new tags
|
|
|
|
|
|
|
|
err = WriteID3v11Tags(f, tags) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|