From b8da487f774aa2399b79bbde5f2e6e13be4fce1c Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 17 May 2024 10:51:42 +0200 Subject: feat: decode brotli-compressed files --- internal/importer/utils.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'internal/importer/utils.go') diff --git a/internal/importer/utils.go b/internal/importer/utils.go index 3eb034f..1c0d3af 100644 --- a/internal/importer/utils.go +++ b/internal/importer/utils.go @@ -3,11 +3,14 @@ package importer import ( "bytes" "fmt" + "io" "net/url" "os" + "path" "searchix/internal/config" "searchix/internal/nix" + "github.com/andybalholm/brotli" "github.com/bcicen/jstream" "github.com/pkg/errors" ) @@ -80,3 +83,41 @@ func setRepoRevision(filename string, source *config.Source) error { return nil } + +type brotliReadCloser struct { + src io.ReadCloser + *brotli.Reader +} + +func newBrotliReader(src io.ReadCloser) *brotliReadCloser { + return &brotliReadCloser{ + src: src, + Reader: brotli.NewReader(src), + } +} + +func (r *brotliReadCloser) Close() error { + return errors.Wrap(r.src.Close(), "failed to call close on underlying reader") +} + +func openFileDecoded(filename string) (io.ReadCloser, error) { + var reader io.ReadCloser + var err error + ext := path.Ext(filename) + reader, err = os.Open(filename) + if err != nil { + return nil, errors.WithMessagef(err, "failed to open file %s", filename) + } + switch ext { + case ".json": + // nothing to do + case ".br": + reader = newBrotliReader(reader) + default: + reader.Close() + + return nil, errors.Errorf("invalid file extension %s", ext) + } + + return reader, nil +} -- cgit 1.4.1