Skip to content

Commit

Permalink
Fixes #113, allow client-side generation of ReGrid file ids. (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez authored May 5, 2017
1 parent 8982bb1 commit 1affa5d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v2.3.19
* Issue 113: Allow client-side generation of ReGrid file ids.

## v2.3.18-beta-1
* Use some C# 7 features in AST composition for faster performance.
* Issue 112: Fixed `NotSupportedException` when connecting to an IPv6 address.
Expand Down
31 changes: 31 additions & 0 deletions Source/RethinkDb.Driver.ReGrid.Tests/UploadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,37 @@ public void upload_files_to_different_paths_and_revisions()
TestFiles.DifferentPathsAndRevisions(this.bucket);
}



[Test]
public void upload_file_with_client_generated_fileid()
{
var guid = new Guid("BC2B30C2-6780-4ABC-A033-8F2396BA0846");
var forcedGuid = this.bucket.Upload(testfile, TestBytes.OneHalfChunk, new UploadOptions
{
ForceFileId = guid
});

forcedGuid.Should().Be(guid);
}

[Test]
public void uploading_to_the_same_file_id_twice_should_fail()
{
var guid = new Guid("BC2B30C2-6780-4ABC-A033-8F2396BA0846");
var opts = new UploadOptions
{
ForceFileId = guid
};
var forcedGuid = this.bucket.Upload(testfile, TestBytes.OneHalfChunk, opts);

forcedGuid.Should().Be(guid);

Action act = () => this.bucket.Upload(testfile, TestBytes.OneHalfChunk, opts);

act.ShouldThrow<ReqlAssertFailure>();
}

//DEBUG INDEXES:
//r.db("query").table("fs_files").map(function(x) { return x('filename').split("/").slice(0, -1); })

Expand Down
6 changes: 5 additions & 1 deletion Source/RethinkDb.Driver.ReGrid/Bucket.Upload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private async Task<UploadStream> CreateUploadStreamAsync(string filename, Upload

var fileInfo = new FileInfo()
{
Id = options.ForceFileId,
Status = Status.Incomplete,
FileName = filename.SafePath(),
StartedAtDate = DateTimeOffset.UtcNow,
Expand All @@ -179,9 +180,12 @@ private async Task<UploadStream> CreateUploadStreamAsync(string filename, Upload
var result = await fileTable.Insert(fileInfo).RunResultAsync(conn, cancelToken)
.ConfigureAwait(false);

result.AssertNoErrors();
result.AssertInserted(1);

var fileInfoId = result.GeneratedKeys[0];
//Return the real ID for the FileInfo, either generated by RethinkDB
//or specified by the caller.
var fileInfoId = result.GeneratedKeys?[0] ?? options.ForceFileId;

return new UploadStream(this.conn, fileInfoId, fileInfo, this.fileTable, this.chunkTable, options);
}
Expand Down
9 changes: 9 additions & 0 deletions Source/RethinkDb.Driver.ReGrid/UploadOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Newtonsoft.Json.Linq;
using RethinkDb.Driver.Net;

Expand Down Expand Up @@ -47,6 +48,14 @@ public UploadOptions()
/// </summary>
public JObject Metadata { get; set; }

/// <summary>
/// Set a predetermined (client-side generated) identifier
/// that can used to uniquely reference the file in ReGrid.
/// An exception will be thrown if a duplicate identifier
/// is encountered when creating the upload session.
/// </summary>
public Guid ForceFileId { get; set; }

/// <summary>
/// Sets the metadata JObject with pseudo-type conversion.
/// </summary>
Expand Down
5 changes: 1 addition & 4 deletions Source/RethinkDb.Driver.ReGrid/UploadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,7 @@ protected override void Dispose(bool disposing)

if( disposing )
{
if( sha256 != null )
{
sha256.Dispose();
}
sha256?.Dispose();
}
}

Expand Down
8 changes: 4 additions & 4 deletions Source/RethinkDb.Driver/Utils/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ static TaskHelper()
CompletedResponse = Task.FromResult<Response>(null);
}

public static Task CompletedTask { get; private set; }
public static Task<bool> CompletedTaskTrue { get; private set; }
public static Task<bool> CompletedTaskFalse { get; private set; }
public static Task<Response> CompletedResponse { get; private set; }
public static Task CompletedTask { get; }
public static Task<bool> CompletedTaskTrue { get; }
public static Task<bool> CompletedTaskFalse { get; }
public static Task<Response> CompletedResponse { get; }
}
}

0 comments on commit 1affa5d

Please sign in to comment.