/
dir.go
173 lines (159 loc) · 3.5 KB
/
dir.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
package controller
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/rjeczalik/notify"
projectModel "github.com/sysu-go-online/project-service/model"
"github.com/sysu-go-online/public-service/tools"
userModel "github.com/sysu-go-online/user-service/model"
)
// DirRequest contains message send from user
type DirRequest struct {
JWT string `json:"jwt"`
Project string `json:"project"`
}
// DirResponse stores response to the user
type DirResponse struct {
OK bool `json:"ok"`
Path string `json:"path"`
State int `json:"state"`
Type string `json:"type"`
}
// MonitorDirHandler monitor user dir status
func MonitorDirHandler(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if err != nil {
fmt.Println(err)
return
}
// wait for the first message
msg := DirRequest{}
err = ws.ReadJSON(&msg)
if err != nil {
fmt.Println(err)
ws.Close()
return
}
// listening for close event
stop := make(chan bool, 0)
// keep connection
go func() {
for {
timer := time.NewTimer(time.Second * 2)
<-timer.C
err := ws.WriteControl(websocket.PingMessage, []byte("ping"), time.Time{})
if err != nil {
stop <- true
timer.Stop()
return
}
}
}()
// parse jwt for auth and get relevant message
// check token
res := DirResponse{}
ok, username := tools.GetUserNameFromToken(msg.JWT, AuthRedisClient)
if !ok {
fmt.Fprintln(os.Stderr, "Can not get user token information")
res.OK = false
ws.WriteJSON(r)
ws.Close()
return
}
// Get project information
session := MysqlEngine.NewSession()
u := userModel.User{Username: username}
ok, err = u.GetWithUsername(session)
if !ok {
fmt.Fprintln(os.Stderr, "Can not get user information")
res.OK = false
ws.WriteJSON(r)
ws.Close()
return
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
res.OK = false
ws.WriteJSON(r)
ws.Close()
return
}
p := projectModel.Project{Name: msg.Project, UserID: u.ID}
has, err := p.GetWithUserIDAndName(session)
if !has {
fmt.Fprintln(os.Stderr, "Can not get project information")
res.OK = false
ws.WriteJSON(r)
ws.Close()
return
}
if err != nil {
fmt.Fprintln(os.Stderr, err)
res.OK = false
ws.WriteJSON(r)
ws.Close()
return
}
// listen to dir events
path := filepath.Join("/home", username, "projects", p.Path, p.Name)
c := make(chan notify.EventInfo, 1)
if err := notify.Watch(path+"/...", c, notify.All); err != nil {
log.Println(err)
return
}
defer notify.Stop(c)
res.OK = true
res.State = -1
err = ws.WriteJSON(res)
if err != nil {
log.Println(err)
return
}
// Block until an event is received.
go func() {
for n := range c {
res.Path = strings.Split(n.Path(), filepath.Join("/home", username, "projects/", p.Path, p.Name))[1][1:]
res.OK = true
fileInfo,err := os.Stat(n.Path());
if err != nil {
log.Println(err)
} else {
if fileInfo.IsDir() {
res.Type = "dir"
} else {
res.Type = "file"
}
}
switch n.Event() {
case notify.Create:
res.State = 0
case notify.Remove:
res.State = 1
case notify.Write:
res.State = 2
case notify.InMovedFrom:
res.State = 3
case notify.InMovedTo:
res.State = 4
default:
res.State = 5
}
err = ws.WriteJSON(res)
if err != nil {
stop <- true
log.Println(err)
return
}
if err := notify.Watch(n.Path(), c, notify.All); err != nil {
log.Println(err)
}
}
}()
<-stop
}