forked from deta/deta-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.go
279 lines (237 loc) · 5.8 KB
/
drive.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package deta
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
)
const (
uploadChunkSize = 1024 * 1024 * 10
)
var (
// ErrEmptyName empty name
ErrEmptyName = errors.New("name is empty")
// ErrEmptyNames empty names
ErrEmptyNames = errors.New("names is empty")
// ErrTooManyNames too many items
ErrTooManyNames = errors.New("more than 1000 files to delete")
// ErrEmptyData no data
ErrEmptyData = errors.New("no data provided")
)
// Drive is a Deta Drive service client that offers the API to make requests to Deta Drive
type Drive struct {
// deta api client
client *detaClient
// auth info for authenticating requests
auth *authInfo
}
// NewDrive returns a pointer to new Drive
func newDrive(projectKey, driveName, rootEndpoint string) *Drive {
parts := strings.Split(projectKey, "_")
projectID := parts[0]
// root endpoint for the base
rootEndpoint = fmt.Sprintf("%s/%s/%s", rootEndpoint, projectID, driveName)
return &Drive{
client: newDetaClient(rootEndpoint, &authInfo{
authType: "api-key",
headerKey: "X-API-Key",
headerValue: projectKey,
}),
}
}
type deleteManyRequest struct {
Names []string `json:"names"`
}
type DeleteManyOutput struct {
Deleted []string `json:"deleted"`
Failed map[string]string `json:"failed"`
}
// DeleteMany deletes multiple files in the Drive.
//
// Deletes at most 1000 files in a single request.
// The files names should be in a slice.
// Returns a response in the DeleteManyOutput interface format.
func (d *Drive) DeleteMany(names []string) (*DeleteManyOutput, error) {
if len(names) == 0 || names == nil {
return nil, ErrEmptyNames
}
if len(names) > 1000 {
return nil, ErrTooManyNames
}
o, err := d.client.request(&requestInput{
Path: "/files",
Method: "DELETE",
Body: &deleteManyRequest{
Names: names,
},
})
if err != nil {
return nil, err
}
var dr DeleteManyOutput
err = json.Unmarshal(o.Body, &dr)
if err != nil {
return nil, err
}
return &dr, nil
}
// Delete a file from the drive
//
// If deleted (even if file with such name does not exist), returns the name
// Else returns an error
func (d *Drive) Delete(name string) (string, error) {
if name == "" {
return name, ErrEmptyName
}
payload := make([]string, 1)
payload = append(payload, name)
dr, err := d.DeleteMany(payload)
if err != nil {
return name, err
}
_, ok := dr.Failed[name]
if !ok {
return name, fmt.Errorf("failed to delete %s", name)
}
return name, nil
}
type ListOutput struct {
Paging *paging `json:"paging"`
Names []string `json:"names"`
}
// List file names from the Drive.
//
// List is paginated, returns the last name fetched, and the size if further pages are left.
// Provide the last name in the subsequent list operation to list remaining pages.
func (d *Drive) List(limit int, prefix, last string) (*ListOutput, error) {
url := fmt.Sprintf("/files?limit=%d", limit)
if prefix != "" {
url = url + fmt.Sprintf("&prefix=%s", prefix)
}
if last != "" {
url = url + fmt.Sprintf("&last=%s", last)
}
o, err := d.client.request(&requestInput{
Path: url,
Method: "GET",
})
if err != nil {
return nil, err
}
var lr ListOutput
err = json.Unmarshal(o.Body, &lr)
if err != nil {
return nil, err
}
return &lr, nil
}
// Get a file from the Drive.
//
// Returns a io.ReadCloser for the file from the Drive.
func (d *Drive) Get(name string) (io.ReadCloser, error) {
if name == "" {
return nil, ErrEmptyName
}
url := fmt.Sprintf("/files/download?name=%s", name)
o, err := d.client.request(&requestInput{
Path: url,
Method: "GET",
Read: true,
})
if err != nil {
return nil, err
}
return o.RawBody, nil
}
type startUploadResponse struct {
UploadID string `json:"upload_id"`
Name string `json:"name"`
ProjectID string `json:"project_id"`
DriveName string `json:"drive_name"`
}
// Initializes a chuncked file upload.
//
// If successful, returns the UploadID
func (d *Drive) startUpload(name string) (string, error) {
url := fmt.Sprintf("/uploads?name=%s", name)
o, err := d.client.request(&requestInput{
Path: url,
Method: "POST",
})
if err != nil {
return "", err
}
var sr startUploadResponse
err = json.Unmarshal(o.Body, &sr)
if err != nil {
return "", err
}
return sr.UploadID, nil
}
// End a chuncked upload.
func (d *Drive) finishUpload(name, uploadId string) {
url := fmt.Sprintf("/uploads/%s?name=%s", uploadId, name)
_, _ = d.client.request(&requestInput{
Path: url,
Method: "PATCH",
})
}
// Abort a chunked upload.
func (d *Drive) abortUpload(name, uploadId string) {
url := fmt.Sprintf("/uploads/%s?name=%s", uploadId, name)
_, _ = d.client.request(&requestInput{
Path: url,
Method: "DELETE",
})
}
// Uploads a chunked part.
func (d *Drive) uploadPart(name string, chunk []byte, uploadId string, part int, contentType string) error {
url := fmt.Sprintf("/uploads/%s/parts?name=%s&part=%d", uploadId, name, part)
_, err := d.client.request(&requestInput{
Path: url,
Method: "POST",
RawBody: chunk,
ContentType: contentType,
})
if err != nil {
return err
}
return nil
}
type PutInput struct {
Name string
Body io.Reader
ContentType string
}
// Put a file in the Drive.
//
// Returns the name of file that was put in the drive.
func (d *Drive) Put(i *PutInput) (string, error) {
if i.Name == "" {
return i.Name, ErrEmptyName
}
if i.Body == nil {
return i.Name, ErrEmptyData
}
// start upload
uploadId, _ := d.startUpload(i.Name)
contentStream := i.Body
part := 1
for {
chunk := make([]byte, uploadChunkSize)
n, err := contentStream.Read(chunk)
chunk = chunk[:n]
if err == io.EOF {
fmt.Printf("finishing")
d.finishUpload(i.Name, uploadId)
return i.Name, nil
}
err = d.uploadPart(i.Name, chunk, uploadId, part, i.ContentType)
part = part + 1
if err != nil {
d.abortUpload(i.Name, uploadId)
return i.Name, err
}
}
}