diff --git a/DropboxClientExtensions.cs b/DropboxClientExtensions.cs index b7486fa..8c6bf6f 100644 --- a/DropboxClientExtensions.cs +++ b/DropboxClientExtensions.cs @@ -9,6 +9,9 @@ namespace PneumaticTube { internal static class DropboxClientExtensions { + public const int ChunkSize = 128 * 1024; + public const int ChunkedThreshold = 150 * 1024 * 1024; + private static string CombinePath(string folder, string fileName) { // We can't use Path.Combine here because we'll end up with the Windows separator ("\") and @@ -32,10 +35,9 @@ public static async Task Upload(this DropboxClient client, string public static async Task UploadChunked(this DropboxClient client, string folder, string fileName, Stream fs, CancellationToken cancellationToken, IProgress progress) { - const int chunkSize = 128 * 1024; - int chunks = (int)Math.Ceiling((double)fs.Length / chunkSize); + int chunks = (int)Math.Ceiling((double)fs.Length / ChunkSize); - byte[] buffer = new byte[chunkSize]; + byte[] buffer = new byte[ChunkSize]; string sessionId = null; FileMetadata resultMetadata = null; @@ -48,9 +50,9 @@ public static async Task UploadChunked(this DropboxClient client, throw new OperationCanceledException(cancellationToken); } - var byteRead = fs.Read(buffer, 0, chunkSize); + var bytesRead = fs.Read(buffer, 0, ChunkSize); - using(var memStream = new MemoryStream(buffer, 0, byteRead)) + using(var memStream = new MemoryStream(buffer, 0, bytesRead)) { if(i == 0) { @@ -59,7 +61,7 @@ public static async Task UploadChunked(this DropboxClient client, } else { - UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * i)); + UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(ChunkSize * i)); if(i == chunks - 1) { @@ -75,7 +77,7 @@ public static async Task UploadChunked(this DropboxClient client, await client.Files.UploadSessionAppendV2Async(cursor, body: memStream); if(!cancellationToken.IsCancellationRequested) { - progress.Report(i * chunkSize); + progress.Report(i * ChunkSize); } } } diff --git a/Program.cs b/Program.cs index cbd8e42..f270592 100644 --- a/Program.cs +++ b/Program.cs @@ -104,8 +104,6 @@ private static int Main(string[] args) return (int)exitCode; } - - private static async Task Upload(IEnumerable paths, UploadOptions options, DropboxClient client, CancellationToken cancellationToken) { @@ -134,12 +132,18 @@ private static async Task Upload(string source, string filename, UploadOptions o { Metadata uploaded; - if(!options.Chunked && fs.Length >= 150 * 1024 * 1024) + 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 <= DropboxClientExtensions.ChunkSize) + { + Output("File is less than 128kB, disabling chunked uploading.", options); + options.Chunked = false; + } + if(options.Chunked) { var progress = ConfigureProgressHandler(options, fs.Length);