-
Notifications
You must be signed in to change notification settings - Fork 0
/
blobRoutes.go
222 lines (197 loc) · 5.72 KB
/
blobRoutes.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"encoding/xml"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/julienschmidt/httprouter"
)
// DELETEs the blob
func blobDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params, c BlobRequestContext) {
container := ps.ByName("container")
blob := ps.ByName("blob")
blobPath := filepath.Join(blobBase, container, blob)
if filepath.Clean(blobPath) != blobPath {
log.Printf("Bad path %s", blobPath)
http.Error(w, "Bad Request", 400)
}
err := os.Remove(blobPath)
if err != nil && os.IsNotExist(err) {
http.Error(w, "File Not Found", 404)
return
} else if err != nil {
panic(err)
}
http.Error(w, "Accepted", 202)
}
// GETs the blob
func blobGet(w http.ResponseWriter, r *http.Request, ps httprouter.Params, c BlobRequestContext) {
container := ps.ByName("container")
blob := ps.ByName("blob")
blobPath := filepath.Join(blobBase, container, blob)
if filepath.Clean(blobPath) != blobPath {
log.Printf("Bad path %s", blobPath)
http.Error(w, "Bad Request", 400)
}
blobData, err := ioutil.ReadFile(blobPath)
if err != nil && os.IsNotExist(err) {
http.Error(w, "File Not Found", 404)
return
} else if err != nil {
panic(err)
}
defer r.Body.Close()
if r.Form.Get("rscd") != "" {
w.Header().Set("Content-Disposition", r.Form.Get("rscd"))
}
w.Write(blobData)
}
// Handles a PUT on a blob (creates the blob, block, or commits the block list)
func blobPut(w http.ResponseWriter, r *http.Request, ps httprouter.Params, c BlobRequestContext) {
container := ps.ByName("container")
blob := ps.ByName("blob")
blobPath := filepath.Join(blobBase, container, blob)
blobUncommittedPath := filepath.Join(blobBase, container, "uncommitted", blob)
if filepath.Clean(blobPath) != blobPath {
log.Printf("Bad path %s", blobPath)
http.Error(w, "Bad Request", 400)
}
if filepath.Clean(blobUncommittedPath) != blobUncommittedPath {
log.Printf("Bad path %s", blobUncommittedPath)
http.Error(w, "Bad Request", 400)
}
blobData, err := ioutil.ReadAll(r.Body)
check(err)
defer r.Body.Close()
comp := r.FormValue("comp")
if comp == "block" {
blockid := r.FormValue("blockid")
blockPath := blobUncommittedPath + "_" + blockid
if filepath.Clean(blockPath) != blockPath {
log.Printf("Bad path %s", blockPath)
http.Error(w, "Bad Request", 400)
return
}
err = ioutil.WriteFile(blockPath, blobData, 0777)
check(err)
http.Error(w, "Created", 201)
} else if comp == "blocklist" {
list := BlobBlockListResponse{}
err := xml.Unmarshal(blobData, &list)
if err != nil {
log.Println("Bad block list")
http.Error(w, "Bad Request", 400)
return
}
file, err := os.Create(blobPath)
check(err)
defer file.Close()
for _, blockId := range list.Blocks {
blockPath := blobUncommittedPath + "_" + blockId
blockData, err := ioutil.ReadFile(blockPath)
check(err)
os.Remove(blockPath)
_, err = file.Write(blockData)
check(err)
}
http.Error(w, "Created", 201)
} else {
err = ioutil.WriteFile(blobPath, blobData, 0777)
check(err)
http.Error(w, "Created", 201)
}
}
// Handles a GET on a container (lists blobs)
func blobContainerGet(w http.ResponseWriter, r *http.Request, ps httprouter.Params, c BlobRequestContext) {
container := ps.ByName("container")
prefix := r.FormValue("prefix")
containerPath := filepath.Join(blobBase, container)
if filepath.Clean(containerPath) != containerPath {
log.Printf("Bad path %s", containerPath)
http.Error(w, "Bad Request", 400)
return
}
blobs := []BlobResult{}
containerInfo, err := ioutil.ReadDir(containerPath)
check(err)
for _, file := range containerInfo {
if !file.IsDir() {
if prefix != "" && !strings.HasPrefix(file.Name(), prefix) {
continue
}
blobs = append(blobs, BlobResult{
Name: file.Name(),
Snapshot: time.Now(),
Properties: BlobPropertiesResult{
LastModified: TimeRFC1123(file.ModTime()),
Etag: "",
ContentMD5: "",
ContentLength: file.Size(),
ContentType: "",
ContentEncoding: "",
CacheControl: "",
ContentLanguage: "",
ContentDisposition: "",
BlobType: BlobTypeBlock,
SequenceNumber: 0,
CopyID: "",
CopyStatus: "",
CopySource: "",
CopyProgress: "",
CopyCompletionTime: TimeRFC1123(time.Now()),
CopyStatusDescription: "",
LeaseStatus: "",
LeaseState: "",
LeaseDuration: "",
ServerEncrypted: false,
IncrementalCopy: false,
},
Metadata: "",
})
}
}
err = writeXML(w, BlobEnumerationResults{
xml.Name{},
"",
"",
0,
"",
blobs,
"",
})
check(err)
}
// Handles a PUT on a container (creates the container)
func blobContainerPut(w http.ResponseWriter, r *http.Request, ps httprouter.Params, c BlobRequestContext) {
restype := r.FormValue("restype")
container := ps.ByName("container")
if restype == "container" {
containerPath := filepath.Join(blobBase, container)
if filepath.Clean(containerPath) != containerPath {
log.Printf("Bad path %s", containerPath)
http.Error(w, "Bad Request", 400)
return
}
containerUncommittedPath := filepath.Join(containerPath, "uncommitted")
_, err := os.Stat(containerPath)
if err != nil {
err = os.Mkdir(containerPath, 0777)
check(err)
err = os.Mkdir(containerUncommittedPath, 0777)
check(err)
http.Error(w, "Created", 201)
} else {
http.Error(w, "The specified container already exists.", 409)
writeXML(w, BlobErrorResponse{
Code: "ContainerAlreadyExists",
Message: "The specified container already exists.",
})
}
} else {
http.Error(w, "Bad Request", 400)
}
}