-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropboxRepository.cs
67 lines (53 loc) · 1.87 KB
/
DropboxRepository.cs
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Dropbox.Api;
using System.Linq;
using Dropbox.Api.Files;
namespace DropboxExplorer
{
public class DropboxRepository : IStorageRepository
{
private string _authToken;
public void SetAuthToken(string authToken)
{
_authToken = authToken;
}
public async Task<IEnumerable<IStorageItem>> GetItemsAsync(string rootPath, bool recursive)
{
using(var client = CreateClient())
{
var results = new List<IStorageItem>();
var response = await client.Files.ListFolderAsync(rootPath, recursive, true);
results.AddRange(response.Entries.Select(ResultSelectorCallback));
while(response.HasMore)
{
response = await client.Files.ListFolderContinueAsync(response.Cursor);
results.AddRange(response.Entries.Select(ResultSelectorCallback));
}
return results;
}
}
private IStorageItem ResultSelectorCallback(Metadata metadata)
{
if (metadata.IsFile)
{
return new FileData(metadata.Name, metadata.AsFile.Size);
}
if (metadata.IsFolder)
{
return new FolderData(metadata.Name);
}
throw new Exception("Unhandled file type");
}
public async Task<ulong> GetFolderSizeAsync(string rootPath, bool recursive)
{
var items = await GetItemsAsync(rootPath, recursive);
return (ulong)items.Sum(x => (long)x.Size); //(todo) Casting like this can cause loss of data - change this
}
private DropboxClient CreateClient()
{
return new DropboxClient(_authToken);
}
}
}