-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
87 lines (72 loc) · 1.4 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
package main
import (
"io"
"net/http"
"os"
"path/filepath"
)
func registerHandlers() {
if goFileServer {
http.Handle("/", http.FileServer(http.Dir(exports[0])))
} else {
registerShitHandlers()
}
}
func registerShitHandlers() {
http.HandleFunc("GET /", get)
if upload {
http.HandleFunc("POST /", post)
}
}
func get(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
if err := serveRoot(w, r); err != nil {
httpErr(w, err)
}
return
}
if err := serveExports(w, r); err != nil {
httpErr(w, err)
}
}
func post(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(maxUploadMemory); err != nil {
httpErr(w, err)
}
path, err := getRealPath(r.URL.Path)
if err != nil {
httpErr(w, err)
}
if path == "" {
http.NotFound(w, r)
}
fileinfo, err := os.Stat(path)
if err != nil {
httpErr(w, err)
}
if !fileinfo.IsDir() {
http.NotFound(w, r)
return
}
files := r.MultipartForm.File["files"]
for _, header := range files {
file, err := header.Open()
if err != nil {
httpErr(w, err)
return
}
defer file.Close()
dst, err := os.Create(filepath.Join(path, header.Filename))
if err != nil {
http.Error(w, "Error saving file", http.StatusInternalServerError)
return
}
defer dst.Close()
_, err = io.Copy(dst, file)
if err != nil {
httpErr(w, err)
return
}
}
http.Redirect(w, r, r.URL.Path, http.StatusSeeOther)
}