forked from tj/go-dropy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
318 lines (272 loc) · 7.11 KB
/
client.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Package dropy implements a higher-level Dropbox API on top of go-dropbox.
package dropy
import (
"io"
"io/ioutil"
"os"
"time"
"github.com/tj/go-dropbox"
)
// Client wraps dropbox.Client to provide higher level sugar.
type Client struct {
*dropbox.Client
}
// New client.
func New(d *dropbox.Client) *Client {
return &Client{
Client: d,
}
}
// Stat returns file and directory meta-data for `name`.
func (c *Client) Stat(name string) (os.FileInfo, error) {
out, err := c.Files.GetMetadata(&dropbox.GetMetadataInput{
Path: name,
})
if err != nil {
return nil, err
}
return &FileInfo{&out.Metadata}, nil
}
// ListN returns entries in dir `name`. Up to `n` entries, or all when `n` <= 0.
func (c *Client) ListN(name string, n int) (list []os.FileInfo, err error) {
var cursor string
if n <= 0 {
n = -1
}
for {
var out *dropbox.ListFolderOutput
if cursor == "" {
if out, err = c.Files.ListFolder(&dropbox.ListFolderInput{Path: name}); err != nil {
return
}
cursor = out.Cursor
} else {
if out, err = c.Files.ListFolderContinue(&dropbox.ListFolderContinueInput{cursor}); err != nil {
return
}
cursor = out.Cursor
}
if err != nil {
return
}
for _, ent := range out.Entries {
list = append(list, &FileInfo{ent})
}
if n >= 0 && len(list) >= n {
list = list[:n]
break
}
if !out.HasMore {
break
}
}
if n >= 0 && len(list) == 0 {
err = io.EOF
return
}
return
}
// List returns all entries in dir `name`.
func (c *Client) List(name string) ([]os.FileInfo, error) {
return c.ListN(name, 0)
}
// ListFilter returns all entries in dir `name` filtered by `filter`.
func (c *Client) ListFilter(name string, filter func(info os.FileInfo) bool) (ret []os.FileInfo, err error) {
ents, err := c.ListN(name, 0)
if err != nil {
return
}
for _, ent := range ents {
if filter(ent) {
ret = append(ret, ent)
}
}
return
}
// ListFolders returns all folders in dir `name`.
func (c *Client) ListFolders(name string) ([]os.FileInfo, error) {
return c.ListFilter(name, func(info os.FileInfo) bool {
return info.IsDir()
})
}
// ListFiles returns all files in dir `name`.
func (c *Client) ListFiles(name string) ([]os.FileInfo, error) {
return c.ListFilter(name, func(info os.FileInfo) bool {
return !info.IsDir()
})
}
// Open returns a File for reading and writing.
func (c *Client) Open(name string) *File {
r, w := io.Pipe()
return &File{
Name: name,
c: c,
pipeR: r,
pipeW: w,
}
}
// Read returns the contents of `name`.
func (c *Client) Read(name string) ([]byte, error) {
f := c.Open(name)
defer f.Close()
return ioutil.ReadAll(f)
}
// Download returns the contents of `name`.
func (c *Client) Download(name string) (io.ReadCloser, error) {
out, err := c.Files.Download(&dropbox.DownloadInput{name})
if err != nil {
return nil, err
}
return out.Body, nil
}
// Preview returns the PDF preview of `name`.
func (c *Client) Preview(name string) (io.ReadCloser, error) {
out, err := c.Files.GetPreview(&dropbox.GetPreviewInput{name})
if err != nil {
return nil, err
}
return out.Body, nil
}
// Mkdir creates folder `name`.
func (c *Client) Mkdir(name string) error {
_, err := c.Files.CreateFolder(&dropbox.CreateFolderInput{name})
return err
}
// Delete file `name`.
func (c *Client) Delete(name string) error {
_, err := c.Files.Delete(&dropbox.DeleteInput{name})
return err
}
// Copy file from `src` to `dst`.
func (c *Client) Copy(src, dst string) error {
_, err := c.Files.Copy(&dropbox.CopyInput{
FromPath: src,
ToPath: dst,
})
return err
}
// Move file from `src` to `dst`.
func (c *Client) Move(src, dst string) error {
_, err := c.Files.Move(&dropbox.MoveInput{
FromPath: src,
ToPath: dst,
})
return err
}
// Search return results for a search against `path` with the given `query`.
func (c *Client) Search(path, query string) (list []os.FileInfo, err error) {
var start uint64
more:
out, err := c.Files.Search(&dropbox.SearchInput{
Mode: dropbox.SearchModeFilename,
Path: path,
Query: query,
Start: start,
})
if err != nil {
return
}
for _, match := range out.Matches {
list = append(list, &FileInfo{match.Metadata})
}
if out.More {
start = out.Start
goto more
}
return
}
// Upload reader to path (must be less than 150MB).
func (c *Client) Upload(path string, r io.Reader) error {
_, err := c.Files.Upload(&dropbox.UploadInput{
Mode: dropbox.WriteModeOverwrite,
Path: path,
Reader: r,
Mute: true,
})
return err
}
const defaultChunkSize = 125e6
// UploadSessionInput request input.
type UploadSessionOptions struct {
//When Size is known, UploadSession can prevent a superfluous request.
//If not provided, Commit.Reader will be checked for a Size() int64 method.
Size int64
//ChunkSize is the number of bytes to upload in each call to append (defaults to 125MB).
ChunkSize int64
//Commit information for uploaded file
Commit dropbox.UploadInput
}
// Upload reader to path (must be larger than 150MB).
// Uses an internally managed upload session.
// The input reader will be split into 125MB chunks.
func (c *Client) UploadSession(path string, r io.Reader) error {
_, err := c.UploadSessionOptions(UploadSessionOptions{
ChunkSize: defaultChunkSize,
Commit: dropbox.UploadInput{
Path: path,
ClientModified: time.Now(),
Mode: dropbox.WriteModeOverwrite,
Reader: r,
Mute: true,
},
})
return err
}
// Upload reader to path (must be larger than 150MB) and specify session options.
func (c *Client) UploadSessionOptions(opts UploadSessionOptions) (info os.FileInfo, err error) {
//find size and chunk size
if s, ok := opts.Commit.Reader.(interface {
Size() int64
}); ok && opts.Size == 0 {
opts.Size = s.Size()
}
if opts.ChunkSize == 0 {
opts.ChunkSize = defaultChunkSize
} else if opts.ChunkSize > 150e6 {
opts.ChunkSize = 150e6 //cap at 150MB
}
//upload session not required, use regular upload
if opts.Size > 0 && opts.ChunkSize > opts.Size && opts.Size < 150e6 {
out, err := c.Files.Upload(&opts.Commit)
if err != nil {
return nil, err
}
return &FileInfo{&out.Metadata}, nil
}
//prepare chunk-sized-reader
lr := &io.LimitedReader{R: opts.Commit.Reader, N: opts.ChunkSize}
//start
start, err := c.Files.UploadSessionStart(&dropbox.UploadSessionStartInput{Reader: lr})
if err != nil {
return nil, err
}
//initialise cursor with id and offset
curs := start.UploadSessionCursor
curs.Offset = opts.ChunkSize - lr.N
//while the limited reader has reached its limit
for lr.N == 0 {
//reset
lr.N = opts.ChunkSize
//upload more
if err = c.Files.UploadSessionAppend(&dropbox.UploadSessionAppendInput{
Cursor: curs,
Reader: lr,
}); err != nil {
return nil, err
}
curs.Offset += opts.ChunkSize - lr.N
//final chunk? save one call to append
if opts.Size > 0 && curs.Offset+opts.ChunkSize > opts.Size {
break
}
}
//finish (commit.Reader will either have 0 or less than chunkSize bytes remaining)
fin, err := c.Files.UploadSessionFinish(&dropbox.UploadSessionFinishInput{
Cursor: curs,
Commit: opts.Commit,
})
if err != nil {
return nil, err
}
return &FileInfo{&fin.UploadOutput.Metadata}, nil
}