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.Sync.cs
More file actions
378 lines (329 loc) · 13.3 KB
/
Copy pathFiles.Sync.cs
File metadata and controls
378 lines (329 loc) · 13.3 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
#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
{
/// <summary>
/// Gets MetaData for the root folder.
/// </summary>
/// <returns></returns>
public MetaData GetMetaData()
{
return GetMetaData(string.Empty);
}
/// <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>
/// <returns></returns>
public MetaData GetMetaData(string path)
{
var request = _requestHelper.CreateMetadataRequest(path, Root);
return Execute<MetaData>(ApiType.Base, request);
}
/// <summary>
/// Gets list of metadata for search string
/// </summary>
/// <param name="searchString">The search string </param>
public List<MetaData> Search(string searchString)
{
return Search(searchString, string.Empty);
}
/// <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>
public List<MetaData> Search(string searchString, string path)
{
var request = _requestHelper.CreateSearchRequest(searchString, path, DropboxRoot);
return Execute<List<MetaData>>(ApiType.Base, request);
}
//TODO - Make class for this to return (instead of just a byte[])
/// <summary>
/// Downloads a File from dropbox given the path
/// </summary>
/// <param name="path">The path of the file to download</param>
/// <returns>The files raw bytes</returns>
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;
}
/// <summary>
/// Retrieve the content of a file in the local file system
/// </summary>
/// <param name="localFile">The local file to upload</param>
/// <returns>True on success</returns>
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;
}
/// <summary>
/// NOTE: DOES NOT WORK, Use UploadFile
/// 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>
/// <returns>True on success</returns>
[Obsolete("PUT doesn't work with current RestSharp. Sorry :(")]
public MetaData UploadFilePUT(string path, string filename, byte[] fileData)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateUploadFilePutRequest(path, filename, fileData, Root);
var response = _restClientContent.Execute<MetaData>(request);
//TODO - Return something better here?
return response.Data;
}
/// <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>
/// <returns>True on success</returns>
public MetaData UploadFile(string path, string filename, byte[] fileData)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateUploadFileRequest(path, filename, fileData, Root);
var response = _restClientContent.Execute<MetaData>(request);
//TODO - Return something better here?
return response.Data;
}
/// <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="stream">The file stream</param>
/// <returns>True on success</returns>
public MetaData UploadFile(string path, string filename, Stream stream)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateUploadFileRequest(path, filename, stream, Root);
var response = _restClientContent.Execute<MetaData>(request);
//TODO - Return something better here?
return response.Data;
}
/// <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 MetaData Delete(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateDeleteFileRequest(path, Root);
return Execute<MetaData>(ApiType.Base, request);
}
/// <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>
/// <returns>True on success</returns>
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);
}
/// <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>
/// <returns>True on success</returns>
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);
}
/// <summary>
/// Creates a folder on Dropbox
/// </summary>
/// <param name="path">The path to the folder to create</param>
/// <returns>MetaData of the newly created folder</returns>
public MetaData CreateFolder(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateCreateFolderRequest(path, Root);
return Execute<MetaData>(ApiType.Base, request);
}
/// <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"></param>
/// <returns></returns>
public ShareResponse GetShare(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateShareRequest(path, Root);
return Execute<ShareResponse>(ApiType.Base, request);
}
/// <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 ShareResponse GetMedia(string path)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var request = _requestHelper.CreateMediaRequest(path, Root);
return Execute<ShareResponse>(ApiType.Base, request);
}
/// <summary>
/// Gets the thumbnail of an image given its MetaData
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public byte[] GetThumbnail(MetaData file)
{
return 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 byte[] GetThumbnail(MetaData file, ThumbnailSize size)
{
return GetThumbnail(file.Path, size);
}
/// <summary>
/// Gets the thumbnail of an image given its path
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public byte[] GetThumbnail(string path)
{
return GetThumbnail(path, ThumbnailSize.Small);
}
/// <summary>
/// Gets the thumbnail of an image given its path
/// </summary>
/// <param name="path">The path to the picture</param>
/// <param name="size">The size to return the thumbnail</param>
/// <returns></returns>
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;
}
/// <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);
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>
/// Helper function to convert a stringlist to a DeltaEntry object
/// </summary>
/// <param name="stringList"></param>
/// <returns></returns>
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;
}
}
}
#endif
You can’t perform that action at this time.
