-
Notifications
You must be signed in to change notification settings - Fork 26
/
handlers.go
98 lines (73 loc) · 2.75 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
package blobber
import (
"encoding/json"
"net/http"
"github.com/0chain/gosdk/dev/blobber/model"
"github.com/gorilla/mux"
)
func uploadAndUpdateFile(w http.ResponseWriter, req *http.Request) {
uploadMeta := req.FormValue("uploadMeta")
var form *model.UploadFormData
err := json.Unmarshal([]byte(uploadMeta), &form)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
//nolint: errcheck
json.NewEncoder(w).Encode(&model.UploadResult{
Filename: form.Filename,
ValidationRoot: form.ValidationRoot,
FixedMerkleRoot: form.FixedMerkleRoot,
})
}
func getReference(w http.ResponseWriter, req *http.Request) {
var vars = mux.Vars(req)
alloctionID := vars["allocation"]
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
result, ok := referencePathResults[alloctionID]
if ok {
buf, _ := json.Marshal(result)
//nolint: errcheck
w.Write(buf)
return
}
rootRefs := `{"meta_data":{"chunk_size":0,"created_at":0,"hash":"","lookup_hash":"","name":"/","num_of_blocks":0,"path":"/","path_hash":"","size":0,"type":"d","updated_at":0},"Ref":{"ID":0,"Type":"d","AllocationID":"` + vars["allocation"] + `","LookupHash":"","Name":"/","Path":"/","Hash":"","NumBlocks":0,"PathHash":"","ParentPath":"","PathLevel":1,"CustomMeta":"","ValidationRoot":"","Size":0,"FixedMerkleRoot":"","ActualFileSize":0,"ActualFileHash":"","MimeType":"","WriteMarker":"","ThumbnailSize":0,"ThumbnailHash":"","ActualThumbnailSize":0,"ActualThumbnailHash":"","EncryptedKey":"","Children":null,"OnCloud":false,"CreatedAt":0,"UpdatedAt":0,"ChunkSize":0},"latest_write_marker":null}`
//nolint: errcheck
w.Write([]byte(rootRefs))
}
func commitWrite(w http.ResponseWriter, req *http.Request) {
// var vars = mux.Vars(req)
writeMarker := &model.WriteMarker{}
err := json.Unmarshal([]byte(req.FormValue("write_marker")), writeMarker)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result := &model.CommitResult{}
result.AllocationRoot = writeMarker.AllocationRoot
result.Success = true
result.WriteMarker = writeMarker
err = json.NewEncoder(w).Encode(result)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
func mockRespone(w http.ResponseWriter, statusCode int, respBody []byte) {
w.Header().Set("Content-Type", "application/json")
if respBody != nil {
_, err := w.Write(respBody)
if err != nil {
statusCode = http.StatusInternalServerError
}
}
w.WriteHeader(statusCode)
}
func rollback(w http.ResponseWriter, _ *http.Request) {
mockRespone(w, http.StatusOK, nil)
}
func latestWriteMarker(w http.ResponseWriter, _ *http.Request) {
latestByte := `{"latest_write_marker":null,"prev_write_marker":null}`
mockRespone(w, http.StatusOK, []byte(latestByte))
}