This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompressor.go
165 lines (128 loc) · 3.05 KB
/
compressor.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package parcello
import (
"archive/zip"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
)
var _ Compressor = &ZipCompressor{}
// ErrSkipResource skips a particular file from processing
var ErrSkipResource = fmt.Errorf("Skip Resource Error")
// CompressorConfig controls how the code generation happens
type CompressorConfig struct {
// Logger prints each step of compression
Logger io.Writer
// Filename is the name of the compressed bundle
Filename string
// IgnorePatterns provides a list of all files that has to be ignored
IgnorePatterns []string
// Recurive enables embedding the resources recursively
Recurive bool
}
// ZipCompressor compresses content as GZip tarball
type ZipCompressor struct {
// Config controls how the compression is made
Config *CompressorConfig
}
// Compress compresses given source in tar.gz
func (e *ZipCompressor) Compress(ctx *CompressorContext) (*Bundle, error) {
buffer := &bytes.Buffer{}
count, err := e.write(buffer, ctx)
if err != nil {
return nil, err
}
if count == 0 {
return nil, nil
}
return &Bundle{
Name: e.Config.Filename,
Body: buffer.Bytes(),
Count: count,
}, nil
}
func (e *ZipCompressor) write(w io.Writer, ctx *CompressorContext) (int, error) {
compressor := zip.NewWriter(w)
if ctx.Offset > 0 {
compressor.SetOffset(ctx.Offset)
}
count := 0
err := ctx.FileSystem.Walk("/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
err = e.filter(path, info)
switch err {
case ErrSkipResource:
return nil
default:
if err != nil {
return err
}
}
if err = e.walk(compressor, ctx.FileSystem, path, info); err != nil {
return err
}
count = count + 1
return nil
})
if err != nil {
return count, err
}
_ = compressor.Flush()
if ioErr := compressor.Close(); err == nil {
err = ioErr
}
return count, err
}
func (e *ZipCompressor) walk(compressor *zip.Writer, fileSystem FileSystem, path string, info os.FileInfo) error {
fmt.Fprintln(e.Config.Logger, fmt.Sprintf("Compressing '%s'", path))
header, _ := zip.FileInfoHeader(info)
header.Method = zip.Deflate
header.Name = path
writer, _ := compressor.CreateHeader(header)
resource, err := fileSystem.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return err
}
defer func() {
if ioErr := resource.Close(); err == nil {
err = ioErr
}
}()
_, err = io.Copy(writer, resource)
return err
}
func (e *ZipCompressor) filter(path string, info os.FileInfo) error {
if info == nil {
return ErrSkipResource
}
if err := e.ignore(path, info); err != nil {
return err
}
if !info.IsDir() {
return nil
}
if !e.Config.Recurive && path != "." {
return filepath.SkipDir
}
return ErrSkipResource
}
func (e *ZipCompressor) ignore(path string, info os.FileInfo) error {
ignore := append(e.Config.IgnorePatterns, "*.go")
for _, pattern := range ignore {
matched, err := match(pattern, path, info.Name())
if err != nil {
return err
}
if !matched {
continue
}
if info.IsDir() {
return filepath.SkipDir
}
return ErrSkipResource
}
return nil
}