From e5ae343a4bc909e270f3d54b4a483c107b5e3967 Mon Sep 17 00:00:00 2001 From: Josh Liburdi Date: Wed, 19 Apr 2023 19:50:47 +0000 Subject: [PATCH 1/2] feat: zstd, snappy readers --- internal/bufio/bufio.go | 13 +++++++++++++ internal/media/media.go | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/internal/bufio/bufio.go b/internal/bufio/bufio.go index a4b10ae2..7aa604ba 100644 --- a/internal/bufio/bufio.go +++ b/internal/bufio/bufio.go @@ -12,6 +12,8 @@ import ( "github.com/brexhq/substation/internal/errors" "github.com/brexhq/substation/internal/media" + "github.com/klauspost/compress/snappy" + "github.com/klauspost/compress/zstd" ) // errInvalidMethod is returned when an invalid scanner method is provided. @@ -101,6 +103,17 @@ func (s *scanner) ReadFile(file *os.File) error { reader = gzipReader s.openHandles = append(s.openHandles, reader) + case "application/zstd": + zstdReader, err := zstd.NewReader(file) + if err != nil { + return fmt.Errorf("readopenfile: %v", err) + } + + reader = io.NopCloser(zstdReader) + s.openHandles = append(s.openHandles, reader) + case "application/x-snappy-framed": + snappyReader := snappy.NewReader(file) + reader = io.NopCloser(snappyReader) default: // file was previously added to openHandles reader = file diff --git a/internal/media/media.go b/internal/media/media.go index b6e66f59..bbecdc89 100644 --- a/internal/media/media.go +++ b/internal/media/media.go @@ -17,6 +17,12 @@ func Bytes(b []byte) string { // http.DetectContentType occasionally (rarely) generates false positive matches for application/vnd.ms-fontobject when the bytes are application/x-gzip case bytes.HasPrefix(b, []byte("\x1f\x8b\x08")): return "application/x-gzip" + // http.DetectContentType cannot detect zstd + case bytes.HasPrefix(b, []byte("\x28\xb5\x2f\xfd")): + return "application/x-zstd" + // http.DetectContentType cannot detect snappy + case bytes.HasPrefix(b, []byte("\xff\x06\x00\x00\x73\x4e\x61\x50\x70\x59")): + return "application/x-snappy-framed" default: return http.DetectContentType(b) } From 0895f434859145c3bb00795411c06e2f017f0b20 Mon Sep 17 00:00:00 2001 From: Josh Liburdi Date: Sat, 22 Apr 2023 07:56:46 -0700 Subject: [PATCH 2/2] test: zstd, snappy --- internal/media/media_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/media/media_test.go b/internal/media/media_test.go index 3e2fd903..1ea43a6b 100644 --- a/internal/media/media_test.go +++ b/internal/media/media_test.go @@ -20,6 +20,16 @@ var mediaTests = []struct { []byte("\x1f\x8b\x08"), "application/x-gzip", }, + { + "zstd", + []byte("\x28\xb5\x2f\xfd"), + "application/x-zstd", + }, + { + "snappy", + []byte("\xff\x06\x00\x00\x73\x4e\x61\x50\x70\x59"), + "application/x-snappy-framed", + }, } func TestBytes(t *testing.T) {