about summary refs log tree commit diff stats
path: root/internal/importer/utils.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-17 10:51:42 +0200
committerAlan Pearce2024-05-17 12:21:19 +0200
commitb8da487f774aa2399b79bbde5f2e6e13be4fce1c (patch)
treeff3379ea1ac598a025f7e70ccc6c7fc3d0b44434 /internal/importer/utils.go
parent27b07a80f1872205dfeb949e9b90cf85f6e43744 (diff)
downloadsearchix-b8da487f774aa2399b79bbde5f2e6e13be4fce1c.tar.lz
searchix-b8da487f774aa2399b79bbde5f2e6e13be4fce1c.tar.zst
searchix-b8da487f774aa2399b79bbde5f2e6e13be4fce1c.zip
feat: decode brotli-compressed files
Diffstat (limited to 'internal/importer/utils.go')
-rw-r--r--internal/importer/utils.go41
1 files changed, 41 insertions, 0 deletions
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
+}