-
Notifications
You must be signed in to change notification settings - Fork 1
/
DropnetExtensions.cs
111 lines (104 loc) · 4.65 KB
/
DropnetExtensions.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using DropNet;
using DropNet.Models;
using FubuCore;
using ChpokkWeb.Infrastructure;
namespace ChpokkWeb.Features.Remotes.Dropbox {
public static class DropnetExtensions {
public static IEnumerable<MetaData> GetMetadataRecursive(MetaData metaData) {
var result = new List<MetaData>() {metaData};
if (metaData.Contents != null) {
foreach (var data in metaData.Contents) {
result.AddRange(GetMetadataRecursive(data));
}
}
return result;
}
/// <summary>
/// Get all remote folder names recursively
/// </summary>
/// <param name="client"></param>
/// <param name="path">Parent path, defaults to "/"</param>
/// <returns></returns>
public static IEnumerable<string> GetFoldernames(this DropNetClient client, string path = "/") {
var metaData = client.GetMetaData(path);
var folders = from meta in GetMetadataRecursive(metaData)
where meta.Is_Dir && !meta.Is_Deleted
select meta.Path;
return folders;
}
/// <summary>
/// Get all remote file names recursively
/// </summary>
/// <param name="client"></param>
/// <param name="path">Parent path, defaults to "/"</param>
/// <returns></returns>
public static IEnumerable<string> GetFilenames(this DropNetClient client, string path = "/") {
var metaData = client.GetMetaData(path);
var folders = from meta in GetMetadataRecursive(metaData)
where !meta.Is_Dir && !meta.Is_Deleted
select meta.Path;
return folders;
}
/// <summary>
/// Download a file from Dropbox
/// </summary>
/// <param name="client">Dropnet client</param>
/// <param name="remotePath">The file's path on Dropbox (use forward slashes for directory separators)</param>
/// <param name="localPath">The target path</param>
public static void DownloadFile(this DropNetClient client, string remotePath, string localPath) {
var bytes = client.GetFile(remotePath);
var targetFolder = localPath.ParentDirectory();
if (!Directory.Exists(targetFolder)) {
Directory.CreateDirectory(targetFolder);
}
File.WriteAllBytes(localPath, bytes);
}
/// <summary>
/// Downloads a folder from Dropbox
/// </summary>
/// <param name="client"></param>
/// <param name="remoteFolder">The path to the source folder. </param>
/// <param name="localFolder"></param>
/// <param name="includeSelf">If false, only the contents of the source folder are downloaded, not the source folder itself. If true, the source folder is downloaded into the target folder.</param>
public static void DownloadFolder(this DropNetClient client, string remoteFolder, string localFolder, bool includeSelf) {
foreach (var remoteFile in client.GetFilenames(remoteFolder)) {
var parent = includeSelf ? remoteFolder.ParentDirectory() : remoteFolder;
var relativeRemotePath = remoteFile.RemoveFromStart(parent);
var localFilePath = localFolder.AppendPathEvenIfItIsRooted(relativeRemotePath.Replace('/', Path.DirectorySeparatorChar));
client.DownloadFile(remoteFile, localFilePath);
}
}
/// <summary>
/// Upload a file
/// </summary>
/// <param name="client"></param>
/// <param name="localPath">Local filename</param>
/// <param name="remotePath">Filename on Dropbox</param>
public static void UploadFile(this DropNetClient client, string localPath, string remotePath) {
var path = remotePath.ParentDirectory();
var fileName = remotePath.GetFileNameUniversal();
client.UploadFile(path, fileName, File.ReadAllBytes(localPath));
}
/// <summary>
/// Upload a folder
/// </summary>
/// <param name="client"></param>
/// <param name="localFolder">Source</param>
/// <param name="remoteFolder">Destination</param>
/// <param name="includeSelf">If false, only the contents of the source folder are uploaded, not the source folder itself. If true, the source folder is uploaded into the target folder.</param>
public static void UploadFolder(this DropNetClient client, string localFolder, string remoteFolder, bool includeSelf) {
foreach (var localPath in Directory.GetFiles(localFolder, "*.*", SearchOption.AllDirectories)) {
var parent = includeSelf ? localFolder.ParentDirectory() : localFolder;
var relativeLocalPath = localPath.PathRelativeTo(parent);
var relativeRemotePath = relativeLocalPath.Replace(Path.DirectorySeparatorChar, '/');
var remotePath = remoteFolder.AppendPath(relativeRemotePath).Replace(Path.DirectorySeparatorChar, '/');
client.UploadFile(localPath, remotePath);
}
}
}
}