-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
224 lines (188 loc) · 5.01 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"bytes"
"context"
"fmt"
"html/template"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/mux"
)
type perm interface{}
var (
userPermission = perm("user-permission")
tmpl = template.Must(template.ParseFiles("./static/layout.html"))
)
// pageData stores information for HTML template for Paste uploads.
type pastePageData struct {
Title string
Data string
}
// response is a JSON http response.
type response struct {
Status int
Result result
}
type result struct {
Name string
URL string
DeleteKey string // unused atm.
}
// uploadPasteHandler is an upload handler for text.
func uploadPasteHandler(w http.ResponseWriter, r *http.Request) {
// get permissions
perms := r.Context().Value(userPermission).(string)
r.Body = http.MaxBytesReader(w, r.Body, int64(config.getMaxUpload(perms, db)<<20))
fmt.Println(r)
if err := r.ParseMultipartForm(int64(config.getMaxUpload(perms, db) << 20)); err != nil {
fmt.Println(err)
renderError(w, &fileSizeError)
return
}
// parse and validate file and post parameters
paste, _, err := r.FormFile("file")
if err != nil {
renderError(w, &fileReadError)
fmt.Println(err)
return
}
fileBytes, err := ioutil.ReadAll(paste)
if err != nil {
renderError(w, &fileReadError)
fmt.Println(err)
return
}
// creates a buffer and compiles data for template.
tpl := bytes.Buffer{}
data := pastePageData{
Title: "Paste",
Data: string(fileBytes),
}
// renders the template.
if err := tmpl.Execute(&tpl, data); err != nil {
fmt.Println(err)
renderError(w, &fileWriteError)
return
}
// Create file in save location.
newFilename := randomToken(6)
newPath := filepath.Join(uploadPastePath, newFilename)
newFile, err := os.Create(newPath)
if err != nil {
renderError(w, &fileWriteError)
return
}
defer func(newFile *os.File) {
err := newFile.Close()
if err != nil {
}
}(newFile)
if _, err := newFile.Write(tpl.Bytes()); err != nil || newFile.Close() != nil {
renderError(w, &fileWriteError)
return
}
// send the response
_, err = w.Write(createResponse("paste", newFilename))
if err != nil {
return
}
}
// uploadFileHandler is a upload handler for files.
func uploadFileHandler(w http.ResponseWriter, r *http.Request) {
// get permissions
perms := r.Context().Value(userPermission).(string)
// validate file size
r.Body = http.MaxBytesReader(w, r.Body, int64(config.getMaxUpload(perms, db)<<20))
if err := r.ParseMultipartForm(int64(config.getMaxUpload(perms, db) << 20)); err != nil {
renderError(w, &fileSizeError)
fmt.Println(int64(config.getMaxUpload(perms, db)))
return
}
// parse and validate file and post parameters
file, _, err := r.FormFile("file")
if err != nil {
renderError(w, &invalidFileError)
return
}
defer func(file multipart.File) {
err := file.Close()
if err != nil {
}
}(file)
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
renderError(w, &invalidFileError)
return
}
// check file type.
fileType, err := validateFileType(&fileBytes)
if err != nil {
renderError(w, err.(*httpError))
return
}
// Create file in save location.
newFilename := randomToken(6) + fileType
newPath := filepath.Join(uploadFilePath, newFilename)
newFile, err := os.Create(newPath)
if err != nil {
renderError(w, &fileWriteError)
return
}
defer func(newFile *os.File) {
err := newFile.Close()
if err != nil {
}
}(newFile) // idempotent, okay to call twice
if _, err := newFile.Write(fileBytes); err != nil || newFile.Close() != nil {
renderError(w, &fileWriteError)
return
}
// send the response
_, err = w.Write(createResponse("file", newFilename))
if err != nil {
return
}
}
// permissionMiddleware validates the password provided in a request.
// If the password is valid, it adds the correct permissionGroup to the
// request's context and passes it to the upload handler.
func permissionMiddleware(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var ctx context.Context
var user = r.FormValue("user")
var pass = r.FormValue("pass")
var uservalidity = config.getUserValidity(user, db)
var userpass = config.getPassHash(user, db)
var useradmin = config.getUserAdmin(user, db)
if uservalidity && userpass == pass {
fmt.Println("valid user")
// check password
if useradmin {
fmt.Println("Admin Upload")
ctx = context.WithValue(ctx, userPermission, user)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
fmt.Println("Default Upload")
ctx = context.WithValue(ctx, userPermission, user)
next.ServeHTTP(w, r.WithContext(ctx))
}
} else {
renderError(w, &invalidPasswordError)
return
}
})
}
// detailsHandler responds with simple file details.
func detailsHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, err := w.Write([]byte(vars["file"]))
if err != nil {
return
}
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/pages/404.html")
}