forked from jpillora/go-dropbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.go
558 lines (466 loc) · 13.8 KB
/
files.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package dropbox
import (
"encoding/json"
"io"
"time"
)
// Files client for files and folders.
type Files struct {
*Client
}
// NewFiles client.
func NewFiles(config *Config) *Files {
return &Files{
Client: &Client{
Config: config,
},
}
}
// WriteMode determines what to do if the file already exists.
type WriteMode string
// Supported write modes.
const (
WriteModeAdd WriteMode = "add"
WriteModeOverwrite = "overwrite"
)
// Dimensions specifies the dimensions of a photo or video.
type Dimensions struct {
Width uint64 `json:"width"`
Height uint64 `json:"height"`
}
// GPSCoordinates specifies the GPS coordinate of a photo or video.
type GPSCoordinates struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
// PhotoMetadata specifies metadata for a photo.
type PhotoMetadata struct {
Dimensions *Dimensions `json:"dimensions,omitempty"`
Location *GPSCoordinates `json:"location,omitempty"`
TimeTaken time.Time `json:"time_taken,omitempty"`
}
// VideoMetadata specifies metadata for a video.
type VideoMetadata struct {
Dimensions *Dimensions `json:"dimensions,omitempty"`
Location *GPSCoordinates `json:"location,omitempty"`
TimeTaken time.Time `json:"time_taken,omitempty"`
Duration uint64 `json:"duration,omitempty"`
}
// MediaMetadata provides metadata for a photo or video.
type MediaMetadata struct {
Photo *PhotoMetadata `json:"photo,omitempty"`
Video *VideoMetadata `json:"video,omitempty"`
}
// MediaInfo provides additional information for a photo or video file.
type MediaInfo struct {
Pending bool `json:"pending"`
Metadata *MediaMetadata `json:"metadata,omitempty"`
}
// FileSharingInfo for a file which is contained in a shared folder.
type FileSharingInfo struct {
ReadOnly bool `json:"read_only"`
ParentSharedFolderID string `json:"parent_shared_folder_id"`
ModifiedBy string `json:"modified_by,omitempty"`
}
// Metadata for a file or folder.
type Metadata struct {
Tag string `json:".tag"`
Name string `json:"name"`
PathLower string `json:"path_lower"`
PathDisplay string `json:"path_display"`
ClientModified time.Time `json:"client_modified"`
ServerModified time.Time `json:"server_modified"`
Rev string `json:"rev"`
Size uint64 `json:"size"`
ID string `json:"id"`
MediaInfo *MediaInfo `json:"media_info,omitempty"`
SharingInfo *FileSharingInfo `json:"sharing_info,omitempty"`
}
// GetMetadataInput request input.
type GetMetadataInput struct {
Path string `json:"path"`
IncludeMediaInfo bool `json:"include_media_info"`
}
// GetMetadataOutput request output.
type GetMetadataOutput struct {
Metadata
}
// GetMetadata returns the metadata for a file or folder.
func (c *Files) GetMetadata(in *GetMetadataInput) (out *GetMetadataOutput, err error) {
body, err := c.call("/files/get_metadata", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// CreateFolderInput request input.
type CreateFolderInput struct {
Path string `json:"path"`
}
// CreateFolderOutput request output.
type CreateFolderOutput struct {
Name string `json:"name"`
PathLower string `json:"path_lower"`
ID string `json:"id"`
}
// CreateFolder creates a folder.
func (c *Files) CreateFolder(in *CreateFolderInput) (out *CreateFolderOutput, err error) {
body, err := c.call("/files/create_folder", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// DeleteInput request input.
type DeleteInput struct {
Path string `json:"path"`
}
// DeleteOutput request output.
type DeleteOutput struct {
Metadata
}
// Delete a file or folder and its contents.
func (c *Files) Delete(in *DeleteInput) (out *DeleteOutput, err error) {
body, err := c.call("/files/delete", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// PermanentlyDeleteInput request input.
type PermanentlyDeleteInput struct {
Path string `json:"path"`
}
// PermanentlyDelete a file or folder and its contents.
func (c *Files) PermanentlyDelete(in *PermanentlyDeleteInput) (err error) {
body, err := c.call("/files/permanently_delete", in)
if err != nil {
return
}
defer body.Close()
return
}
// CopyInput request input.
type CopyInput struct {
FromPath string `json:"from_path"`
ToPath string `json:"to_path"`
}
// CopyOutput request output.
type CopyOutput struct {
Metadata
}
// Copy a file or folder to a different location.
func (c *Files) Copy(in *CopyInput) (out *CopyOutput, err error) {
body, err := c.call("/files/copy", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// MoveInput request input.
type MoveInput struct {
FromPath string `json:"from_path"`
ToPath string `json:"to_path"`
}
// MoveOutput request output.
type MoveOutput struct {
Metadata
}
// Move a file or folder to a different location.
func (c *Files) Move(in *MoveInput) (out *MoveOutput, err error) {
body, err := c.call("/files/move", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// RestoreInput request input.
type RestoreInput struct {
Path string `json:"path"`
Rev string `json:"rev"`
}
// RestoreOutput request output.
type RestoreOutput struct {
Metadata
}
// Restore a file to a specific revision.
func (c *Files) Restore(in *RestoreInput) (out *RestoreOutput, err error) {
body, err := c.call("/files/restore", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// ListFolderInput request input.
type ListFolderInput struct {
Path string `json:"path"`
Recursive bool `json:"recursive"`
IncludeMediaInfo bool `json:"include_media_info"`
IncludeDeleted bool `json:"include_deleted"`
}
// ListFolderOutput request output.
type ListFolderOutput struct {
Cursor string `json:"cursor"`
HasMore bool `json:"has_more"`
Entries []*Metadata
}
// ListFolder returns the metadata for a file or folder.
func (c *Files) ListFolder(in *ListFolderInput) (out *ListFolderOutput, err error) {
in.Path = normalizePath(in.Path)
body, err := c.call("/files/list_folder", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// ListFolderContinueInput request input.
type ListFolderContinueInput struct {
Cursor string `json:"cursor"`
}
// ListFolderContinue pagenates using the cursor from ListFolder.
func (c *Files) ListFolderContinue(in *ListFolderContinueInput) (out *ListFolderOutput, err error) {
body, err := c.call("/files/list_folder/continue", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// SearchMode determines how a search is performed.
type SearchMode string
// Supported search modes.
const (
SearchModeFilename SearchMode = "filename"
SearchModeFilenameAndContent = "filename_and_content"
SearchModeDeletedFilename = "deleted_filename"
)
// SearchMatchType represents the type of match made.
type SearchMatchType string
// Supported search match types.
const (
SearchMatchFilename SearchMatchType = "filename"
SearchMatchContent = "content"
SearchMatchBoth = "both"
)
// SearchMatch represents a matched file or folder.
type SearchMatch struct {
MatchType struct {
Tag SearchMatchType `json:".tag"`
} `json:"match_type"`
Metadata *Metadata `json:"metadata"`
}
// SearchInput request input.
type SearchInput struct {
Path string `json:"path"`
Query string `json:"query"`
Start uint64 `json:"start,omitempty"`
MaxResults uint64 `json:"max_results,omitempty"`
Mode SearchMode `json:"mode"`
}
// SearchOutput request output.
type SearchOutput struct {
Matches []*SearchMatch `json:"matches"`
More bool `json:"more"`
Start uint64 `json:"start"`
}
// Search for files and folders.
func (c *Files) Search(in *SearchInput) (out *SearchOutput, err error) {
in.Path = normalizePath(in.Path)
if in.Mode == "" {
in.Mode = SearchModeFilename
}
body, err := c.call("/files/search", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// UploadInput request input.
type UploadInput struct {
Path string `json:"path"`
Mode WriteMode `json:"mode"`
AutoRename bool `json:"autorename"`
Mute bool `json:"mute"`
ClientModified time.Time `json:"client_modified,omitempty"`
Reader io.Reader `json:"-"`
}
// UploadOutput request output.
type UploadOutput struct {
Metadata
}
// Upload a file smaller than 150MB.
func (c *Files) Upload(in *UploadInput) (out *UploadOutput, err error) {
err = c.decodeContent("/files/upload", in, in.Reader, &out)
return
}
// UploadSessionCursor
type UploadSessionCursor struct {
ID string `json:"session_id"`
Offset int64 `json:"offset"`
}
// UploadSessionStartInput request input.
type UploadSessionStartInput struct {
Close bool `json:"close"`
Reader io.Reader `json:"-"`
}
// UploadSessionStartOutput request output.
type UploadSessionStartOutput struct {
UploadSessionCursor
}
// New upload session exposing the /files/upload_session API semantics.
func (c *Files) UploadSessionStart(in *UploadSessionStartInput) (out *UploadSessionStartOutput, err error) {
err = c.decodeContent("/files/upload_session/start", in, in.Reader, &out)
return
}
// UploadSessionAppendInput request input.
type UploadSessionAppendInput struct {
Cursor UploadSessionCursor `json:"cursor"`
Close bool `json:"close"`
Reader io.Reader `json:"-"`
}
// Append a chunk to the current session (smaller than 150MB).
func (c *Files) UploadSessionAppend(in *UploadSessionAppendInput) (err error) {
body, _, err := c.content("/files/upload_session/append_v2", in, in.Reader)
body.Close()
return
}
// UploadSessionFinishInput request input.
type UploadSessionFinishInput struct {
Cursor UploadSessionCursor `json:"cursor"`
Commit UploadInput `json:"commit"`
}
// UploadSessionFinishInput request output.
type UploadSessionFinishOutput struct {
UploadOutput
}
// Finish an upload session and provide the file commit information.
func (c *Files) UploadSessionFinish(in *UploadSessionFinishInput) (out *UploadSessionFinishOutput, err error) {
err = c.decodeContent("/files/upload_session/finish", in, in.Commit.Reader, &out)
return
}
// DownloadInput request input.
type DownloadInput struct {
Path string `json:"path"`
}
// DownloadOutput request output.
type DownloadOutput struct {
Body io.ReadCloser
Length int64
}
// Download a file.
func (c *Files) Download(in *DownloadInput) (out *DownloadOutput, err error) {
body, l, err := c.content("/files/download", in, nil)
if err != nil {
return
}
out = &DownloadOutput{body, l}
return
}
// ThumbnailFormat determines the format of the thumbnail.
type ThumbnailFormat string
const (
// GetThumbnailFormatJPEG specifies a JPG thumbnail
GetThumbnailFormatJPEG ThumbnailFormat = "jpeg"
// GetThumbnailFormatPNG specifies a PNG thumbnail
GetThumbnailFormatPNG = "png"
)
// ThumbnailFormat determines the size of the thumbnail.
type ThumbnailSize string
const (
// GetThumbnailSizeW32H32 specifies a size of 32 by 32 px
GetThumbnailSizeW32H32 ThumbnailSize = "w32h32"
// GetThumbnailSizeW64H64 specifies a size of 64 by 64 px
GetThumbnailSizeW64H64 = "w64h64"
// GetThumbnailSizeW128H128 specifies a size of 128 by 128 px
GetThumbnailSizeW128H128 = "w128h128"
// GetThumbnailSizeW640H480 specifies a size of 640 by 480 px
GetThumbnailSizeW640H480 = "w640h480"
// GetThumbnailSizeW1024H768 specifies a size of 1024 by 768 px
GetThumbnailSizeW1024H768 = "w1024h768"
)
// GetThumbnailInput request input.
type GetThumbnailInput struct {
Path string `json:"path"`
Format ThumbnailFormat `json:"format"`
Size ThumbnailSize `json:"size"`
}
// GetThumbnailOutput request output.
type GetThumbnailOutput struct {
Body io.ReadCloser
Length int64
}
// GetThumbnail a thumbnail for a file. Currently thumbnails are only generated for the
// files with the following extensions: png, jpeg, png, tiff, tif, gif and bmp.
func (c *Files) GetThumbnail(in *GetThumbnailInput) (out *GetThumbnailOutput, err error) {
body, l, err := c.content("/files/get_thumbnail", in, nil)
if err != nil {
return
}
out = &GetThumbnailOutput{body, l}
return
}
// GetPreviewInput request input.
type GetPreviewInput struct {
Path string `json:"path"`
}
// GetPreviewOutput request output.
type GetPreviewOutput struct {
Body io.ReadCloser
Length int64
}
// GetPreview a preview for a file. Currently previews are only generated for the
// files with the following extensions: .doc, .docx, .docm, .ppt, .pps, .ppsx,
// .ppsm, .pptx, .pptm, .xls, .xlsx, .xlsm, .rtf
func (c *Files) GetPreview(in *GetPreviewInput) (out *GetPreviewOutput, err error) {
body, l, err := c.content("/files/get_preview", in, nil)
if err != nil {
return
}
out = &GetPreviewOutput{body, l}
return
}
// ListRevisionsInput request input.
type ListRevisionsInput struct {
Path string `json:"path"`
Limit uint64 `json:"limit,omitempty"`
}
// ListRevisionsOutput request output.
type ListRevisionsOutput struct {
IsDeleted bool
Entries []*Metadata
}
// ListRevisions gets the revisions of the specified file.
func (c *Files) ListRevisions(in *ListRevisionsInput) (out *ListRevisionsOutput, err error) {
body, err := c.call("/files/list_revisions", in)
if err != nil {
return
}
defer body.Close()
err = json.NewDecoder(body).Decode(&out)
return
}
// Normalize path so people can use "/" as they expect.
func normalizePath(s string) string {
if s == "/" {
return ""
} else {
return s
}
}