-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDracoonExamples.cs
More file actions
425 lines (340 loc) · 18.7 KB
/
DracoonExamples.cs
File metadata and controls
425 lines (340 loc) · 18.7 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
using Dracoon.Sdk.Error;
using Dracoon.Sdk.Filter;
using Dracoon.Sdk.Model;
using Dracoon.Sdk.Sort;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using Attribute = Dracoon.Sdk.Model.Attribute;
namespace Dracoon.Sdk.Example {
public static class DracoonExamples {
private static readonly Uri SERVER_URI = new Uri("https://dracoon.team");
private static readonly string ACCESS_TOKEN = "ACCESS_TOKEN";
private static readonly string ENCRYPTION_PASSWORD = "ENCRYPTION_PASSWORD";
private static DracoonClient dc;
[STAThread]
private static void Main() {
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
DracoonAuth dracoonAuth = new DracoonAuth(ACCESS_TOKEN);
IWebProxy wp = WebRequest.GetSystemWebProxy();
wp.Credentials = CredentialCache.DefaultNetworkCredentials;
DracoonHttpConfig config = new DracoonHttpConfig(retryEnabled: true, webProxy: wp);
dc = new DracoonClient(SERVER_URI, dracoonAuth, ENCRYPTION_PASSWORD.ToCharArray(), new Logger(), config);
}
#region DracoonClient.Server
private static void GetServerData() {
string serverVersion = dc.Server.GetVersion();
Console.WriteLine("Server version: " + serverVersion);
DateTime? serverTime = dc.Server.GetTime();
if (serverTime.HasValue) {
Console.WriteLine("Server time: " + serverTime.Value.ToLocalTime());
}
}
private static void GetServerSettings() {
ServerGeneralSettings generalSettings = dc.Server.ServerSettings.GetGeneral();
Console.WriteLine("Crypto is enabled: " + generalSettings.CryptoEnabled);
Console.WriteLine("Share password via SMS is enabled: " + generalSettings.SharePasswordSmsEnabled);
ServerInfrastructureSettings infrastructureSettings = dc.Server.ServerSettings.GetInfrastructure();
//...
}
private static void GetPasswordPolicies() {
PasswordEncryptionPolicies encryptionPolicy = dc.Server.ServerPolicies.GetEncryptionPasswordPolicies();
PasswordSharePolicies sharePolicy = dc.Server.ServerPolicies.GetSharesPasswordPolicies();
Console.WriteLine("Minimum share password length is: " + sharePolicy.MinimumPasswordLength);
Console.WriteLine("Minimum encryption password length is: " + encryptionPolicy.MinimumPasswordLength);
}
private static void GetAvailableUserKeyPairAlgorithms() {
List<UserKeyPairAlgorithmData> availableUserKeyPairAlgorithms = dc.Server.ServerSettings.GetAvailableUserKeyPairAlgorithms();
foreach (UserKeyPairAlgorithmData current in availableUserKeyPairAlgorithms) {
Console.WriteLine("Available User key pair algorithm: " + current.Algorithm + " with its state: " + current.State);
}
}
#endregion
#region DracoonClient.Account
private static void CheckAuth() {
try {
dc.Account.ValidateTokenValidity();
Console.WriteLine("Tokens are still valid.");
} catch (DracoonApiException apiError) {
if (apiError.ErrorCode.IsAuthError()) {
Console.WriteLine("Tokens are not valid anymore.");
}
}
}
private static void GetUserAccount() {
UserAccount userAccount = dc.Account.GetUserAccount();
Console.WriteLine("UserId: " + userAccount.Id + "; FirstName: " + userAccount.FirstName + "; LastName: " + userAccount.LastName +
"; E-mail: " + userAccount.Email);
}
private static void GetCustomerAccount() {
CustomerAccount customerAccount = dc.Account.GetCustomerAccount();
Console.WriteLine("CustomerId: " + customerAccount.Id + "; Name: " + customerAccount.Name + "; Accounts: " +
customerAccount.AccountsUsed + "/" + customerAccount.AccountsLimit + "; Space: " + customerAccount.SpaceUsed + "/" +
customerAccount.SpaceLimit);
}
private static void SetUserKeyPair() {
dc.Account.SetUserKeyPair(Crypto.Sdk.UserKeyPairAlgorithm.RSA2048);
}
private static void CheckUserKeyPair() {
bool encryptionPasswordIsValid = dc.Account.CheckUserKeyPairPassword(Crypto.Sdk.UserKeyPairAlgorithm.RSA2048);
Console.WriteLine("Encryption password is valid: " + encryptionPasswordIsValid);
}
private static void DeleteUserKeyPair() {
dc.Account.DeleteUserKeyPair(Crypto.Sdk.UserKeyPairAlgorithm.RSA2048);
}
private static void GetUserAvatar() {
byte[] avatar = dc.Account.GetAvatar();
}
private static void GetUserProfileAttributes() {
AttributeList attributes = dc.Account.GetUserProfileAttributeList();
foreach (Attribute current in attributes.Items) {
Console.WriteLine("Attribute key: " + current.Key + "; Attribute value: " + current.Value);
}
}
private static void GetUserProfileAttribute() {
Attribute attribute = dc.Account.GetUserProfileAttribute("anyKey");
Console.WriteLine("Single attribute key: " + attribute.Key + "; Single attribute value: " + attribute.Value);
}
private static void AddUserProfileAttribute() {
List<Attribute> attributes = new List<Attribute>() {
new Attribute() {
Key = "anyKey",
Value = "anyValue"
}
};
dc.Account.AddOrUpdateUserProfileAttributes(attributes);
}
private static void DeleteUserProfileAttribute() {
dc.Account.DeleteProfileAttribute("anyKey");
}
#endregion
#region DracoonClient.Nodes
private static void ListRootNodes() {
NodeList rootNodes = dc.Nodes.GetNodes();
foreach (Node current in rootNodes.Items) {
Console.WriteLine("NodeId: " + current.Id + "; NodeName: " + current.Name);
}
}
private static void GetAvatarImageOfNodeCreator() {
long nodeId = 1;
Node node = dc.Nodes.GetNode(nodeId);
byte[] avatar = dc.Users.GetUserAvatar(node.CreatedBy.Id, node.CreatedBy.AvatarUUID);
}
private static void ListFilteredRootNodes() {
GetNodesFilter getNodesFilter = new GetNodesFilter();
getNodesFilter.AddNodeTypeFilter(GetNodesFilter.Type.EqualTo(NodeType.Room).Or().EqualTo(NodeType.Folder).Build());
getNodesFilter.AddNameFilter(GetNodesFilter.Name.Contains("Test").Build());
NodeList rootNodes = dc.Nodes.GetNodes(filter: getNodesFilter);
foreach (Node current in rootNodes.Items) {
Console.WriteLine("NodeId: " + current.Id + "; NodeName: " + current.Name);
}
}
private static void CreateRoom() {
List<long> roomAdminIds = new List<long> {
1
};
CreateRoomRequest request = new CreateRoomRequest("TestRoom", adminUserIds: roomAdminIds, notes: "It's a test room creation.");
Node createdRoomNode = dc.Nodes.CreateRoom(request);
Console.WriteLine("Created room id: " + createdRoomNode.Id + "; Name: " + createdRoomNode.Name);
}
private static void UpdateRoom() {
UpdateRoomRequest request = new UpdateRoomRequest(1, name: "RenamedTestRoom", notes: "Renamed the test room");
Node updatedRoomNode = dc.Nodes.UpdateRoom(request);
Console.WriteLine("Updated room id: " + updatedRoomNode.Id + "; Name: " + updatedRoomNode.Name);
}
private static void CreateFolder() {
CreateFolderRequest request = new CreateFolderRequest(1, "TestFolder", "It's a test folder creation.");
Node createdFolderNode = dc.Nodes.CreateFolder(request);
Console.WriteLine("Created folder id: " + createdFolderNode.Id + "; Name: " + createdFolderNode.Name);
}
private static void DeleteNodes() {
List<long> nodeIdsForDeletion = new List<long> {
1,
2
};
DeleteNodesRequest request = new DeleteNodesRequest(nodeIdsForDeletion);
dc.Nodes.DeleteNodes(request);
}
private static void CopyNodes() {
List<CopyNode> nodeWhichWereCopied = new List<CopyNode> {
new CopyNode(2),
new CopyNode(3),
new CopyNode(4)
};
CopyNodesRequest request = new CopyNodesRequest(1, nodeWhichWereCopied);
Node resultingParentNode = dc.Nodes.CopyNodes(request);
Console.WriteLine("New parent node id: " + resultingParentNode.Id + "; parent node childes: " + resultingParentNode.CountChildren);
}
private static void MoveNodes() {
List<MoveNode> nodesWhichWereMoved = new List<MoveNode> {
new MoveNode(5),
new MoveNode(6),
new MoveNode(7)
};
MoveNodesRequest request = new MoveNodesRequest(1, nodesWhichWereMoved);
Node resultingParentNode = dc.Nodes.MoveNodes(request);
Console.WriteLine("New parent node id: " + resultingParentNode.Id + "; parent node childes: " + resultingParentNode.CountChildren);
}
private static void SearchNodes() {
NodeList searchedNodes = dc.Nodes.SearchNodes("Test", 0);
foreach (Node current in searchedNodes.Items) {
Console.WriteLine("SearchedNodeId: " + current.Id + "; NodeName: " + current.Name);
}
}
private static void SearchNodesWithFilterAndSort() {
SearchNodesFilter searchFilter = new SearchNodesFilter();
searchFilter.AddNodeTypeFilter(SearchNodesFilter.Type.EqualTo(NodeType.File).Build());
NodeList searchedNodes = dc.Nodes.SearchNodes("Test", 0, filter: searchFilter, sort: SearchNodesSort.Size.Ascending());
foreach (Node current in searchedNodes.Items) {
Console.WriteLine("SearchedNodeId: " + current.Id + "; NodeName: " + current.Name + "; Size: " + current.Size);
}
}
private static void GetFavorites() {
SearchNodesFilter favoriteFilter = new SearchNodesFilter();
favoriteFilter.AddIsFavoriteFilter(SearchNodesFilter.IsFavorite.EqualTo(true).Build());
NodeList favoriteNodes = dc.Nodes.SearchNodes("*", 0, filter: favoriteFilter);
foreach (Node current in favoriteNodes.Items) {
Console.WriteLine("SearchedNodeId: " + current.Id + "; NodeName: " + current.Name + "; isFavorite: " + current.IsFavorite);
}
}
private static void UploadFile() {
string localFilePath = "C:\\temp\\test.txt";
FileInfo fileInfo = new FileInfo(localFilePath);
FileUploadRequest request = new FileUploadRequest(1, "test.txt");
request.CreationTime = fileInfo.CreationTimeUtc;
request.ModificationTime = fileInfo.LastWriteTimeUtc;
FileStream stream = File.Open(localFilePath, FileMode.Open);
Node uploadedNode = dc.Nodes.UploadFile(Guid.NewGuid().ToString(), request, stream, callback: new ULCallback());
}
private static void DownloadEncryptedFile() {
dc.EncryptionPassword = ENCRYPTION_PASSWORD.ToCharArray();
Node node = dc.Nodes.GetNode(1);
FileStream stream = File.Create("C:\\temp\\" + node.Name);
dc.Nodes.DownloadFile(Guid.NewGuid().ToString(), node.Id, stream, new DLCallback());
}
private static void DownloadFileAsync() {
Node node = dc.Nodes.GetNode(1);
FileStream stream = File.Create("C:\\temp\\" + node.Name);
dc.Nodes.StartDownloadFileAsync(Guid.NewGuid().ToString(), node.Id, stream, new DLCallback());
}
private static void DownloadFile() {
Node node = dc.Nodes.GetNode(10);
FileStream stream = File.Create("C:\\temp\\" + node.Name);
dc.Nodes.DownloadFile(Guid.NewGuid().ToString(), node.Id, stream, new DLCallback());
}
private static void GenerateVirusProtectionInfo() {
List<long> requestFileIds = new List<long> {
2549
};
List<FileVirusProtectionInfo> infoList = dc.Nodes.GenerateVirusProtectionInfo(requestFileIds);
foreach (FileVirusProtectionInfo current in infoList) {
Console.WriteLine("FileId: " + current.NodeId + "; Verdict: " + current.Verdict);
}
}
#endregion
#region DracoonClient.Shares
public static void CreateDownloadShare() {
CreateDownloadShareRequest req = new CreateDownloadShareRequest(1, password: "Passw0rd!".ToCharArray());
DownloadShare dl = dc.Shares.CreateDownloadShare(req);
}
public static void SendDownloadShareInfoMail() {
MailShareInfoRequest req = new MailShareInfoRequest(1, "Testmail!", new List<string> { "[email protected]" });
dc.Shares.SendMailForDownloadShare(req);
}
public static void GetDownloadShares() {
GetDownloadSharesFilter filter = new GetDownloadSharesFilter();
filter.AddAccessKeyFilter(GetDownloadSharesFilter.AccessKey.Contains("ACCESSKEY").Build());
DownloadShareList res = dc.Shares.GetDownloadShares(filter: filter, sort: SharesSort.CreatedAt.Descending());
}
public static void GetUploadShares() {
GetUploadSharesFilter filter = new GetUploadSharesFilter();
filter.AddAccessKeyFilter(GetUploadSharesFilter.AccessKey.Contains("ACCESSKEY").Build());
UploadShareList res = dc.Shares.GetUploadShares(filter: filter, sort: SharesSort.CreatedAt.Ascending());
}
#endregion
#region RecycleBin / Versioning
private static void GetFileVersions() {
RecycleBinItemList binItems = dc.Nodes.GetRecycleBinItems(1);
foreach (RecycleBinItem current in binItems.Items) {
Console.WriteLine("NodeName: " + current.Name + "; Versions: " + current.VersionsCount + "; LastDeletedNodeId: " +
current.LastDeletedNodeId + "; ParentPath: " + current.ParentPath);
}
PreviousVersionList versionList = dc.Nodes.GetPreviousVersions(1, NodeType.File, "test.txt");
foreach (PreviousVersion current in versionList.Items) {
Console.WriteLine("NodeName: " + current.Name + "; Id: " + current.Id + "; ParentPath: " + current.ParentPath + "; DeletedAt: " +
current.DeletedAt.ToString());
}
// Restore the last version of the node "test.txt"
RestorePreviousVersionsRequest request = new RestorePreviousVersionsRequest(new List<long>() {
versionList.Items[0].Id.Value
});
dc.Nodes.RestorePreviousVersion(request);
}
#endregion
#region File Up/Download-Callbacks
private class DLCallback : IFileDownloadCallback {
private Dictionary<string, Stopwatch> requestTimings = new Dictionary<string, Stopwatch>();
public void OnCanceled(string actionId) {
requestTimings.Remove(actionId);
Console.WriteLine("DLCallback -> " + "Download canceled: " + actionId);
}
public void OnFailed(string actionId, DracoonException occuredError) {
requestTimings.Remove(actionId);
Console.WriteLine("DLCallback -> " + "Download failed: " + actionId + " with: " + occuredError.Message);
}
public void OnFinished(string actionId) {
Stopwatch watch;
if (requestTimings.TryGetValue(actionId, out watch)) {
watch.Stop();
Console.WriteLine("DLCallback -> " + "Download finished: " + actionId + " (" + watch.Elapsed.ToString() + ")");
requestTimings.Remove(actionId);
} else {
Console.WriteLine("DLCallback -> " + "Download finished: " + actionId);
}
}
public void OnRunning(string actionId, long bytesDownloaded, long bytesTotal) {
Console.WriteLine("DLCallback -> " + "Download progress for: " + actionId + " --> " + bytesDownloaded + "/" + bytesTotal);
}
public void OnStarted(string actionId) {
Stopwatch newWatch = new Stopwatch();
requestTimings.Add(actionId, newWatch);
newWatch.Start();
Console.WriteLine("DLCallback -> " + "Download started: " + actionId);
}
}
private class ULCallback : IFileUploadCallback {
private Dictionary<string, Stopwatch> requestTimings = new Dictionary<string, Stopwatch>();
public void OnCanceled(string actionId) {
requestTimings.Remove(actionId);
Console.WriteLine("ULCallback -> " + "Upload canceled: " + actionId);
}
public void OnFailed(string actionId, DracoonException occuredError) {
requestTimings.Remove(actionId);
Console.WriteLine("ULCallback -> " + "Upload failed: " + actionId + " with: " + occuredError.Message);
}
public void OnFinished(string actionId, Node resultNode) {
if (requestTimings.TryGetValue(actionId, out Stopwatch watch)) {
watch.Stop();
Console.WriteLine("ULCallback -> " + "Upload finished: " + actionId + " | New node id is " + resultNode.Id + " and name " +
resultNode.Name + " (" + watch.Elapsed.ToString() + ")");
requestTimings.Remove(actionId);
} else {
Console.WriteLine("ULCallback -> " + "Upload finished: " + actionId);
}
}
public void OnRunning(string actionId, long bytesUploaded, long bytesTotal) {
Console.WriteLine("ULCallback -> " + "Upload progress for: " + actionId + " --> " + bytesUploaded + "/" + bytesTotal);
}
public void OnStarted(string actionId) {
Stopwatch newWatch = new Stopwatch();
requestTimings.Add(actionId, newWatch);
newWatch.Start();
Console.WriteLine("ULCallback -> " + "Upload started: " + actionId);
}
}
#endregion
}
}