This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
static.go
185 lines (163 loc) · 3.58 KB
/
static.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
package xweb
import (
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"github.com/howeyc/fsnotify"
)
type StaticVerMgr struct {
Caches map[string]string
mutex *sync.Mutex
Path string
Ignores map[string]bool
app *App
}
func (self *StaticVerMgr) Moniter(staticPath string) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
done := make(chan bool)
go func() {
for {
select {
case ev := <-watcher.Event:
if ev == nil {
break
}
if _, ok := self.Ignores[filepath.Base(ev.Name)]; ok {
break
}
d, err := os.Stat(ev.Name)
if err != nil {
break
}
if ev.IsCreate() {
if d.IsDir() {
watcher.Watch(ev.Name)
} else {
url := ev.Name[len(self.Path)+1:]
self.CacheItem(url)
}
} else if ev.IsDelete() {
if d.IsDir() {
watcher.RemoveWatch(ev.Name)
} else {
pa := ev.Name[len(self.Path)+1:]
self.CacheDelete(pa)
}
} else if ev.IsModify() {
if d.IsDir() {
} else {
url := ev.Name[len(staticPath)+1:]
self.CacheItem(url)
}
} else if ev.IsRename() {
if d.IsDir() {
watcher.RemoveWatch(ev.Name)
} else {
url := ev.Name[len(staticPath)+1:]
self.CacheDelete(url)
}
}
case err := <-watcher.Error:
self.app.Errorf("error: %v", err)
}
}
}()
err = filepath.Walk(staticPath, func(f string, info os.FileInfo, err error) error {
if info.IsDir() {
return watcher.Watch(f)
}
return nil
})
if err != nil {
fmt.Println(err)
return err
}
<-done
watcher.Close()
return nil
}
func (self *StaticVerMgr) Init(app *App, staticPath string) error {
self.Path = staticPath
self.Caches = make(map[string]string)
self.mutex = &sync.Mutex{}
self.Ignores = map[string]bool{".DS_Store": true}
self.app = app
if dirExists(staticPath) {
self.CacheAll(staticPath)
go self.Moniter(staticPath)
}
return nil
}
func (self *StaticVerMgr) getFileVer(url string) string {
//content, err := ioutil.ReadFile(path.Join(self.Path, url))
fPath := filepath.Join(self.Path, url)
self.app.Debug("loaded static ", fPath)
f, err := os.Open(fPath)
if err != nil {
return ""
}
defer f.Close()
fInfo, err := f.Stat()
if err != nil {
return ""
}
content := make([]byte, int(fInfo.Size()))
_, err = f.Read(content)
if err == nil {
h := md5.New()
io.WriteString(h, string(content))
return fmt.Sprintf("%x", h.Sum(nil))[0:4]
}
return ""
}
func (self *StaticVerMgr) CacheAll(staticPath string) error {
self.mutex.Lock()
defer self.mutex.Unlock()
//fmt.Print("Getting static file version number, please wait... ")
err := filepath.Walk(staticPath, func(f string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
rp := f[len(staticPath)+1:]
if _, ok := self.Ignores[filepath.Base(rp)]; !ok {
self.Caches[rp] = self.getFileVer(rp)
}
return nil
})
//fmt.Println("Complete.")
return err
}
func (self *StaticVerMgr) GetVersion(url string) string {
self.mutex.Lock()
defer self.mutex.Unlock()
if ver, ok := self.Caches[url]; ok {
return ver
}
ver := self.getFileVer(url)
if ver != "" {
self.Caches[url] = ver
}
return ver
}
func (self *StaticVerMgr) CacheDelete(url string) {
self.mutex.Lock()
defer self.mutex.Unlock()
delete(self.Caches, url)
self.app.Infof("static file %s is deleted.\n", url)
}
func (self *StaticVerMgr) CacheItem(url string) {
fmt.Println(url)
ver := self.getFileVer(url)
if ver != "" {
self.mutex.Lock()
defer self.mutex.Unlock()
self.Caches[url] = ver
self.app.Infof("static file %s is created.", url)
}
}