-
Notifications
You must be signed in to change notification settings - Fork 35
/
expander.go
83 lines (67 loc) · 1.68 KB
/
expander.go
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
package asset
import (
"errors"
"fmt"
"io"
archiver "github.com/mholt/archiver/v3"
filetype "gopkg.in/h2non/filetype.v1"
filetype_types "gopkg.in/h2non/filetype.v1/types"
)
const (
// Size of file header for sniffing type
headerSize = 262
)
// A archiveExpander detects the archive type and expands it to the local
// filesystem.
//
// Supported archive types:
// - tar
// - tar-gzip
type archiveExpander struct{}
type namer interface {
Name() string
}
// Expand an archive to a target directory.
func (a *archiveExpander) Expand(archive io.ReadSeeker, targetDirectory string) error {
// detect the type of archive the asset is
ft, err := sniffType(archive)
if err != nil {
return err
}
var ar archiver.Unarchiver
// If the file is not an archive, exit with an error.
switch ft.MIME.Value {
case "application/x-tar":
ar = archiver.NewTar()
case "application/gzip":
ar = archiver.NewTarGz()
default:
return fmt.Errorf(
"given file of format '%s' does not appear valid",
ft.MIME.Value,
)
}
namer, ok := archive.(namer)
if !ok {
return errors.New("couldn't get path to archive")
}
// Extract the archive to the desired path
if err := ar.Unarchive(namer.Name(), targetDirectory); err != nil {
return fmt.Errorf("error extracting asset: %s", err)
}
return nil
}
func sniffType(f io.ReadSeeker) (filetype_types.Type, error) {
header := make([]byte, headerSize)
if _, err := f.Read(header); err != nil {
return filetype_types.Type{}, fmt.Errorf("unable to read asset header: %s", err)
}
ft, err := filetype.Match(header)
if err != nil {
return ft, err
}
if _, err := f.Seek(0, 0); err != nil {
return filetype_types.Type{}, err
}
return ft, nil
}