forked from bep/s3deploy
/
files.go
247 lines (194 loc) · 4.34 KB
/
files.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright © 2018 Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package lib
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"sync"
"github.com/dsnet/golib/memfile"
)
var (
_ file = (*osFile)(nil)
_ localFile = (*osFile)(nil)
)
type file interface {
// Key represents the key on the target file store.
Key() string
ETag() string
Size() int64
}
type localFile interface {
file
shouldThisReplace(other file) (bool, string)
// Content returns the content to be stored remotely. If this file
// configured to be gzipped, then that is what you get.
Content() io.ReadSeeker
Headers() map[string]string
}
type osFile struct {
relPath string
// Filled when BucketPath is provided. Will store files in a sub-path
// of the target file store.
targetRoot string
absPath string
size int64
etag string
etagInit sync.Once
contentType string
f *memfile.File
route *route
}
func (f *osFile) Key() string {
if f.targetRoot != "" {
return path.Join(f.targetRoot, f.relPath)
}
return f.relPath
}
func (f *osFile) ETag() string {
f.etagInit.Do(func() {
var err error
f.etag, err = calculateETag(f.Content())
if err != nil {
panic(err)
}
})
return f.etag
}
func (f *osFile) Size() int64 {
return f.size
}
func (f *osFile) Content() io.ReadSeeker {
f.f.Seek(0, 0)
return f.f
}
func (f *osFile) Headers() map[string]string {
headers := map[string]string{
"Content-Type": f.contentType,
}
if f.route != nil {
if f.route.Gzip {
headers["Content-Encoding"] = "gzip"
}
if f.route.Headers != nil {
for k, v := range f.route.Headers {
headers[k] = v
}
}
}
return headers
}
func (f *osFile) initContentType(peek []byte) error {
if f.route != nil {
if contentType, found := f.route.Headers["Content-Type"]; found {
f.contentType = contentType
return nil
}
}
contentType := mime.TypeByExtension(filepath.Ext(f.relPath))
if contentType != "" {
f.contentType = contentType
return nil
}
// Have to look inside the file itself.
if peek != nil {
f.contentType = detectContentTypeFromContent(peek)
} else {
f.contentType = detectContentTypeFromContent(f.f.Bytes())
}
return nil
}
func detectContentTypeFromContent(b []byte) string {
const magicSize = 512 // Size that DetectContentType expects
var peek []byte
if len(b) > magicSize {
peek = b[:magicSize]
} else {
peek = b
}
return http.DetectContentType(peek)
}
func (f *osFile) shouldThisReplace(other file) (bool, string) {
if f.Size() != other.Size() {
return true, "size"
}
if f.ETag() != other.ETag() {
return true, "ETag"
}
return false, ""
}
func newOSFile(routes routes, targetRoot, relPath, absPath string, fi os.FileInfo) (*osFile, error) {
relPath = filepath.ToSlash(relPath)
file, err := os.Open(absPath)
if err != nil {
return nil, fmt.Errorf("failed to open %q: %s", absPath, err)
}
defer file.Close()
var (
mFile *memfile.File
size = fi.Size()
peek []byte
)
route := routes.get(relPath)
if route != nil && route.Gzip {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
io.Copy(gz, file)
gz.Close()
mFile = memfile.New(b.Bytes())
size = int64(b.Len())
peek = make([]byte, 512)
file.Read(peek)
} else {
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
mFile = memfile.New(b)
}
of := &osFile{route: route, f: mFile, targetRoot: targetRoot, absPath: absPath, relPath: relPath, size: size}
if err := of.initContentType(peek); err != nil {
return nil, err
}
return of, nil
}
type routes []*route
func (r routes) get(path string) *route {
for _, route := range r {
if route.routerRE.MatchString(path) {
return route
}
}
// no route found
return nil
}
// read config from .s3deploy.yml if found.
type fileConfig struct {
Routes routes `yaml:"routes"`
}
type route struct {
Route string `yaml:"route"`
Headers map[string]string `yaml:"headers"`
Gzip bool `yaml:"gzip"`
routerRE *regexp.Regexp // compiled version of Route
}
func calculateETag(r io.Reader) (string, error) {
h := md5.New()
_, err := io.Copy(h, r)
if err != nil {
return "", err
}
return "\"" + hex.EncodeToString(h.Sum(nil)) + "\"", nil
}