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

feat: Add Zstandard & Snappy Readers #105

Merged
merged 3 commits into from
Apr 27, 2023
Merged
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
13 changes: 13 additions & 0 deletions internal/bufio/bufio.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strconv"

"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.
Expand Down Expand Up @@ -100,6 +102,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
Expand Down
6 changes: 6 additions & 0 deletions internal/media/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions internal/media/media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down