Skip to content

Commit

Permalink
Fix exception using chunked uploading with small files
Browse files Browse the repository at this point in the history
  • Loading branch information
hartez committed Apr 5, 2020
1 parent 6956b2c commit d81dd08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
16 changes: 9 additions & 7 deletions DropboxClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,10 +35,9 @@ public static async Task<FileMetadata> Upload(this DropboxClient client, string
public static async Task<FileMetadata> UploadChunked(this DropboxClient client,
string folder, string fileName, Stream fs, CancellationToken cancellationToken, IProgress<long> 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;
Expand All @@ -48,9 +50,9 @@ public static async Task<FileMetadata> 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)
{
Expand All @@ -59,7 +61,7 @@ public static async Task<FileMetadata> 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)
{
Expand All @@ -75,7 +77,7 @@ public static async Task<FileMetadata> UploadChunked(this DropboxClient client,
await client.Files.UploadSessionAppendV2Async(cursor, body: memStream);
if(!cancellationToken.IsCancellationRequested)
{
progress.Report(i * chunkSize);
progress.Report(i * ChunkSize);
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ private static int Main(string[] args)
return (int)exitCode;
}



private static async Task Upload(IEnumerable<string> paths, UploadOptions options, DropboxClient client,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d81dd08

Please sign in to comment.