-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
187 lines (168 loc) · 5.56 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
func getJSONStringHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("[Request] %s", r.URL.Path)
var data interface{}
err = json.NewDecoder(r.Body).Decode(&data)
if respondIfError(err, w, fmt.Sprintf("Valid JSON body required. Error: %v", err), err400) {
return
}
b, err := json.Marshal(data)
if respondIfError(err, w, fmt.Sprintf("Valid JSON body required. Error: %v", err), err500) {
return
}
respondJSON(w, string(b), ok200)
}
// dialHandler connects to cloud storage provider and validates whether credentials are correct
func dialHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("[Request] %s", r.URL.Path)
_, _, success := connect(w, r)
respondJSON(w, map[string]bool{"success": success}, ok200)
}
func containersHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("[Request] %s", r.URL.Path)
con, loc, success := connect(w, r)
if !success {
return
}
containers, cursor, err := loc.Containers(con.Cursor, "", con.Count)
if respondIfError(err, w, fmt.Sprintf("Failed to retrieve containers. Error: %v", err), err500) {
return
}
result := ContainersResult{}
result.Count = con.Count
result.Cursor = cursor
result.Containers = map[string]string{}
for i := 0; i < len(containers); i++ {
result.Containers[containers[i].ID()] = containers[i].Name()
}
respondJSON(w, result, ok200)
}
func itemsHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("[Request] %s", r.URL.Path)
con, loc, success := connect(w, r)
if !success {
return
}
container, err := loc.Container(con.ContainerName)
if respondIfError(err, w, fmt.Sprintf("Failed to retrieve container. Error: %v", err), err500) {
return
}
items, cursor, err := container.Items("", con.Cursor, con.Count)
result := ItemsResult{}
result.Count = con.Count
result.Cursor = cursor
for i := 0; i < len(items); i++ {
size, _ := items[i].Size()
metadata, _ := items[i].Metadata()
result.Items = append(result.Items, Item{
ID: items[i].ID(),
Name: items[i].Name(),
Size: size,
URL: items[i].URL().Host + items[i].URL().Path,
Metadata: metadata,
})
}
respondJSON(w, result, ok200)
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("[Request] %s", r.URL.Path)
con, destLoc, success := connectJSON(w, []byte(r.FormValue("to")))
if !success {
return
}
file, fileHandle, err := r.FormFile("file")
if respondIfError(err, w, fmt.Sprintf("Invalid file or file read failed. Error: %v", err), err400) {
return
}
defer file.Close()
destContainer, err := destLoc.Container(con.ContainerName)
if respondIfError(err, w, fmt.Sprintf("Failed to retrieve container. Error: %v", err), err500) {
return
}
name := con.ItemName
if name == "" {
name = fileHandle.Filename
}
log.Printf("Uploading %s to %s/%s", name, con.Kind, con.ContainerName)
item, err := destContainer.Put(name, file, fileHandle.Size, map[string]interface{}{})
if respondIfError(err, w, fmt.Sprintf("Failed to upload file to %s. Error: %v", con.Kind, err), err500) {
return
}
log.Printf("Upload complete: %s to %s", name, con.Kind)
respondJSON(w, item, created201)
}
func copyHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("[Request] %s", r.URL.Path)
con := struct {
From Connection `json:"from"`
To Connection `json:"to"`
}{}
err = json.NewDecoder(r.Body).Decode(&con)
if respondIfError(err, w, fmt.Sprintf("Valid JSON body required. Error: %v", err), err400) {
return
}
log.Printf("Dialing source: %s/%s", con.From.Kind, con.From.ContainerName)
sourceLoc, err := con.From.Dial()
if respondIfError(err, w, fmt.Sprintf("Connection to %s failed. %v", con.From.Kind, err), err500) {
return
}
log.Printf("Connected to %s/%s", con.From.Kind, con.From.ContainerName)
sourceContainer, err := sourceLoc.Container(con.From.ContainerName)
if respondIfError(err, w, fmt.Sprintf("Failed to retrieve container. Error: %v", err), err500) {
return
}
itemID := con.From.ItemID
if itemID == "" {
itemID = con.From.ItemName
}
item, err := sourceContainer.Item(itemID)
log.Printf("Dialing destination: %s/%s", con.To.Kind, con.To.ContainerName)
destLoc, err := con.To.Dial()
if respondIfError(err, w, fmt.Sprintf("Connection to %s failed. %v", con.To.Kind, err), err500) {
return
}
log.Printf("Connected to %s/%s", con.To.Kind, con.To.ContainerName)
destContainer, err := destLoc.Container(con.To.ContainerName)
if respondIfError(err, w, fmt.Sprintf("Failed to retrieve container. Error: %v", err), err500) {
return
}
size, err := item.Size()
if respondIfError(err, w, fmt.Sprintf("Failed to retrieve file size. Error: %v", err), err500) {
return
}
metadata, err := item.Metadata()
if respondIfError(err, w, fmt.Sprintf("Failed to retrieve file metadata. Error: %v", err), err500) {
return
}
name := con.To.ItemName
if name == "" {
name = con.To.ItemID
if name == "" {
name = item.Name()
}
}
reader, err := item.Open()
if respondIfError(err, w, fmt.Sprintf("Failed to open file. Error: %v", err), err500) {
return
}
defer reader.Close()
log.Printf("Transfering %s from %s/%s to %s/%s", name, con.From.Kind, con.From.ContainerName, con.To.Kind, con.To.ContainerName)
copiedItem, err := destContainer.Put(name, reader, size, metadata)
if respondIfError(err, w, fmt.Sprintf("File transfer failed. Error: %v", err), err500) {
return
}
resultItem := Item{
ID: copiedItem.ID(),
Name: copiedItem.Name(),
Size: size,
URL: copiedItem.URL().Host + copiedItem.URL().Path,
Metadata: metadata,
}
respondJSON(w, resultItem, created201)
}