-
Notifications
You must be signed in to change notification settings - Fork 56
/
mutate.go
69 lines (61 loc) · 1.71 KB
/
mutate.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
package serverHandler
import (
"net/http"
)
func (h *aliasHandler) mutate(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
success := false
switch {
case data.IsUpload:
if data.CanUpload {
success = h.saveUploadFiles(session.authUserName, h.fs+session.aliasReqPath, data.CanMkdir, data.CanDelete, data.AliasSubItems, r)
} else {
w.WriteHeader(http.StatusBadRequest)
return
}
case data.IsMkdir:
if data.CanMkdir && !h.logError(r.ParseForm()) {
success = h.mkdirs(session.authUserName, h.fs+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
} else {
w.WriteHeader(http.StatusBadRequest)
return
}
case data.IsDelete:
if data.CanDelete && !h.logError(r.ParseForm()) {
success = h.deleteItems(session.authUserName, h.fs+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
} else {
w.WriteHeader(http.StatusBadRequest)
return
}
}
if session.wantJson {
header := w.Header()
header.Set("Content-Type", "application/json; charset=utf-8")
header.Set("Cache-Control", "public, max-age=0")
if success {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"success":true}`))
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"success":false}`))
}
} else {
reqPath := session.prefixReqPath
ctxQsList := r.Form["contextquerystring"]
ctxQsListLen := len(ctxQsList)
if ctxQsListLen > 0 {
ctxQs := ctxQsList[ctxQsListLen-1]
if len(ctxQs) > 0 {
reqPath += ctxQs
}
}
if success {
http.Redirect(w, r, reqPath, http.StatusFound)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
}