-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
48 lines (39 loc) · 1.01 KB
/
upload.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"path/filepath"
)
// upload
// Upload .
func Upload(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response = make(map[string]interface{})
body := map[string]string{}
// file upload
r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("photo")
if err != nil {
fmt.Println("Upload", err)
}
if file != nil {
defer file.Close()
fileName, uploaded := uploadToS3(docS3Path, file, filepath.Ext(handler.Filename))
if !uploaded {
fmt.Println("Upload", err)
SetReponseStatus(w, r, statusCodeServerError, "", dialogType, response)
return
}
body["image"] = fileName
}
//
response["data"] = []map[string]string{body}
response["meta"] = setMeta(statusCodeOk, "Image upload", "")
w.WriteHeader(getHTTPStatusCode(response["meta"].(map[string]string)["status"]))
meta, required := checkAppUpdate(r)
if required {
response["meta"] = meta
}
json.NewEncoder(w).Encode(response)
}