-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
198 lines (170 loc) · 4.05 KB
/
handlers.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
package main
import (
"crypto/subtle"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"html/template"
"log"
"net/http"
"os"
"path"
)
type FilePageData struct {
CurrentPath string
Files []*Path
IsNotRoot bool
ParentPath string
ParentPathID string
}
func routers() *mux.Router {
r := mux.NewRouter()
// listing files - main display
r.HandleFunc("/", handleFileList).Methods("GET")
r.HandleFunc("/files/{path}", handleFileList).Methods("GET")
// file queue handling
r.HandleFunc("/queue/status", handleQueueStatus).Methods("POST")
r.HandleFunc("/queue/update/{status}", handleQueueUpdate).Methods("POST")
r.Use(authMiddleware)
return r
}
func authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authOk := basicAuth(w, r)
if authOk {
next.ServeHTTP(w, r)
}
})
}
func basicAuth(w http.ResponseWriter, r *http.Request) bool {
if settings.AuthUser == "" && settings.AuthPass == "" {
return true
}
user, pass, ok := r.BasicAuth()
if !ok ||
subtle.ConstantTimeCompare([]byte(user), []byte(settings.AuthUser)) != 1 ||
subtle.ConstantTimeCompare([]byte(pass), []byte(settings.AuthPass)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="gdriver-go"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorized.\n"))
return false
}
return true
}
func handleFileList(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
relPath := vars["path"]
if len(relPath) > 0 {
bytes, err := base64.RawURLEncoding.DecodeString(relPath)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
relPath = string(bytes)
} else {
relPath = "/"
}
rootPath := path.Clean(settings.LocalRoot)
fulPath := path.Clean(rootPath + string(os.PathSeparator) + relPath)
paths, err := listPaths(fulPath)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
parentPath := path.Clean(relPath + string(os.PathSeparator) + "..")
data := FilePageData{
CurrentPath: relPath,
Files: paths,
IsNotRoot: fulPath != rootPath,
ParentPath: parentPath,
ParentPathID: base64.RawURLEncoding.EncodeToString([]byte(parentPath)),
}
tmpl, err := template.ParseFiles("html/files.html")
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
tmpl.Execute(w, data)
}
func handleQueueStatus(w http.ResponseWriter, r *http.Request) {
fileIDs, err := parsePathPost(r)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
statusList, err := getFileStatusList(fileIDs)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)
err = encoder.Encode(statusList)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
}
func handleQueueUpdate(w http.ResponseWriter, r *http.Request) {
fileIDs, err := parsePathPost(r)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
status, err := validateStatus(Status(mux.Vars(r)["status"]))
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
err = updateFileStatus(fileIDs, status)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
statusList, err := getFileStatusList(fileIDs)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
encoder := json.NewEncoder(w)
err = encoder.Encode(statusList)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), 500)
return
}
}
func parsePathPost(r *http.Request) ([]FileID, error) {
decoder := json.NewDecoder(r.Body)
var arr []FileID
err := decoder.Decode(&arr)
if err != nil {
return nil, err
}
defer r.Body.Close()
return arr, nil
}
func validateStatus(status Status) (Status, error) {
switch status {
case StatusUnknown,
StatusError,
StatusReady,
StatusPending,
StatusInProgress,
StatusDone:
return status, nil
}
return "", fmt.Errorf("invalid status: %v", status)
}