Remove non-relative Seek calls from all functions.
Previously we would seek to the beginning of the io.ReadSeeker. It is much more flexible to assume that the caller passes the ReadSeeker in an appropriate state.
This commit is contained in:
parent
7f8f21cc46
commit
6df4e78d96
5
flac.go
5
flac.go
@ -27,11 +27,6 @@ const (
|
||||
// ReadFLACTags reads FLAC metadata from the io.ReadSeeker, returning the resulting
|
||||
// metadata in a Metadata implementation, or non-nil error if there was a problem.
|
||||
func ReadFLACTags(r io.ReadSeeker) (Metadata, error) {
|
||||
_, err := r.Seek(0, os.SEEK_SET)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
flac, err := readString(r, 4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
6
id3v2.go
6
id3v2.go
@ -7,7 +7,6 @@ package tag
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -302,11 +301,6 @@ func (r *unsynchroniser) Read(p []byte) (int, error) {
|
||||
// ReadID3v2Tags parses ID3v2.{2,3,4} tags from the io.ReadSeeker into a Metadata, returning
|
||||
// non-nil error on failure.
|
||||
func ReadID3v2Tags(r io.ReadSeeker) (Metadata, error) {
|
||||
_, err := r.Seek(0, os.SEEK_SET)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
h, err := readID3v2Header(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
6
mp4.go
6
mp4.go
@ -62,12 +62,8 @@ type metadataMP4 map[string]interface{}
|
||||
// ReadAtoms reads MP4 metadata atoms from the io.ReadSeeker into a Metadata, returning
|
||||
// non-nil error if there was a problem.
|
||||
func ReadAtoms(r io.ReadSeeker) (Metadata, error) {
|
||||
_, err := r.Seek(0, os.SEEK_SET)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := make(metadataMP4)
|
||||
err = m.readAtoms(r)
|
||||
err := m.readAtoms(r)
|
||||
return m, err
|
||||
}
|
||||
|
||||
|
5
ogg.go
5
ogg.go
@ -20,11 +20,6 @@ const (
|
||||
// See http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html
|
||||
// and http://www.xiph.org/ogg/doc/framing.html for details.
|
||||
func ReadOGGTags(r io.ReadSeeker) (Metadata, error) {
|
||||
_, err := r.Seek(0, os.SEEK_SET)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
oggs, err := readString(r, 4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
32
sum.go
32
sum.go
@ -17,6 +17,11 @@ func Sum(r io.ReadSeeker) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = r.Seek(-11, os.SEEK_CUR)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not seek back to original position: %v", err)
|
||||
}
|
||||
|
||||
if string(b[4:11]) == "ftypM4A" {
|
||||
return SumAtoms(r)
|
||||
}
|
||||
@ -35,13 +40,8 @@ func Sum(r io.ReadSeeker) (string, error) {
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// SumAll returns a checksum of the entire content.
|
||||
// SumAll returns a checksum of the content from the reader (until EOF).
|
||||
func SumAll(r io.ReadSeeker) (string, error) {
|
||||
_, err := r.Seek(0, os.SEEK_SET)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error seeking to 0: %v", err)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
@ -52,14 +52,6 @@ func SumAll(r io.ReadSeeker) (string, error) {
|
||||
// SumAtoms constructs a checksum of MP4 audio file data provided by the io.ReadSeeker which is
|
||||
// metadata invariant.
|
||||
func SumAtoms(r io.ReadSeeker) (string, error) {
|
||||
_, err := r.Seek(0, os.SEEK_SET)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error seeking to 0: %v", err)
|
||||
}
|
||||
return sumAtoms(r)
|
||||
}
|
||||
|
||||
func sumAtoms(r io.ReadSeeker) (string, error) {
|
||||
for {
|
||||
var size uint32
|
||||
err := binary.Read(r, binary.BigEndian, &size)
|
||||
@ -85,7 +77,7 @@ func sumAtoms(r io.ReadSeeker) (string, error) {
|
||||
fallthrough
|
||||
|
||||
case "moov", "udta", "ilst":
|
||||
return sumAtoms(r)
|
||||
return SumAtoms(r)
|
||||
|
||||
case "free":
|
||||
_, err = r.Seek(int64(size-8), os.SEEK_CUR)
|
||||
@ -112,11 +104,6 @@ func sumAtoms(r io.ReadSeeker) (string, error) {
|
||||
// SumID3v1 constructs a checksum of MP3 audio file data (assumed to have ID3v1 tags) provided
|
||||
// by the io.ReadSeeker which is metadata invariant.
|
||||
func SumID3v1(r io.ReadSeeker) (string, error) {
|
||||
_, err := r.Seek(0, os.SEEK_SET)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error seeking to 0: %v", err)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -131,11 +118,6 @@ func SumID3v1(r io.ReadSeeker) (string, error) {
|
||||
// SumID3v2 constructs a checksum of MP3 audio file data (assumed to have ID3v2 tags) provided by the
|
||||
// io.ReadSeeker which is metadata invariant.
|
||||
func SumID3v2(r io.ReadSeeker) (string, error) {
|
||||
_, err := r.Seek(0, os.SEEK_SET)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error seeking to 0: %v", err)
|
||||
}
|
||||
|
||||
h, err := readID3v2Header(r)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error reading ID3v2 header: %v", err)
|
||||
|
7
tag.go
7
tag.go
@ -7,7 +7,9 @@ package tag
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// ErrNoTagsFound is the error returned by ReadFrom when the metadata format
|
||||
@ -25,6 +27,11 @@ func ReadFrom(r io.ReadSeeker) (Metadata, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = r.Seek(-11, os.SEEK_CUR)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not seek back to original position: %v", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case string(b[0:4]) == "fLaC":
|
||||
return ReadFLACTags(r)
|
||||
|
Loading…
Reference in New Issue
Block a user