summaryrefslogtreecommitdiff
path: root/src/post/asset/loader_archive.go
blob: 1c8697feb0401bcc5f27c2b771869e324b4f067b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package asset

import (
	"bytes"
	"compress/gzip"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"strings"

	"github.com/omeid/go-tarfs"
)

type archiveLoader struct {
	loader Loader
}

// NewArchiveLoader wraps an existing Loader in order to support extracting
// files from within an archive file asset.
//
// For example, loading the path `foo.tgz/foo/bar.jpg` will call
// `Load("foo.tgz")`, load that archive into memory, and serve the file
// `./foo/bar.jpg` from within the archive.
func NewArchiveLoader(loader Loader) Loader {
	return &archiveLoader{loader: loader}
}

func (l *archiveLoader) Load(path string, into io.Writer, opts LoadOpts) error {

	id, subPath, ok := strings.Cut(strings.TrimPrefix(path, "/"), "/")

	if !ok {
		return l.loader.Load(path, into, opts)
	}

	var isGzipped bool

	switch {

	case strings.HasSuffix(id, ".tar.gz"),
		strings.HasSuffix(id, ".tgz"):
		isGzipped = true

	case strings.HasSuffix(id, ".tar"):
		// ok

	default:
		// unsupported
		return l.loader.Load(path, into, opts)
	}

	buf := new(bytes.Buffer)

	if err := l.loader.Load(id, buf, opts); err != nil {
		return fmt.Errorf("loading archive into buffer: %w", err)
	}

	var (
		from io.Reader = buf
		err  error
	)

	if isGzipped {

		if from, err = gzip.NewReader(from); err != nil {
			return fmt.Errorf("decompressing archive asset with id %q: %w", id, err)
		}
	}

	tarFS, err := tarfs.New(from)

	if err != nil {
		return fmt.Errorf("reading archive asset with id %q as fs: %w", id, err)
	}

	f, err := tarFS.Open(subPath)

	if errors.Is(err, fs.ErrExist) {
		return ErrNotFound

	} else if err != nil {
		return fmt.Errorf(
			"opening path %q from archive asset with id %q as fs: %w",
			subPath, id, err,
		)
	}

	defer f.Close()

	if _, err = io.Copy(into, f); err != nil {
		return fmt.Errorf(
			"reading %q from archive asset with id %q as fs: %w",
			subPath, id, err,
		)
	}

	return nil
}