Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seeker instead of reader to preserve the original state of reader after filetype checking #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion match.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ func MatchFile(filepath string) (types.Type, error) {
}

// MatchReader is convenient wrapper to Match() any Reader
func MatchReader(reader io.Reader) (types.Type, error) {
func MatchReader(reader io.ReadSeeker) (types.Type, error) {
buffer := make([]byte, 8192) // 8K makes msooxml tests happy and allows for expanded custom file checks

_, err := reader.Read(buffer)
if err != nil && err != io.EOF {
return types.Unknown, err
}

reader.Seek(0, 0)

return Match(buffer)
}

Expand Down
8 changes: 4 additions & 4 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func TestMatchFile(t *testing.T) {

func TestMatchReader(t *testing.T) {
cases := []struct {
buf io.Reader
buf io.ReadSeeker
ext string
}{
{bytes.NewBuffer([]byte{0xFF, 0xD8, 0xFF}), "jpg"},
{bytes.NewBuffer([]byte{0xFF, 0xD8, 0x00}), "unknown"},
{bytes.NewBuffer([]byte{0x89, 0x50, 0x4E, 0x47}), "png"},
{bytes.NewReader([]byte{0xFF, 0xD8, 0xFF}), "jpg"},
{bytes.NewReader([]byte{0xFF, 0xD8, 0x00}), "unknown"},
{bytes.NewReader([]byte{0x89, 0x50, 0x4E, 0x47}), "png"},
}

for _, test := range cases {
Expand Down