This repository was archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathClient.Files.cs
More file actions
930 lines (786 loc) · 35.4 KB
/
Copy pathClient.Files.cs
File metadata and controls
930 lines (786 loc) · 35.4 KB
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
using System.Threading;
using DropNetRT.Exceptions;
using DropNetRT.Extensions;
using DropNetRT.HttpHelpers;
using DropNetRT.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace DropNetRT
{
public partial class DropNetClient
{
/// <summary>
/// Gets MetaData for a file or folder given the path
/// </summary>
/// <param name="path">Path to file or folder</param>
/// <returns><see cref="Metadata"/> for a file or folder</returns>
public async Task<Metadata> GetMetaData(string path, string hash = null)
{
return await GetMetaData(path, hash, null, true, false);
}
/// <summary>
/// Shorthand for a GetMetadata call without contents listing
/// </summary>
/// <param name="path">Path to file or folder</param>
/// <returns><see cref="Metadata"/> for a file or folder</returns>
public async Task<Metadata> GetMetaDataNoList(string path, string hash = null)
{
return await GetMetaData(path, hash, null, false, false);
}
/// <summary>
/// Shorthand for a GetMetadata call with deleted files
/// </summary>
/// <param name="path">Path to file or folder</param>
/// <returns><see cref="Metadata"/> for a file or folder</returns>
public async Task<Metadata> GetMetaDataWithDeleted(string path, string hash = null)
{
return await GetMetaData(path, hash, null, true, true);
}
/// <summary>
/// Gets MetaData for a file or Folder (All options)
/// </summary>
/// <param name="path">Path to file or folder</param>
/// <param name="hash">Each call to /metadata on a folder will return a hash field, generated by hashing all of the metadata contained in that response. On later calls to /metadata, you should provide that value via this parameter so that if nothing has changed, the response will be a 304 (Not Modified)</param>
/// <param name="rev">If you include a particular revision number, then only the metadata for that revision will be returned.</param>
/// <param name="list"> If true, the folder's metadata will include a contents field with a list of metadata entries for the contents of the folder. If false, the contents field will be omitted.</param>
/// <param name="includeDeleted">Only applicable when list is set. If this parameter is set to true, then contents will include the metadata of deleted children. Note that the target of the metadata call is always returned even when it has been deleted (with is_deleted set to true) regardless of this flag.</param>
/// <returns></returns>
public Task<Metadata> GetMetaData(string path, string hash, int? rev, bool list, bool includeDeleted)
{
return GetMetaData(path, hash, rev, list, includeDeleted, null, CancellationToken.None);
}
/// <summary>
/// Gets MetaData for a file or Folder (All options)
/// </summary>
/// <param name="path">Path to file or folder</param>
/// <param name="hash">Each call to /metadata on a folder will return a hash field, generated by hashing all of the metadata contained in that response. On later calls to /metadata, you should provide that value via this parameter so that if nothing has changed, the response will be a 304 (Not Modified)</param>
/// <param name="rev">If you include a particular revision number, then only the metadata for that revision will be returned.</param>
/// <param name="list"> If true, the folder's metadata will include a contents field with a list of metadata entries for the contents of the folder. If false, the contents field will be omitted.</param>
/// <param name="includeDeleted">Only applicable when list is set. If this parameter is set to true, then contents will include the metadata of deleted children. Note that the target of the metadata call is always returned even when it has been deleted (with is_deleted set to true) regardless of this flag.</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Metadata> GetMetaData(string path, string hash, int? rev, bool list, bool includeDeleted, bool? includeMembership, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString(string.Format("1/metadata/{0}/{1}", Root, path.CleanPath()), ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
if (!string.IsNullOrEmpty(hash))
{
request.Parameters.Add(new HttpParameter("hash", hash));
}
request.Parameters.Add(new HttpParameter("list", list));
request.Parameters.Add(new HttpParameter("include_deleted", includeDeleted));
if (rev.HasValue)
{
request.Parameters.Add(new HttpParameter("rev", rev));
}
if (includeMembership.HasValue)
{
request.Parameters.Add(new HttpParameter("include_membership", includeMembership.Value));
}
var response = await SendAsync<Metadata>(request, cancellationToken);
return response;
}
/// <summary>
/// Gets a share link from a give path
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public Task<ShareResponse> GetShare(string path)
{
return GetShare(path, CancellationToken.None);
}
/// <summary>
/// Gets a share link from a give path
/// </summary>
/// <param name="path"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<ShareResponse> GetShare(string path, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString(string.Format("1/shares/{0}/{1}", Root, path.CleanPath()), ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
var response = await SendAsync<ShareResponse>(request, cancellationToken);
return response;
}
/// <summary>
/// Gets a share link from a give path
/// </summary>
/// <param name="path"></param>
/// <param name="shortUrl"></param>
/// <returns></returns>
public Task<ShareResponse> GetShare(string path, bool shortUrl)
{
return GetShare(path, shortUrl, CancellationToken.None);
}
/// <summary>
/// Gets a share link from a give path
/// </summary>
/// <param name="path"></param>
/// <param name="shortUrl"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<ShareResponse> GetShare(string path, bool shortUrl, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString(string.Format("1/shares/{0}/{1}", Root, path.CleanPath()), ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
request.Parameters.Add(new HttpParameter("short_url", shortUrl));
var response = await SendAsync<ShareResponse>(request, cancellationToken);
return response;
}
/// <summary>
/// Searches for a given text in the entire dropbox/sandbox folder
/// </summary>
/// <param name="searchString"></param>
/// <returns></returns>
public async Task<List<Metadata>> Search(string searchString)
{
return await Search(searchString, string.Empty);
}
/// <summary>
/// Searches for a given text in the entire dropbox/sandbox folder
/// </summary>
/// <param name="searchString"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<List<Metadata>> Search(string searchString, CancellationToken cancellationToken)
{
return await Search(searchString, string.Empty, cancellationToken);
}
/// <summary>
/// Searches for a given text in a specified folder
/// </summary>
/// <param name="searchString"></param>
/// <param name="path"></param>
/// <returns></returns>
public Task<List<Metadata>> Search(string searchString, string path)
{
return Search(searchString, path, CancellationToken.None);
}
/// <summary>
/// Searches for a given text in a specified folder
/// </summary>
/// <param name="searchString"></param>
/// <param name="path"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<List<Metadata>> Search(string searchString, string path, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString(string.Format("1/search/{0}/{1}", Root, path.CleanPath()), ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
request.Parameters.Add(new HttpParameter("query", searchString));
var response = await SendAsync<List<Metadata>>(request, cancellationToken);
return response;
}
/// <summary>
/// Gets a file from the given path
/// </summary>
/// <param name="path">Path of the file in Dropbox to go</param>
/// <returns></returns>
public Task<byte[]> GetFile(string path)
{
return GetFile(path, CancellationToken.None);
}
/// <summary>
/// Gets a file from the given path
/// </summary>
/// <param name="path">Path of the file in Dropbox to go</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<byte[]> GetFile(string path, CancellationToken cancellationToken)
{
var request = MakeGetFileRequest(path);
var response = await _httpClient.SendAsync(request, cancellationToken);
//TODO - Error Handling
return await response.Content.ReadAsByteArrayAsync();
}
/// <summary>
/// Gets a file stream from the given path
/// </summary>
/// <param name="path">Path of the file in Dropbox to go</param>
/// <returns></returns>
public Task<Stream> GetFileStream(string path)
{
return GetFileStream(path, CancellationToken.None);
}
/// <summary>
/// Gets a file stream from the given path
/// </summary>
/// <param name="path">Path of the file in Dropbox to go</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Stream> GetFileStream(string path, CancellationToken cancellationToken)
{
var request = MakeGetFileRequest(path);
var response = await _httpClient.SendAsync(request, cancellationToken);
//TODO - Error Handling
if (HttpStatusCode.OK != response.StatusCode)
{
throw new DropboxException(response.StatusCode);
}
return await response.Content.ReadAsStreamAsync();
}
/// <summary>
/// Gets a file download uri with user authentication added (for use with Background Transfers)
/// </summary>
/// <param name="path">Path of the file in Dropbox to go</param>
/// <returns></returns>
public Uri GetFileUrl(string path)
{
var request = MakeGetFileRequest(path);
return request.RequestUri;
}
/// <summary>
/// Gets the upload Uri for a file (for use with Background Transfers)
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
/// <returns></returns>
public Uri UploadUrl(string path, string filename)
{
return UploadUrl(path, filename, null);
}
/// <summary>
/// Gets the upload Uri for a file (for use with Background Transfers)
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
/// <param name="parentRevision"></param>
/// <returns></returns>
public Uri UploadUrl(string path, string filename, string parentRevision)
{
var request = MakeUploadRequest(path, filename, parentRevision);
return request.RequestUri;
}
/// <summary>
/// Uploads a file to a Dropbox folder
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
/// <param name="fileData"></param>
/// <returns></returns>
public Task<Metadata> Upload(string path, string filename, byte[] fileData)
{
return Upload(path, filename, fileData, CancellationToken.None);
}
/// <summary>
/// Uploads a file to a Dropbox folder
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
/// <param name="fileData"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<Metadata> Upload(string path, string filename, byte[] fileData, CancellationToken cancellationToken)
{
return Upload(path, filename, fileData, null, cancellationToken);
}
/// <summary>
/// Uploads a file to a Dropbox folder
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
/// <param name="fileData"></param>
/// <param name="parentRevision"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Metadata> Upload(string path, string filename, byte[] fileData, string parentRevision, CancellationToken cancellationToken)
{
var request = MakeUploadRequest(path, filename, parentRevision);
var content = new MultipartFormDataContent(_formBoundary);
foreach (var parm in request.Parameters)
{
content.Add(new StringContent(parm.Value.ToString()), parm.Name);
}
var fileContent = new ByteArrayContent(fileData);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("file")
{
FileName = filename,
Name = "file"
};
fileContent.Headers.Add("Content-Type", "application/octet-stream");
content.Add(fileContent);
HttpResponseMessage response;
try
{
response = await _httpClient.PostAsync(request.RequestUri, content, cancellationToken);
}
catch (Exception ex)
{
throw new DropboxException(ex);
}
//TODO - More Error Handling
if (response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}
string responseBody = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Metadata>(responseBody);
}
/// <summary>
/// Uploads a streamed data to a Dropbox folder
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
/// <param name="stream"></param>
/// <returns></returns>
public Task<Metadata> Upload(string path, string filename, Stream stream)
{
return Upload(path, filename, stream, CancellationToken.None);
}
/// <summary>
/// Uploads a streamed data to a Dropbox folder
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
/// <param name="stream"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Metadata> Upload(string path, string filename, Stream stream, CancellationToken cancellationToken)
{
return await Upload(path, filename, stream, null, cancellationToken);
}
/// <summary>
/// Uploads a streamed data to a Dropbox folder
/// </summary>
/// <param name="path"></param>
/// <param name="filename"></param>
/// <param name="stream"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Metadata> Upload(string path, string filename, Stream stream, string parentRevision, CancellationToken cancellationToken)
{
var request = MakeUploadPutRequest(path, filename, parentRevision);
var content = new StreamContent(stream);
HttpResponseMessage response;
try
{
response = await _httpClient.PutAsync(request.RequestUri, content, cancellationToken);
}
catch (Exception ex)
{
throw new DropboxException(ex);
}
//TODO - More Error Handling
if (response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}
var responseBody = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Metadata>(responseBody);
}
private const int ChunkSize = 1024 * 1024;
private async Task<ChunkedUploadResponse> UploadChunk(Stream stream, ChunkedUploadResponse lastResponse, CancellationToken cancellationToken)
{
return await UploadChunk(stream, ChunkSize, lastResponse, cancellationToken);
}
public async Task<ChunkedUploadResponse> UploadChunk(Stream stream, int chunkSize, ChunkedUploadResponse lastResponse, CancellationToken cancellationToken)
{
if(lastResponse == null)
{
throw new ArgumentNullException("lastResponse");
}
var offset = lastResponse.Offset;
var uploadId = lastResponse.UploadId;
var request = MakeChunkedUploadPutRequest(offset, uploadId);
var buffer = new byte[chunkSize];
stream.Seek(lastResponse.Offset, SeekOrigin.Begin);
var contentSize = stream.Read(buffer, 0, chunkSize);
if(contentSize == 0)
{
// Nothing left to send
return null;
}
HttpContent content = new ByteArrayContent(buffer, 0, contentSize);
try
{
var response = await _httpClient.PutAsync(request.RequestUri, content, cancellationToken);
if (response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}
var body = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ChunkedUploadResponse>(body);
}
catch (AggregateException)
{
throw;
}
catch (DropboxException)
{
throw;
}
catch (Exception ex)
{
throw new DropboxException(ex);
}
}
private async Task<ChunkedUploadResponse> StartChunkedUpload(Stream stream, CancellationToken cancellationToken)
{
return await StartChunkedUpload(stream, ChunkSize, cancellationToken);
}
public async Task<ChunkedUploadResponse> StartChunkedUpload(Stream stream, int chunkSize, CancellationToken cancellationToken)
{
var request = MakeChunkedUploadPutRequest(0);
var buffer = new byte[chunkSize];
var contentSize = stream.Read(buffer, 0, chunkSize);
HttpContent content = new ByteArrayContent(buffer, 0, contentSize);
try
{
var response = await _httpClient.PutAsync(request.RequestUri, content, cancellationToken);
if(response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}
var body = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ChunkedUploadResponse>(body);
}
catch (AggregateException)
{
throw;
}
catch(DropboxException)
{
throw;
}
catch(Exception ex)
{
throw new DropboxException(ex);
}
}
public async Task<Metadata> UploadChunked(string path, string filename, Stream stream, CancellationToken cancellationToken, IProgress<long> progress = null)
{
try
{
// Upload the initial chunk so we can get the upload_id value
var chunkedUploadResponse = await StartChunkedUpload(stream, cancellationToken);
if(chunkedUploadResponse == null)
{
throw new Exception("Initial chunk upload response was null.");
}
var uploadId = chunkedUploadResponse.UploadId;
if(progress != null)
{
progress.Report(chunkedUploadResponse.Offset);
}
// Keep uploading subsequent chunks until we don't have anything left to upload
while(chunkedUploadResponse != null)
{
chunkedUploadResponse = await UploadChunk(stream, chunkedUploadResponse, cancellationToken);
if(progress != null && chunkedUploadResponse != null)
{
progress.Report(chunkedUploadResponse.Offset);
}
}
// Commit the upload
return await CommitChunkedUpload(path, filename, null, uploadId, cancellationToken);
}
catch(AggregateException)
{
throw;
}
catch(DropboxException)
{
throw;
}
catch(Exception ex)
{
throw new DropboxException(ex);
}
}
public async Task<Metadata> CommitChunkedUpload(string path, string filename, string parentRevision, string uploadId, CancellationToken cancellationToken)
{
try
{
var commitRequest = MakeChunkedUploadCommitRequest(path, filename, parentRevision, uploadId);
var response = await _httpClient.PostAsync(commitRequest.RequestUri, null, cancellationToken);
if (response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}
var responseBody = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Metadata>(responseBody);
}
catch (AggregateException)
{
throw;
}
catch (DropboxException)
{
throw;
}
catch (Exception ex)
{
throw new DropboxException(ex);
}
}
/// <summary>
/// Deletes the file or folder from dropbox with the given path
/// </summary>
/// <param name="path">The Path of the file or folder to delete.</param>
/// <returns></returns>
public Task<Metadata> Delete(string path)
{
return Delete(path, CancellationToken.None);
}
/// <summary>
/// Deletes the file or folder from dropbox with the given path
/// </summary>
/// <param name="path">The Path of the file or folder to delete.</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Metadata> Delete(string path, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString("1/fileops/delete", ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
request.AddParameter("path", path);
request.AddParameter("root", Root);
var response = await SendAsync<Metadata>(request, cancellationToken);
return response;
}
/// <summary>
/// Copies a file or folder on Dropbox
/// </summary>
/// <param name="fromPath"></param>
/// <param name="toPath"></param>
/// <returns></returns>
public Task<Metadata> Copy(string fromPath, string toPath)
{
return Copy(fromPath, toPath, CancellationToken.None);
}
/// <summary>
/// Copies a file or folder on Dropbox
/// </summary>
/// <param name="fromPath"></param>
/// <param name="toPath"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Metadata> Copy(string fromPath, string toPath, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString("1/fileops/copy", ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
request.AddParameter("from_path", fromPath);
request.AddParameter("to_path", toPath);
request.AddParameter("root", Root);
var response = await SendAsync<Metadata>(request, cancellationToken);
return response;
}
/// <summary>
/// Moves a file or folder on Dropbox
/// </summary>
/// <param name="fromPath"></param>
/// <param name="toPath"></param>
/// <returns></returns>
public Task<Metadata> Move(string fromPath, string toPath)
{
return Move(fromPath, toPath, CancellationToken.None);
}
/// <summary>
/// Moves a file or folder on Dropbox
/// </summary>
/// <param name="fromPath"></param>
/// <param name="toPath"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Metadata> Move(string fromPath, string toPath, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString("1/fileops/move", ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
request.AddParameter("from_path", fromPath);
request.AddParameter("to_path", toPath);
request.AddParameter("root", Root);
var response = await SendAsync<Metadata>(request, cancellationToken);
return response;
}
/// <summary>
/// Created a new folder with the given path
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public Task<Metadata> CreateFolder(string path)
{
return CreateFolder(path, CancellationToken.None);
}
/// <summary>
/// Created a new folder with the given path
/// </summary>
/// <param name="path"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Metadata> CreateFolder(string path, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString("1/fileops/create_folder", ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
request.AddParameter("path", path);
request.AddParameter("root", Root);
var response = await SendAsync<Metadata>(request, cancellationToken);
return response;
}
/// <summary>
/// Returns a link directly to a file.
/// Similar to /shares. The difference is that this bypasses the Dropbox webserver, used to provide a preview of the file, so that you can effectively stream the contents of your media.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public Task<ShareResponse> GetMedia(string path)
{
return GetMedia(path, CancellationToken.None);
}
/// <summary>
/// Returns a link directly to a file.
/// Similar to /shares. The difference is that this bypasses the Dropbox webserver, used to provide a preview of the file, so that you can effectively stream the contents of your media.
/// </summary>
/// <param name="path"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<ShareResponse> GetMedia(string path, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString(string.Format("1/media/{0}/{1}", Root, path.CleanPath()), ApiType.Base);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
var response = await SendAsync<ShareResponse>(request, cancellationToken);
return response;
}
/// <summary>
/// Gets the thumbnail of an image given its MetaData (default size = small)
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public async Task<byte[]> GetThumbnail(Metadata file)
{
return await GetThumbnail(file.Path, ThumbnailSize.Small);
}
/// <summary>
/// Gets the thumbnail of an image given its MetaData
/// </summary>
/// <param name="file"></param>
/// <param name="size"></param>
/// <returns></returns>
public async Task<byte[]> GetThumbnail(Metadata file, ThumbnailSize size)
{
return await GetThumbnail(file.Path, size);
}
/// <summary>
/// Gets the thumbnail of an image given its path (default size = small)
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public async Task<byte[]> GetThumbnail(string path)
{
return await GetThumbnail(path, ThumbnailSize.Small);
}
/// <summary>
/// Gets the thumbnail of an image given its path
/// </summary>
/// <param name="path"></param>
/// <param name="size"></param>
/// <returns></returns>
public Task<byte[]> GetThumbnail(string path, ThumbnailSize size)
{
return GetThumbnail(path, size, CancellationToken.None);
}
/// <summary>
/// Gets the thumbnail of an image given its path
/// </summary>
/// <param name="path"></param>
/// <param name="size"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<byte[]> GetThumbnail(string path, ThumbnailSize size, CancellationToken cancellationToken)
{
var request = MakeThumbnailRequest(path, size);
var response = await _httpClient.SendAsync(request, cancellationToken);
return await response.Content.ReadAsByteArrayAsync();
}
/// <summary>
/// Gets a url for the thumbnail of an image (default size = small)
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public Uri GetThumbnailUrl(string path)
{
var request = MakeThumbnailRequest(path, ThumbnailSize.Small);
return request.RequestUri;
}
/// <summary>
/// Gets a url for the thumbnail of an image
/// </summary>
/// <param name="path"></param>
/// <param name="size"></param>
/// <returns></returns>
public Uri GetThumbnailUrl(string path, ThumbnailSize size)
{
var request = MakeThumbnailRequest(path, size);
return request.RequestUri;
}
/// <summary>
/// Gets the deltas for a user's folders and files.
/// </summary>
/// <param name="cursor">The value returned from the prior call to GetDelta or an empty string</param>
/// <returns></returns>
public Task<DeltaPage> GetDelta(string cursor)
{
return GetDelta(cursor, CancellationToken.None);
}
/// <summary>
/// Gets the deltas for a user's folders and files.
/// </summary>
/// <param name="cursor">The value returned from the prior call to GetDelta or an empty string</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<DeltaPage> GetDelta(string cursor, CancellationToken cancellationToken)
{
var requestUrl = MakeRequestString("1/delta", ApiType.Base);
var request = new HttpRequest(HttpMethod.Post, requestUrl);
request.AddParameter("cursor", cursor);
var deltaResponse = await SendAsync<DeltaPageInternal>(request, cancellationToken);
var deltaPage = new DeltaPage
{
Cursor = deltaResponse.Cursor,
Has_More = deltaResponse.Has_More,
Reset = deltaResponse.Reset,
Entries = new List<DeltaEntry>()
};
foreach (var stringList in deltaResponse.Entries)
{
deltaPage.Entries.Add(JRawListToDeltaEntry(stringList));
}
return deltaPage;
}
/// <summary>
/// A long-poll endpoint to wait for changes on an account. In conjunction with /delta, this call gives you a low-latency way to monitor an account for file changes.
/// </summary>
/// <param name="cursor">The value returned from the prior call to GetDelta.</param>
/// <param name="timeout">An optional integer indicating a timeout, in seconds.
/// The default value is 30 seconds, which is also the minimum allowed value. The maximum is 480 seconds.</param>
/// <returns></returns>
public Task<LongpollDeltaResult> GetLongpollDelta(string cursor, int timeout = 30)
{
return GetLongpollDelta(cursor, CancellationToken.None, timeout);
}
/// <summary>
/// A long-poll endpoint to wait for changes on an account. In conjunction with /delta, this call gives you a low-latency way to monitor an account for file changes.
/// </summary>
/// <param name="cursor">The value returned from the prior call to GetDelta.</param>
/// <param name="cancellationToken"></param>
/// <param name="timeout">An optional integer indicating a timeout, in seconds.
/// The default value is 30 seconds, which is also the minimum allowed value. The maximum is 480 seconds.</param>
/// <returns></returns>
public async Task<LongpollDeltaResult> GetLongpollDelta(string cursor, CancellationToken cancellationToken, int timeout = 30)
{
var requestUrl = MakeRequestString("1/longpoll_delta", ApiType.Notify);
var request = new HttpRequest(HttpMethod.Get, requestUrl);
request.AddParameter("cursor", cursor);
if (timeout < 30)
timeout = 30;
if (timeout > 480)
timeout = 480;
request.AddParameter("timeout", timeout);
return await SendAsync<LongpollDeltaResult>(request, cancellationToken);
}
}
}