Skip to content
Navigation Menu
{{ message }}
forked from DropNet/DropNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiles.Async.cs
More file actions
357 lines (304 loc) · 15.8 KB
/
Copy pathFiles.Async.cs
File metadata and controls
357 lines (304 loc) · 15.8 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
using System.Collections.Generic;
using System.IO;
using DropNet.Models;
using RestSharp;
using System;
using DropNet.Exceptions;
using RestSharp.Deserializers;
namespace DropNet
{
public partial class DropNetClient
{
/// <summary>
/// Gets MetaData for a File or Folder. For a folder this includes its contents. For a file, this includes details such as file size.
/// </summary>
/// <param name="path">The path of the file or folder</param>
/// <param name="success">Success call back</param>
/// <param name="failure">Failure call back </param>
public void GetMetaDataAsync(string path, Action<MetaData> success, Action<DropboxException> failure)
{
if (!string.IsNullOrEmpty(path) && !path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateMetadataRequest(path, Root);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <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">The path</param>
/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void GetMediaAsync(string path, Action<ShareResponse> success, Action<DropboxException> failure)
{
if (!path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateMediaRequest(path, Root);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <summary>
/// Gets MetaData for a File or Folder. For a folder this includes its contents. For a file, this includes details such as file size.
/// Optional 'hash' param returns HTTP code 304 (Directory contents have not changed) if contents have not changed since the
/// hash was retrieved on a previous call.
/// </summary>
/// <param name="path">The path of the file or folder</param>
/// <param name="hash">hash - Optional. Listing return values include a hash representing the state of the directory's contents. If you provide this argument to the metadata call, you give the service an opportunity to respond with a "304 Not Modified" status code instead of a full (potentially very large) directory listing. This argument is ignored if the specified path is associated with a file or if list=false.</param>
/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void GetMetaDataAsync(string path, string hash, Action<MetaData> success, Action<DropboxException> failure)
{
if (path != "" && !path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateMetadataRequest(path, Root);
request.AddParameter("hash", hash);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <summary>
/// Gets list of metadata for search string
/// </summary>
/// <param name="searchString">The search string </param>
/// <param name="success">Success call back</param>
/// <param name="failure">Failure call back </param>
public void SearchAsync(string searchString, Action<List<MetaData>> success, Action<DropboxException> failure)
{
SearchAsync(searchString, string.Empty, success, failure);
}
/// <summary>
/// Gets list of metadata for search string
/// </summary>
/// <param name="searchString">The search string </param>
/// <param name="path">The path of the file or folder</param>
/// <param name="success">Success call back</param>
/// <param name="failure">Failure call back </param>
public void SearchAsync(string searchString, string path, Action<List<MetaData>> success, Action<DropboxException> failure)
{
var request = _requestHelper.CreateSearchRequest(searchString, path, Root);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <summary>
/// Downloads a File from dropbox given the path
/// </summary>
/// <param name="path">The path of the file to download</param>
/// /// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void GetFileAsync(string path, Action<IRestResponse> success, Action<DropboxException> failure)
{
if (!path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateGetFileRequest(path, Root);
ExecuteAsync(ApiType.Content, request, success, failure);
}
#if !WINDOWS_PHONE && !MONOTOUCH
/// <summary>
/// Uploads a File to Dropbox from the local file system to the specified folder
/// </summary>
/// <param name="path">The path of the folder to upload to</param>
/// <param name="localFile">The local file to upload</param>/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void UploadFileAsync(string path, FileInfo localFile, Action<MetaData> success, Action<DropboxException> failure)
{
//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);
}
}
UploadFileAsync(path, localFile.Name, bytes, success, failure);
}
#endif
/// <summary>
/// Uploads a File to Dropbox given the raw data.
/// </summary>
/// <param name="path">The path of the folder to upload to</param>
/// <param name="filename">The Name of the file to upload to dropbox</param>
/// <param name="fileData">The file data</param>
/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void UploadFileAsync(string path, string filename, byte[] fileData, Action<MetaData> success, Action<DropboxException> failure)
{
if (path != "" && !path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateUploadFileRequest(path, filename, fileData, Root);
ExecuteAsync(ApiType.Content, request, success, failure);
}
/// <summary>
/// Uploads a File to Dropbox given the raw data.
/// </summary>
/// <param name="path">The path of the folder to upload to</param>
/// <param name="filename">The Name of the file to upload to dropbox</param>
/// <param name="fileStream">The file data</param>
/// <param name="success">The callback Action to perform on completion</param>
/// <param name="failure">The callback Action to perform on exception</param>
public void UploadFileAsync(string path, string filename, Stream fileStream, Action<MetaData> success, Action<DropboxException> failure)
{
if (path != "" && !path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateUploadFileRequest(path, filename, fileStream, Root);
ExecuteAsync(ApiType.Content, request, success, failure);
}
/// <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="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void DeleteAsync(string path, Action<IRestResponse> success, Action<DropboxException> failure)
{
if (path != "" && !path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateDeleteFileRequest(path, Root);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <summary>
/// Copies a file or folder on Dropbox
/// </summary>
/// <param name="fromPath">The path to the file or folder to copy</param>
/// <param name="toPath">The path to where the file or folder is getting copied</param>
/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void CopyAsync(string fromPath, string toPath, Action<IRestResponse> success, Action<DropboxException> failure)
{
if (!fromPath.StartsWith("/")) fromPath = "/" + fromPath;
if (!toPath.StartsWith("/")) toPath = "/" + toPath;
var request = _requestHelper.CreateCopyFileRequest(fromPath, toPath, Root);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <summary>
/// Moves a file or folder on Dropbox
/// </summary>
/// <param name="fromPath">The path to the file or folder to move</param>
/// <param name="toPath">The path to where the file or folder is getting moved</param>
/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void MoveAsync(string fromPath, string toPath, Action<IRestResponse> success, Action<DropboxException> failure)
{
if (!fromPath.StartsWith("/")) fromPath = "/" + fromPath;
if (!toPath.StartsWith("/")) toPath = "/" + toPath;
var request = _requestHelper.CreateMoveFileRequest(fromPath, toPath, Root);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <summary>
/// Creates a folder on Dropbox
/// </summary>
/// <param name="path">The path to the folder to create</param>
/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void CreateFolderAsync(string path, Action<MetaData> success, Action<DropboxException> failure)
{
if (!path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateCreateFolderRequest(path, Root);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <summary>
/// Creates and returns a shareable link to files or folders.
/// Note: Links created by the /shares API call expire after thirty days.
/// </summary>
/// <param name="path">The path</param>
/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void GetShareAsync(string path, Action<ShareResponse> success, Action<DropboxException> failure)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateShareRequest(path, Root);
ExecuteAsync(ApiType.Base, request, success, failure);
}
/// <summary>
/// The beta delta function, gets updates for a given folder
/// </summary>
/// <param name="cursor">Cursor returned by prvious call</param>
/// <param name="success">Success Callback</param>
/// <param name="failure">Failure Callback</param>
public void GetDeltaAsync(string cursor, Action<DeltaPage> success, Action<DropboxException> failure)
{
var request = _requestHelper.CreateDeltaRequest(cursor);
ExecuteAsync<DeltaPageInternal>(ApiType.Base, request,
deltaResponse =>
{
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(StringListToDelta(stringList));
}
success (deltaPage);
},
failure);
}
/// <summary>
/// Helper function to convert a stringlist to a DeltaEntry object
/// </summary>
/// <param name="stringList"></param>
/// <returns></returns>
DeltaEntry StringListToDelta(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;
}
/// <summary>
/// Gets the thumbnail of an image given its MetaData
/// </summary>
/// <param name="file">The MetaData</param>
/// <param name="success">Success callback </param>
/// <param name="failure">Failure callback </param>
public void GetThumbnailAsync(MetaData file, Action<byte[]> success, Action<DropboxException> failure)
{
GetThumbnailAsync(file.Path, ThumbnailSize.Small, success, failure);
}
/// <summary>
/// Gets the thumbnail of an image given its MetaData
/// </summary>
/// <param name="file">The metadat file</param>
/// <param name="size">Thumbnail size</param>
/// <param name="success">success callback</param>
/// <param name="failure">Failure callback</param>
public void GetThumbnailAsync(MetaData file, ThumbnailSize size, Action<byte[]> success, Action<DropboxException> failure)
{
GetThumbnailAsync(file.Path, size, success, failure);
}
/// <summary>
/// Gets the thumbnail of an image given its path
/// </summary>
/// <param name="path">The path</param>
/// <param name="success">success callback</param>
/// <param name="failure">failure callback</param>
public void GetThumbnailAsync(string path, Action<byte[]> success, Action<DropboxException> failure)
{
GetThumbnailAsync(path, ThumbnailSize.Small, success, failure);
}
/// <summary>
/// Gets the thumbnail of an image given its path
/// </summary>
/// <param name="path">The path</param>
/// <param name="size">Thumbnail size</param>
/// <param name="success">success callback</param>
/// <param name="failure">failure callback</param>
public void GetThumbnailAsync(string path, ThumbnailSize size, Action<byte[]> success, Action<DropboxException> failure)
{
if (!path.StartsWith("/")) path = "/" + path;
var request = _requestHelper.CreateThumbnailRequest(path, size, Root);
ExecuteAsync(ApiType.Content, request,
response => success(response.RawBytes),
failure);
}
}
}
You can’t perform that action at this time.
