diff --git a/DropNetRT/Client.Files.cs b/DropNetRT/Client.Files.cs index d1633b4..00a734c 100644 --- a/DropNetRT/Client.Files.cs +++ b/DropNetRT/Client.Files.cs @@ -440,6 +440,11 @@ public async Task Upload(string path, string filename, Stream stream, private const int ChunkSize = 1024 * 1024; private async Task UploadChunk(Stream stream, ChunkedUploadResponse lastResponse, CancellationToken cancellationToken) + { + return await UploadChunk(stream, ChunkSize, lastResponse, cancellationToken); + } + + public async Task UploadChunk(Stream stream, int chunkSize, ChunkedUploadResponse lastResponse, CancellationToken cancellationToken) { if(lastResponse == null) { @@ -451,10 +456,10 @@ private async Task UploadChunk(Stream stream, ChunkedUplo var request = MakeChunkedUploadPutRequest(offset, uploadId); - var buffer = new byte[ChunkSize]; + var buffer = new byte[chunkSize]; stream.Seek(lastResponse.Offset, SeekOrigin.Begin); - var contentSize = stream.Read(buffer, 0, ChunkSize); + var contentSize = stream.Read(buffer, 0, chunkSize); if(contentSize == 0) { @@ -491,12 +496,17 @@ private async Task UploadChunk(Stream stream, ChunkedUplo } private async Task StartChunkedUpload(Stream stream, CancellationToken cancellationToken) + { + return await StartChunkedUpload(stream, ChunkSize, cancellationToken); + } + + public async Task StartChunkedUpload(Stream stream, int chunkSize, CancellationToken cancellationToken) { var request = MakeChunkedUploadPutRequest(0); - var buffer = new byte[ChunkSize]; + var buffer = new byte[chunkSize]; - var contentSize = stream.Read(buffer, 0, ChunkSize); + var contentSize = stream.Read(buffer, 0, chunkSize); HttpContent content = new ByteArrayContent(buffer, 0, contentSize); @@ -557,7 +567,27 @@ public async Task UploadChunked(string path, string filename, Stream s } // Commit the upload - var commitRequest = MakeChunkedUploadCommitRequest(path, filename, uploadId); + return await CommitChunkedUpload(path, filename, null, uploadId, cancellationToken); + } + catch(AggregateException) + { + throw; + } + catch(DropboxException) + { + throw; + } + catch(Exception ex) + { + throw new DropboxException(ex); + } + } + + public async Task CommitChunkedUpload(string path, string filename, string parentRevision, string uploadId, CancellationToken cancellationToken) + { + try + { + var commitRequest = MakeChunkedUploadCommitRequest(path, filename, parentRevision, uploadId); var response = await _httpClient.PostAsync(commitRequest.RequestUri, null, cancellationToken); @@ -570,15 +600,15 @@ public async Task UploadChunked(string path, string filename, Stream s return JsonConvert.DeserializeObject(responseBody); } - catch(AggregateException) + catch (AggregateException) { throw; } - catch(DropboxException) + catch (DropboxException) { throw; } - catch(Exception ex) + catch (Exception ex) { throw new DropboxException(ex); } diff --git a/DropNetRT/Client.Helpers.cs b/DropNetRT/Client.Helpers.cs index d4913b3..0c91717 100644 --- a/DropNetRT/Client.Helpers.cs +++ b/DropNetRT/Client.Helpers.cs @@ -232,7 +232,7 @@ private HttpRequest MakeChunkedUploadPutRequest(long offset, string uploadId = n return request; } - private HttpRequest MakeChunkedUploadCommitRequest(string path, string filename, string uploadId) + private HttpRequest MakeChunkedUploadCommitRequest(string path, string filename, string parentRevision, string uploadId) { var requestUrl = MakeRequestString(string.Format("1/commit_chunked_upload/auto/{0}{2}{1}", path.CleanPath(), filename, path.CleanPath().Length > 0 ? "/" : ""), ApiType.Content); @@ -240,6 +240,9 @@ private HttpRequest MakeChunkedUploadCommitRequest(string path, string filename, request.AddParameter("upload_id", uploadId); + if (!string.IsNullOrEmpty(parentRevision)) + request.AddParameter("parent_rev", parentRevision); + _oauthHandler.Authenticate(request); return request; diff --git a/DropNetRT/Client.cs b/DropNetRT/Client.cs index 72b7c42..c111399 100644 --- a/DropNetRT/Client.cs +++ b/DropNetRT/Client.cs @@ -156,6 +156,15 @@ private async Task SendAsync(HttpRequest request, CancellationToken canc { response = await _httpClient.SendAsync(request, cancellationToken); } + catch (TaskCanceledException ex) + { + // Expiration (cancellation) of the longpoll call is a normal situation, it happens regularly if longpoll set to higher values (480 is the max) and as a part of an expected workflow. Seen from this angle it is not an 'exception'. + // For that reason (and the possibility of a simpler consumer code) we simulate "no changes" reply from the server in this very particular case instead of throwing a generic DropBoxException. + if (request.RequestUri.AbsolutePath.Contains("longpoll")) + return "{\"changes\" : false}"; + + throw new DropboxException(ex); + } catch (Exception ex) { throw new DropboxException(ex); diff --git a/DropNetRT/DropNetRT.csproj b/DropNetRT/DropNetRT.csproj index 5e3dc70..499d131 100644 --- a/DropNetRT/DropNetRT.csproj +++ b/DropNetRT/DropNetRT.csproj @@ -36,6 +36,8 @@ true ..\..\..\Source\ true + + true @@ -110,26 +112,33 @@ ..\packages\Microsoft.Bcl.Async.1.0.168\lib\portable-net40+sl4+win8+wp71+wpa81\Microsoft.Threading.Tasks.Extensions.dll - - ..\packages\Newtonsoft.Json.6.0.4\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.8.0.2\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll + True - - ..\packages\Microsoft.Bcl.1.1.9\lib\portable-net40+sl5+win8+wp8+wpa81\System.IO.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\portable-net40+sl5+win8+wp8+wpa81\System.IO.dll + True - - ..\packages\Microsoft.Net.Http.2.2.22\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.dll + + ..\packages\Microsoft.Net.Http.2.2.29\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.dll + True - - ..\packages\Microsoft.Net.Http.2.2.22\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Extensions.dll + + ..\packages\Microsoft.Net.Http.2.2.29\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Extensions.dll + True - - ..\packages\Microsoft.Net.Http.2.2.22\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Primitives.dll + + ..\packages\Microsoft.Net.Http.2.2.29\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Primitives.dll + True - - ..\packages\Microsoft.Bcl.1.1.9\lib\portable-net40+sl5+win8+wp8+wpa81\System.Runtime.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\portable-net40+sl5+win8+wp8+wpa81\System.Runtime.dll + True - - ..\packages\Microsoft.Bcl.1.1.9\lib\portable-net40+sl5+win8+wp8+wpa81\System.Threading.Tasks.dll + + ..\packages\Microsoft.Bcl.1.1.10\lib\portable-net40+sl5+win8+wp8+wpa81\System.Threading.Tasks.dll + True @@ -148,10 +157,12 @@ - - - - + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + +