Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Add support for chunked uploading #17

Merged
merged 3 commits into from
Mar 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions DropNetRT/Client.Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,153 @@ public async Task<Metadata> Upload(string path, string filename, Stream stream,
return JsonConvert.DeserializeObject<Metadata>(responseBody);
}

private const int ChunkSize = 1024 * 1024;

private async Task<ChunkedUploadResponse> UploadChunk(Stream stream, ChunkedUploadResponse lastResponse, CancellationToken cancellationToken)
{
if(lastResponse == null)
{
throw new ArgumentNullException("lastResponse");
}

var offset = lastResponse.Offset;
var uploadId = lastResponse.UploadId;

var request = MakeChunkedUploadPutRequest(offset, uploadId);

var buffer = new byte[ChunkSize];

stream.Seek(lastResponse.Offset, SeekOrigin.Begin);
var contentSize = stream.Read(buffer, 0, ChunkSize);

if(contentSize == 0)
{
// Nothing left to send
return null;
}

HttpContent content = new ByteArrayContent(buffer, 0, contentSize);

try
{
var response = await _httpClient.PutAsync(request.RequestUri, content, cancellationToken);

if (response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}

var body = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ChunkedUploadResponse>(body);
}
catch (AggregateException)
{
throw;
}
catch (DropboxException)
{
throw;
}
catch (Exception ex)
{
throw new DropboxException(ex);
}
}

private async Task<ChunkedUploadResponse> StartChunkedUpload(Stream stream, CancellationToken cancellationToken)
{
var request = MakeChunkedUploadPutRequest(0);

var buffer = new byte[ChunkSize];

var contentSize = stream.Read(buffer, 0, ChunkSize);

HttpContent content = new ByteArrayContent(buffer, 0, contentSize);

try
{
var response = await _httpClient.PutAsync(request.RequestUri, content, cancellationToken);

if(response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}

var body = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ChunkedUploadResponse>(body);
}
catch (AggregateException)
{
throw;
}
catch(DropboxException)
{
throw;
}
catch(Exception ex)
{
throw new DropboxException(ex);
}
}

public async Task<Metadata> UploadChunked(string path, string filename, Stream stream, CancellationToken cancellationToken, IProgress<long> progress = null)
{
try
{
// Upload the initial chunk so we can get the upload_id value
var chunkedUploadResponse = await StartChunkedUpload(stream, cancellationToken);

if(chunkedUploadResponse == null)
{
throw new Exception("Initial chunk upload response was null.");
}

var uploadId = chunkedUploadResponse.UploadId;

if(progress != null)
{
progress.Report(chunkedUploadResponse.Offset);
}

// Keep uploading subsequent chunks until we don't have anything left to upload
while(chunkedUploadResponse != null)
{
chunkedUploadResponse = await UploadChunk(stream, chunkedUploadResponse, cancellationToken);

if(progress != null && chunkedUploadResponse != null)
{
progress.Report(chunkedUploadResponse.Offset);
}
}

// Commit the upload
var commitRequest = MakeChunkedUploadCommitRequest(path, filename, uploadId);

var response = await _httpClient.PostAsync(commitRequest.RequestUri, null, cancellationToken);

if (response.StatusCode != HttpStatusCode.OK)
{
throw new DropboxException(response);
}

var responseBody = await response.Content.ReadAsStringAsync();

return JsonConvert.DeserializeObject<Metadata>(responseBody);
}
catch(AggregateException)
{
throw;
}
catch(DropboxException)
{
throw;
}
catch(Exception ex)
{
throw new DropboxException(ex);
}
}

/// <summary>
/// Deletes the file or folder from dropbox with the given path
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions DropNetRT/Client.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,36 @@ private HttpRequest MakeUploadPutRequest(string path, string filename)

return request;
}

private HttpRequest MakeChunkedUploadPutRequest(long offset, string uploadId = null)
{
var requestUrl = MakeRequestString(string.Format("1/chunked_upload"), ApiType.Content);

var request = new HttpRequest(HttpMethod.Put, requestUrl);

if (!String.IsNullOrEmpty(uploadId))
{
request.AddParameter("upload_id", uploadId);
}

request.AddParameter("offset", offset);

_oauthHandler.Authenticate(request);

return request;
}

private HttpRequest MakeChunkedUploadCommitRequest(string path, string filename, string uploadId)
{
var requestUrl = MakeRequestString(string.Format("1/commit_chunked_upload/auto/{0}{2}{1}", path.CleanPath(), filename, path.CleanPath().Length > 0 ? "/" : ""), ApiType.Content);

var request = new HttpRequest(HttpMethod.Post, requestUrl);

request.AddParameter("upload_id", uploadId);

_oauthHandler.Authenticate(request);

return request;
}
}
}
1 change: 1 addition & 0 deletions DropNetRT/DropNetRT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="HttpHelpers\HttpParameter.cs" />
<Compile Include="HttpHelpers\HttpRequest.cs" />
<Compile Include="Models\AccountInfo.cs" />
<Compile Include="Models\ChunkedUploadResponse.cs" />
<Compile Include="Models\DeltaPage.cs" />
<Compile Include="Models\DeltaPageInternal.cs" />
<Compile Include="Models\Metadata.cs" />
Expand Down
18 changes: 18 additions & 0 deletions DropNetRT/Models/ChunkedUploadResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Runtime.Serialization;

namespace DropNetRT.Models
{
[DataContract]
public class ChunkedUploadResponse
{
[DataMember(Name = "upload_id")]
public string UploadId { get; set; }

[DataMember(Name = "offset")]
public long Offset { get; set; }

[DataMember(Name = "expires")]
public DateTimeOffset Expires { get; set; }
}
}