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 158
Expand file tree
/
Copy pathFiles.Sync.cs
More file actions
398 lines (326 loc) · 14.2 KB
/
Copy pathFiles.Sync.cs
File metadata and controls
398 lines (326 loc) · 14.2 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
#if !WINDOWS_PHONE
using System;
using System.Collections.Generic;
using System.IO;
using DropNet.Models;
using DropNet.Authenticators;
using System.Net;
using DropNet.Exceptions;
using RestSharp;
using RestSharp.Deserializers;
namespace DropNet
{
public partial class DropNetClient
{
public MetaData GetMetaData(String hash = null, Boolean list = false, Boolean include_deleted = false)
{
return GetMetaData(String.Empty, hash, list, include_deleted);
}
public MetaData GetMetaData(String path, String hash = null, Boolean list = false, Boolean include_deleted = false)
{
if (path != "" && !path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateMetadataRequest(path, Root, hash, list, include_deleted);
return Execute<MetaData>(ApiType.Base, request);
}
public List<MetaData> GetVersions(string path, int limit)
{
var request = _requestHelper.CreateVersionsRequest(path, Root, limit);
return Execute<List<MetaData>>(ApiType.Base, request);
}
public MetaData Restore(string rev, string path)
{
var request = _requestHelper.CreateRestoreRequest(rev, path, Root);
return Execute<MetaData>(ApiType.Base, request);
}
public List<MetaData> Search(string searchString)
{
return Search(searchString, string.Empty);
}
public List<MetaData> Search(string searchString, int fileLimit)
{
return Search(searchString, string.Empty, fileLimit);
}
public List<MetaData> Search(string searchString, string path, int fileLimit = 1000)
{
if (fileLimit > 1000)
fileLimit = 1000;
var request = _requestHelper.CreateSearchRequest(searchString, path, Root, fileLimit);
return Execute<List<MetaData>>(ApiType.Base, request);
}
//TODO - Make class for this to return (instead of just a byte[])
public byte[] GetFile(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateGetFileRequest(path, Root);
var response = Execute(ApiType.Content, request);
return response.RawBytes;
}
//TODO - Make class for this to return (instead of just a byte[])
public byte[] GetFile(string path, long startByte, long endByte, string rev)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateGetFileRequest(path, Root, startByte, endByte, rev);
var response = Execute(ApiType.Content, request);
return response.RawBytes;
}
public byte[] GetFileContentFromFS(FileInfo localFile)
{
//Get the file stream
byte[] bytes;
using (var fs = new FileStream(localFile.FullName, FileMode.Open, FileAccess.Read))
{
using (var br = new BinaryReader(fs))
{
long numBytes = localFile.Length;
bytes = br.ReadBytes((int)numBytes);
}
fs.Close();
}
return bytes;
}
public MetaData UploadFilePUT(string path, string filename, byte[] fileData, bool overwrite = true, string parentRevision = null)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateUploadFilePutRequest(path, filename, fileData, Root, overwrite, parentRevision);
var response = _restClientContent.Execute<MetaData>(request);
//TODO - Return something better here?
return response.Data;
}
public MetaData UploadFile(string path, string filename, byte[] fileData, bool overwrite = true, string parentRevision = null)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateUploadFileRequest(path, filename, fileData, Root, overwrite, parentRevision);
var response = _restClientContent.Execute<MetaData>(request);
//TODO - Return something better here?
return response.Data;
}
public MetaData UploadFile(string path, string filename, Stream stream, bool overwrite = true, string parentRevision = null)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateUploadFileRequest(path, filename, stream, Root, overwrite, parentRevision);
var response = _restClientContent.Execute<MetaData>(request);
//TODO - Return something better here?
return response.Data;
}
public ChunkedUpload StartChunkedUpload(byte[] fileData)
{
var request = _requestHelper.CreateChunkedUploadRequest(fileData);
var response = _restClientContent.Execute<ChunkedUpload>(request);
return response.Data;
}
public ChunkedUpload AppendChunkedUpload(ChunkedUpload upload, byte[] fileData)
{
var request = _requestHelper.CreateAppendChunkedUploadRequest(upload, fileData);
var response = _restClientContent.Execute<ChunkedUpload>(request);
return response.Data;
}
public MetaData CommitChunkedUpload(ChunkedUpload upload, string path, bool overwrite = true, string parentRevision = null)
{
var request = _requestHelper.CreateCommitChunkedUploadRequest(upload, path, Root, overwrite, parentRevision);
var response = _restClientContent.Execute<MetaData>(request);
return response.Data;
}
public MetaData Delete(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateDeleteFileRequest(path, Root);
return Execute<MetaData>(ApiType.Base, request);
}
public MetaData Copy(string fromPath, string toPath)
{
if (!fromPath.StartsWith("/"))
{
fromPath = "/" + fromPath;
}
if (!toPath.StartsWith("/"))
{
toPath = "/" + toPath;
}
var request = _requestHelper.CreateCopyFileRequest(fromPath, toPath, Root);
return Execute<MetaData>(ApiType.Base, request);
}
public MetaData CopyFromCopyRef(string fromCopyRef, string toPath)
{
if (!toPath.StartsWith("/"))
{
toPath = "/" + toPath;
}
var request = _requestHelper.CreateCopyFileFromCopyRefRequest(fromCopyRef, toPath, Root);
return Execute<MetaData>(ApiType.Base, request);
}
public MetaData Move(string fromPath, string toPath)
{
if (!fromPath.StartsWith("/"))
{
fromPath = "/" + fromPath;
}
if (!toPath.StartsWith("/"))
{
toPath = "/" + toPath;
}
var request = _requestHelper.CreateMoveFileRequest(fromPath, toPath, Root);
return Execute<MetaData>(ApiType.Base, request);
}
public MetaData CreateFolder(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateCreateFolderRequest(path, Root);
return Execute<MetaData>(ApiType.Base, request);
}
public ShareResponse GetShare(string path, bool shortUrl = true)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateShareRequest(path, Root, shortUrl);
return Execute<ShareResponse>(ApiType.Base, request);
}
public ShareResponse GetMedia(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateMediaRequest(path, Root);
return Execute<ShareResponse>(ApiType.Base, request);
}
public byte[] GetThumbnail(MetaData file)
{
return GetThumbnail(file.Path, ThumbnailSize.Small);
}
public byte[] GetThumbnail(MetaData file, ThumbnailSize size)
{
return GetThumbnail(file.Path, size);
}
public byte[] GetThumbnail(string path)
{
return GetThumbnail(path, ThumbnailSize.Small);
}
public byte[] GetThumbnail(string path, ThumbnailSize size)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateThumbnailRequest(path, size, Root);
var response = Execute(ApiType.Content, request);
return response.RawBytes;
}
public CopyRefResponse GetCopyRef(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateCopyRefRequest(path, Root);
return Execute<CopyRefResponse>(ApiType.Base, request);
}
/// <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 LongpollDeltaResult GetLongpollDelta(string cursor, int timeout = 30)
{
var request = _requestHelper.CreateLongpollDeltaRequest(cursor, timeout);
return Execute<LongpollDeltaResult>(ApiType.Notify, request);
}
/// <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 DeltaPage GetDelta(string cursor)
{
var request = _requestHelper.CreateDeltaRequest(cursor, null, null, false);
var deltaResponse = Execute<DeltaPageInternal>(ApiType.Base, request);
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(StringListToDeltaEntry(stringList));
}
return deltaPage;
}
/// <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="pathPrefix">If present, this parameter filters the response to only include entries at or under the specified path</param>
/// <param name="locale">If present the metadata returned will have its size field translated based on the given locale</param>
/// <param name="includeMediaInfo">If true, each file will include a photo_info dictionary for photos and a video_info dictionary for videos with additional media info. When include_media_info is specified, files will only appear in delta responses when the media info is ready. If you use the include_media_info parameter, you must continue to pass the same value on subsequent calls using the returned cursor.</param>
/// <returns></returns>
public DeltaPage GetDelta(string cursor, string pathPrefix, string locale, bool includeMediaInfo)
{
if (!pathPrefix.StartsWith("/")) pathPrefix = "/" + pathPrefix;
var request = _requestHelper.CreateDeltaRequest(cursor, pathPrefix, locale, includeMediaInfo);
var deltaResponse = Execute<DeltaPageInternal>(ApiType.Base, request);
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(StringListToDeltaEntry(stringList));
}
return deltaPage;
}
private DeltaEntry StringListToDeltaEntry(List<string> stringList)
{
var deltaEntry = new DeltaEntry
{
Path = stringList[0]
};
if (!String.IsNullOrEmpty(stringList[1]))
{
var jsonDeserializer = new JsonDeserializer();
var fakeresponse = new RestResponse
{
Content = stringList[1]
};
deltaEntry.MetaData = jsonDeserializer.Deserialize<MetaData>(fakeresponse);
}
return deltaEntry;
}
private class DeltaPageInternal
{
public string Cursor { get; set; }
public bool Has_More { get; set; }
public bool Reset { get; set; }
public List<List<string>> Entries { get; set; }
}
}
}
#endif