-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
239 lines (215 loc) · 8.35 KB
/
http.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
// https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/04.5.html
// https://nesv.github.io/golang/2014/02/25/worker-queues-in-go.html
import (
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/rs/xid"
)
// receive file from http request, dump to local storage first
func PostObject(w http.ResponseWriter, r *http.Request) {
entityType, entityId, _ := extractEntityVars(r)
uploadReq := &UploadRequest{RequestId: xid.New().String()}
// TODO validate auth with api, this is only the start
if config.EnableAuth {
cookie, err := r.Cookie("JSESSIONID")
if err != nil {
handleError(&w, fmt.Sprintf("Cannot validate authSession %v", r.Body), err, http.StatusForbidden)
return
}
log.Printf("Found api session %v, continue", cookie.Value)
}
// Looks also promising: https://golang.org/pkg/net/http/#DetectContentType DetectContentType
// implements the algorithm described at https://mimesniff.spec.whatwg.org/ to determine the Content-Type of the given data.
contentType := r.Header.Get("Content-type") /* case insentive, returns "" if not found */
log.Printf("PostObject requestId=%s path=%v entityType=%v id=%v",
uploadReq.RequestId, r.URL.Path, entityType, entityId)
if strings.HasPrefix(contentType, "application/json") { // except JSON formatted download request
decoder := json.NewDecoder(r.Body)
var dr DownloadRequest
err := decoder.Decode(&dr) // check if request can be parsed into JSON
if err != nil {
handleError(&w, fmt.Sprintf("Cannot parse %v into DownloadRequest", r.Body), err, http.StatusBadRequest)
return
}
if dr.URL == "" {
handleError(&w, fmt.Sprintf("key 'url' not found in DownloadRequest %v", dr), err, http.StatusBadRequest)
return
}
uploadReq.Origin = dr.URL
// if request contains a filename, use this instead and append suffix if not present
fileExtension := StripRequestParams(filepath.Ext(dr.URL))
if dr.Filename != "" {
uploadReq.Filename = dr.Filename
if !HasExtension(uploadReq.Filename) {
uploadReq.Filename = uploadReq.Filename + fileExtension
// if the original URL also has no extension, we need to rely on s3worker
// (which detected the Mimetype to fix this
}
} else {
uploadReq.Filename = StripRequestParams(path.Base(dr.URL))
}
log.Printf("Trigger URL DownloadRequest url=%s filename=%s ext=%s", dr.URL, uploadReq.Filename, fileExtension)
// delegate actual download from URL to downloadFile
uploadReq.LocalPath, uploadReq.Size = downloadFile(dr.URL, uploadReq.Filename)
} else if strings.HasPrefix(contentType, "multipart/form-data") {
inMemoryFile, handler, err := r.FormFile(config.Fileparam)
if err != nil {
handleError(&w, fmt.Sprintf("Error looking for %s", config.Fileparam), err, http.StatusBadRequest)
return
}
uploadReq.Filename, uploadReq.Origin = handler.Filename, "multipart/form-data"
// delegate dump from request to temporary file to copyFileFromMultipart() function
uploadReq.LocalPath, uploadReq.Size = copyFileFromMultipart(inMemoryFile, handler.Filename)
} else {
handleError(&w, "can only process json or multipart/form-data requests", nil, http.StatusUnsupportedMediaType)
return
}
if uploadReq.Size < 1 {
handleError(&w, fmt.Sprintf("UploadRequest %v unexpected dumpsize < 1", uploadReq), nil, http.StatusBadRequest)
return
}
log.Printf("PostObject successfully dumped to temp storage as %s", uploadReq.LocalPath)
// Push the uploadReq onto the queue.
uploadReq.Key = fmt.Sprintf("%s%s/%s/%s", config.S3Prefix, entityType, entityId, uploadReq.Filename)
// Push the uploadReq onto the queue.
uploadQueue <- *uploadReq
log.Printf("S3UploadRequest %s queued with requestId=%s", uploadReq.Key, uploadReq.RequestId)
w.Header().Set("Content-Type", "application/json")
uploadRequestJson, err := json.Marshal(uploadReq)
if err != nil {
handleError(&w, "cannot marshal request", err, http.StatusInternalServerError)
return
}
w.Write(uploadRequestJson)
}
// called by PostObject if request payload is download request
func downloadFile(url string, filename string) (string, int64) {
localFilename := filepath.Join(config.Dumpdir, filename)
// Get the data
resp, err := http.Get(url)
if err != nil {
log.Printf("%s", err)
return "", -1
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(localFilename)
if err != nil {
log.Printf("%s", err)
return "", -1
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Printf("%s", err)
return "", -1
}
fSize := FileSize(out)
return localFilename, fSize
}
// called by PostObject if payload is multipar file
// which we dump into a local temporary file
func copyFileFromMultipart(inMemoryFile multipart.File, filename string) (string, int64) {
defer inMemoryFile.Close()
//fmt.Fprintf(w, "%v", handler.Header)
localFilename := filepath.Join(config.Dumpdir, filename)
localFile, err := os.OpenFile(localFilename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Printf("Error %v", err)
return "", -1
}
defer localFile.Close()
io.Copy(localFile, inMemoryFile)
fSize := FileSize(localFile)
return localFilename, fSize
}
/* Get a list of objects given a path such as places/12345 */
func ListObjects(w http.ResponseWriter, r *http.Request) {
entityType, entityId, _ := extractEntityVars(r)
prefix := fmt.Sprintf("%s%s/%s", config.S3Prefix, entityType, entityId)
lr, _ := s3Handler.ListObjectsForEntity(prefix)
// https://stackoverflow.com/questions/28595664/how-to-stop-json-marshal-from-escaping-and/28596225
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false) // or & will be escaped with unicode chars
if err := enc.Encode(&lr.Items); err != nil {
log.Println(err)
}
}
// Get presigned url for given a path such as places/12345/hase.txt
// support for resized version if ?small, ?medium and ?large request param is present
func GetObjectPresignUrl(w http.ResponseWriter, r *http.Request) {
// check for ?small etc. request param
resizePath := parseResizeParams(r)
entityType, entityId, item := extractEntityVars(r)
key := fmt.Sprintf("%s%s/%s/%s%s", config.S3Prefix, entityType, entityId, resizePath, item)
target := s3Handler.GetS3PresignedUrl(key)
log.Printf("redirecting to key %s with presign url", key)
http.Redirect(w, r, target,
// see comments below and consider the codes 308, 302, or 301
http.StatusTemporaryRedirect)
}
// Get presigned url for given a path such as places/12345/hase.txt
// support for resized version if ?small, ?medium and ?large request param is present
func DeleteObject(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
http.Error(w, "only supported method is "+http.MethodDelete, http.StatusBadRequest)
return
}
entityType, entityId, item := extractEntityVars(r)
key := fmt.Sprintf("%s%s/%s/%s", config.S3Prefix, entityType, entityId, item)
log.Printf("Delete %s yet to be implemented", key)
w.WriteHeader(http.StatusNoContent) // send the headers with a 204 response cod
}
// A very simple Hztp Health check.
func Health(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
status, err := json.Marshal(map[string]interface{}{
"status": "up",
"info": fmt.Sprintf("%s is healthy", appPrefix),
"time": time.Now().Format(time.RFC3339),
"memstats": MemStats(),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(status)
}
/* helper helper helper helper */
func handleError(writer *http.ResponseWriter, msg string, err error, code int) {
log.Printf("[ERROR] %s - %v", msg, err)
http.Error(*writer, fmt.Sprintf("%s - %v", msg, err), code)
}
// get common vars from path variables e.g. /{entityType}/{entityId}/{item}"
func extractEntityVars(r *http.Request) (entityType string, entityId string, key string) {
vars := mux.Vars(r)
return vars["entityType"], vars["entityId"], vars["item"] // if not found -> no problem
}
// check for ?small, ?medium etc. request param
func parseResizeParams(r *http.Request) string {
parseErr := r.ParseForm()
if parseErr != nil {
log.Printf("WARN: Cannot parse request URI %s", r.RequestURI)
return ""
}
for resizeMode, _ := range config.ResizeModes {
_, isRequested := r.Form[resizeMode]
if isRequested {
return resizeMode + "/"
}
}
return ""
}