-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.go
More file actions
269 lines (223 loc) · 6.22 KB
/
Copy pathstorage.go
File metadata and controls
269 lines (223 loc) · 6.22 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Package staticfiles is an asset manager for versioning static files in web applications.
//
// It collects asset files (CSS, JS, images, etc.) from a different locations (including subdirectories),
// appends hash sum of each file to its name and copies files to the target directory
// to be served by http.FileServer.
//
// This approach allows to serve files without having to clear a CDN or browser cache every time
// the files was changed. This also allows to use aggressive caching on CDN and HTTP headers
// to implement so called "cache hierarchy strategy" (https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#invalidating_and_updating_cached_responses).
package staticfiles
import (
"crypto/md5"
"encoding/hex"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
const hashLength int = 12
type StaticFile struct {
Path string // Original file path
RelPath string // Original file path relative to the one of the Storage.inputDirs
StoragePath string // Storage file path
StorageRelPath string // Storage file path relative to the Storage.OutputDir
}
// PostProcessRule describes the type of a post-process rule functions.
type PostProcessRule func(*Storage, *StaticFile) error
type Storage struct {
OutputDir string
outputDirFS http.FileSystem
FilesMap map[string]*StaticFile
postProcessRules []PostProcessRule
inputDirs []string
OutputDirList bool
Enabled bool
Verbose bool // toggles verbose output to the standard logger
ignorePatterns []string
}
// NewStorage returns new Storage initialized with the root directory and
// registered rule to post-process CSS files.
func NewStorage(outputDir string) (*Storage, error) {
outputDir = filepath.ToSlash(filepath.Clean(outputDir)) + "/"
filesMap, err := loadManifest(outputDir)
if (err != nil) && !os.IsNotExist(err) {
return nil, err
}
s := &Storage{
OutputDir: outputDir,
outputDirFS: http.Dir(outputDir),
FilesMap: filesMap,
OutputDirList: true,
Enabled: true,
}
s.RegisterRule(PostProcessCSS)
return s, nil
}
func (s *Storage) AddInputDir(path string) {
s.inputDirs = append(s.inputDirs, filepath.ToSlash(filepath.Clean(path))+"/")
}
func (s *Storage) AddIgnorePattern(pattern string) {
s.ignorePatterns = append(s.ignorePatterns, pattern)
}
func (s *Storage) RegisterRule(rule PostProcessRule) {
s.postProcessRules = append(s.postProcessRules, rule)
}
func (s *Storage) hashFilename(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
hash := md5.New()
if _, err = io.Copy(hash, f); err != nil {
return "", err
}
ext := filepath.Ext(path)
prefix := strings.TrimSuffix(path, ext)
sum := hex.EncodeToString(hash.Sum(nil))[:hashLength]
return prefix + "." + sum + ext, nil
}
func (s *Storage) copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer out.Close()
if _, err = io.Copy(out, in); err != nil {
return err
}
err = out.Sync()
return err
}
func (s *Storage) collectFiles() error {
for _, dir := range s.inputDirs {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
path = filepath.ToSlash(path)
relPath := strings.TrimPrefix(path, dir)
for _, pattern := range s.ignorePatterns {
if ok, err := filepath.Match(pattern, relPath); ok || err != nil {
return nil
}
}
hashedPath, err := s.hashFilename(path)
if err != nil {
return err
}
storageDir := filepath.Join(s.OutputDir, filepath.Dir(relPath))
storagePath := filepath.ToSlash(filepath.Join(storageDir, filepath.Base(hashedPath)))
if _, err := os.Stat(storagePath); os.IsNotExist(err) {
err = os.MkdirAll(storageDir, 0755)
if err != nil {
return err
}
if s.Verbose {
log.Printf("Copying '%s'", relPath)
}
err = s.copyFile(path, storagePath)
if err != nil {
return err
}
}
s.FilesMap[relPath] = &StaticFile{
Path: path,
RelPath: relPath,
StoragePath: storagePath,
StorageRelPath: strings.TrimPrefix(storagePath, s.OutputDir),
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func (s *Storage) postProcessFiles() error {
for _, sf := range s.FilesMap {
for _, rule := range s.postProcessRules {
if s.Verbose {
log.Printf("Processing '%s'", sf.RelPath)
}
err := rule(s, sf)
if err != nil {
return err
}
}
}
return nil
}
// CollectStatic collects files from the Storage.inputDirs (including subdirectories),
// appends hash sum of each file to its name, applies post-processing rules and
// copies files and manifest to the Storage.OutputDir directory.
func (s *Storage) CollectStatic() error {
err := os.MkdirAll(s.OutputDir, 0755)
if err != nil {
return err
}
err = s.collectFiles()
if err != nil {
return err
}
err = s.postProcessFiles()
if err != nil {
return err
}
err = saveManifest(s.OutputDir, s.FilesMap)
if err != nil {
return err
}
return nil
}
// Open implements http.FileSystem interface to be used primarily in http.FileServer
func (s *Storage) Open(path string) (http.File, error) {
var f http.File
var err error
if !s.Enabled {
log.Print("Static storage is disabled. Don't forget to enable it in production.")
for _, dir := range s.inputDirs {
f, err = http.Dir(dir).Open(path)
if (err == nil) || !os.IsNotExist(err) {
break
}
}
} else {
f, err = s.outputDirFS.Open(path)
}
if err != nil {
return nil, err
}
if !s.OutputDirList {
stat, err := f.Stat()
if err != nil {
return nil, err
}
if stat.IsDir() {
return nil, os.ErrNotExist
}
}
return f, nil
}
// Resolve returns relative storage file path from the relative original file path.
// When storage is disabled it returns unchanged value passed in the function.
func (s *Storage) Resolve(relPath string) string {
if !s.Enabled {
return relPath
} else if sf, ok := s.FilesMap[relPath]; ok {
return sf.StorageRelPath
}
return ""
}