From 6df4e78d9640ddebaed18977a6c2cfcc8bc03281 Mon Sep 17 00:00:00 2001 From: David Howden Date: Sun, 7 Jun 2015 12:58:58 +1000 Subject: [PATCH] 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. --- flac.go | 5 ----- id3v2.go | 6 ------ mp4.go | 6 +----- ogg.go | 5 ----- sum.go | 32 +++++++------------------------- tag.go | 7 +++++++ 6 files changed, 15 insertions(+), 46 deletions(-) diff --git a/flac.go b/flac.go index b0c0435..6b3da10 100644 --- a/flac.go +++ b/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 diff --git a/id3v2.go b/id3v2.go index d723a21..c3052ec 100644 --- a/id3v2.go +++ b/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 diff --git a/mp4.go b/mp4.go index 3458d27..84f241d 100644 --- a/mp4.go +++ b/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 } diff --git a/ogg.go b/ogg.go index 82a19ec..b10535b 100644 --- a/ogg.go +++ b/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 diff --git a/sum.go b/sum.go index 421c4ef..789be46 100644 --- a/sum.go +++ b/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) diff --git a/tag.go b/tag.go index 392916b..b52610e 100644 --- a/tag.go +++ b/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)