-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
226 lines (189 loc) · 7.3 KB
/
Program.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;
using Dropbox.Api;
using Dropbox.Api.Files;
namespace PneumaticTube
{
internal class Program
{
static bool ShowCancelHelp = true;
private static int Main(string[] args)
{
return Parser.Default.ParseArguments<UploadOptions>(args)
.MapResult(
options => RunAndReturnExitCode(options),
errors => (int)ExitCode.BadArguments);
}
static int RunAndReturnExitCode(UploadOptions options)
{
if (options.Reset)
{
DropboxClientFactory.ResetAuthentication();
}
var source = Path.GetFullPath(options.LocalPath);
if (!File.Exists(source) && !Directory.Exists(source))
{
Console.WriteLine("Source does not exist.");
return (int)ExitCode.FileNotFound;
}
// Fix up Dropbox path (fix Windows-style slashes)
options.DropboxPath = options.DropboxPath.Replace(@"\", "/");
string[] files;
// Determine whether source is a file or directory
var attr = File.GetAttributes(source);
if (attr.HasFlag(FileAttributes.Directory))
{
// TODO see if we like what this looks like for directories
Output($"Uploading folder \"{source}\" to {(!string.IsNullOrEmpty(options.DropboxPath) ? options.DropboxPath : "Dropbox")}", options);
Output("Ctrl-C to cancel", options);
ShowCancelHelp = false;
// TODO Figure out what, if anything, we want to do about subdirectories
files = Directory.GetFiles(source);
}
else
{
files = new[] { source };
}
var exitCode = ExitCode.UnknownError;
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
cts.Cancel();
};
try
{
var client = DropboxClientFactory.CreateDropboxClient(options.TimeoutSeconds).Result;
var task = Task.Run(() => Upload(files, options, client, cts.Token), cts.Token);
task.Wait(cts.Token);
exitCode = ExitCode.Success;
}
catch (OperationCanceledException)
{
Output("\nUpload canceled", options);
exitCode = ExitCode.Canceled;
}
catch (AggregateException ex)
{
foreach (var exception in ex.Flatten().InnerExceptions)
{
switch (exception)
{
case DropboxException dex:
exitCode = HandleDropboxException(dex);
break;
case TaskCanceledException tex:
exitCode = HandleTimeoutError(tex);
break;
default:
exitCode = HandleGenericError(ex);
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred and your file was not uploaded.");
Console.WriteLine(ex);
}
return (int)exitCode;
}
private static async Task Upload(IEnumerable<string> paths, UploadOptions options, DropboxClient client,
CancellationToken cancellationToken)
{
foreach(var path in paths)
{
var source = Path.GetFullPath(path);
var filename = Path.GetFileName(source);
await Upload(source, filename, options, client, cancellationToken);
}
}
private static async Task Upload(string source, string filename, UploadOptions options, DropboxClient client,
CancellationToken cancellationToken)
{
Output($"Uploading {filename} to {options.DropboxPath}", options);
Console.Title = $"Uploading {filename} to {(!string.IsNullOrEmpty(options.DropboxPath) ? options.DropboxPath : "Dropbox")}";
if(ShowCancelHelp)
{
Output("Ctrl-C to cancel", options);
ShowCancelHelp = false;
}
using(var fs = new FileStream(source, FileMode.Open, FileAccess.Read))
{
Metadata uploaded;
if(!options.Chunked && fs.Length >= DropboxClientExtensions.ChunkedThreshold)
{
Output("File is larger than 150MB, using chunked uploading.", options);
options.Chunked = true;
}
if (options.Chunked && fs.Length <= options.ChunkSize)
{
Output("File is smaller than the specified chunk size, disabling chunked uploading.", options);
options.Chunked = false;
}
if(options.Chunked)
{
var progress = ConfigureProgressHandler(options, fs.Length);
uploaded = await client.UploadChunked(options.DropboxPath, filename, fs, cancellationToken, progress, options.ChunkSize);
}
else
{
uploaded = await client.Upload(options.DropboxPath, filename, fs);
}
Output("Whoosh...", options);
Output($"Uploaded {uploaded.Name} to {uploaded.PathDisplay}; Revision {uploaded.AsFile.Rev}", options);
}
}
private static void Output(string message, UploadOptions options)
{
if(options.Quiet)
{
return;
}
Console.WriteLine(message);
}
private static IProgress<long> ConfigureProgressHandler(UploadOptions options, long fileSize)
{
if(options.NoProgress || options.Quiet)
{
return new NoProgressDisplay(fileSize, options.Quiet);
}
if(options.Bytes)
{
return new BytesProgressDisplay(fileSize);
}
return new PercentProgressDisplay(fileSize);
}
private static ExitCode HandleDropboxException(DropboxException ex)
{
Console.WriteLine("An error occurred and your file was not uploaded.");
var exitCode = ex.HandleAuthException()
?? ex.HandleAccessException()
?? ex.HandleRateLimitException()
?? ex.HandleBadInputException()
?? ex.HandleHttpException()
?? ExitCode.UnknownError;
if (exitCode == ExitCode.UnknownError)
{
Console.WriteLine(ex.Message);
}
return exitCode;
}
private static ExitCode HandleGenericError(Exception ex)
{
Console.WriteLine("An error occurred and your file was not uploaded.");
Console.WriteLine(ex);
return ExitCode.UnknownError;
}
private static ExitCode HandleTimeoutError(TaskCanceledException ex)
{
Console.WriteLine("An HTTP operation timed out and your file was not uploaded.");
Console.WriteLine(ex);
return ExitCode.Canceled;
}
}
}