forked from nulpunt/nulpunt
/
http_sessionData.go
107 lines (91 loc) · 2.41 KB
/
http_sessionData.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func sessionDataBlobSave(w http.ResponseWriter, r *http.Request) {
type inDataType struct {
Name string `json:"name"`
Blob string `json:"blob"`
}
cs, err := getClientSession(r.Header.Get(headerKeySessionKey))
if err != nil {
http.Error(w, "error", http.StatusInternalServerError)
return
}
defer cs.done()
acc := cs.account
if acc == nil {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
inData := &inDataType{}
err = json.NewDecoder(r.Body).Decode(inData)
defer r.Body.Close()
if err != nil {
log.Printf("error decoding body for sessionDataSave: %s\n", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
file, err := gridFS.Create(fmt.Sprintf("/blobs/%s/%s", acc.Username, inData.Name))
if err != nil {
log.Printf("error creating GridFile for blob: %s\n", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
defer file.Close()
_, err = file.Write([]byte(inData.Blob))
if err != nil {
log.Printf("error writing to blob GridFile: %s\n", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
return
}
func sessionDataBlobLoad(w http.ResponseWriter, r *http.Request) {
type inDataType struct {
Name string `json:"name"`
}
cs, err := getClientSession(r.Header.Get(headerKeySessionKey))
if err != nil {
http.Error(w, "error", http.StatusInternalServerError)
return
}
defer cs.done()
acc := cs.account
if acc == nil {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
inData := &inDataType{}
err = json.NewDecoder(r.Body).Decode(inData)
defer r.Body.Close()
if err != nil {
log.Printf("error decoding body for sessinoDataBlobLoad: %s\n", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
file, err := gridFS.Open(fmt.Sprintf("/blobs/%s/%s", acc.Username, inData.Name))
if err != nil {
log.Printf("error opening GridFile for blob: %s\n", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
defer file.Close()
dataBlob, err := ioutil.ReadAll(file)
if err != nil {
log.Printf("error reading all from GridFile: %s\n ", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
_, err = w.Write(dataBlob)
if err != nil {
log.Printf("error writing blob data to client: %s\n", err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
return
}