From 4fd3a51bb9b9e16b8db9045ab5d81af3d5c0e59d Mon Sep 17 00:00:00 2001 From: ihateradiohead Date: Sat, 28 Dec 2024 02:56:47 +0300 Subject: [PATCH] seeker instead of reader to preserve the original state of reader after filetype checking --- match.go | 4 +++- match_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/match.go b/match.go index 82cf804..f22e46e 100644 --- a/match.go +++ b/match.go @@ -52,7 +52,7 @@ 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) @@ -60,6 +60,8 @@ func MatchReader(reader io.Reader) (types.Type, error) { return types.Unknown, err } + reader.Seek(0, 0) + return Match(buffer) } diff --git a/match_test.go b/match_test.go index 946bb25..09b31e1 100644 --- a/match_test.go +++ b/match_test.go @@ -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 {