/
upload_handler.go
81 lines (64 loc) · 1.63 KB
/
upload_handler.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
package main
import (
"encoding/json"
"fmt"
"github.com/vpereira/signer"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
// TODO
// return on erros should be a json as well
func UploadHandler(res http.ResponseWriter, req *http.Request) {
var (
status int
err error
)
defer func() {
if nil != err {
http.Error(res, err.Error(), status)
}
}()
// parse request
const _24K = (1 << 20) * 24
if err = req.ParseMultipartForm(_24K); nil != err {
status = http.StatusInternalServerError
return
}
for _, fheaders := range req.MultipartForm.File {
for _, hdr := range fheaders {
// open uploaded
var infile multipart.File
if infile, err = hdr.Open(); nil != err {
status = http.StatusInternalServerError
return
}
// open destination
var outfile *os.File
if outfile, err = os.Create(filepath.Join(Config_map_string["upload_dir"], hdr.Filename)); nil != err {
status = http.StatusInternalServerError
return
}
defer outfile.Close()
// 32K buffer copy
var written int64
if written, err = io.Copy(outfile, infile); nil != err {
status = http.StatusInternalServerError
return
}
uploaded_file := NewUploadedFile(hdr.Filename, "uploaded", written)
signer := signer.NewSigner(Config_map_string["email"], "secring.gpg",
Config_map_string["sign_dir"])
go signer.SignIt(uploaded_file.Sha256, fmt.Sprintf("%s", uploaded_file.Sha256))
js, err := json.Marshal(uploaded_file)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.Header().Set("Content-Type", "application/json")
res.Write(js)
}
}
}