Skip to content

Commit

Permalink
fix(FileSystemApi): immediately report progress
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jan 7, 2019
1 parent 9d83d5a commit 5fad85d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/CoreApi/FileSystemApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ internal FileSystemApi(IpfsClient ipfs)
opts.Add($"protect={options.ProtectionKey}");
opts.Add($"chunker=size-{options.ChunkSize}");

var json = await ipfs.UploadAsync("add", cancel, stream, name, opts.ToArray());
var response = await ipfs.Upload2Async("add", cancel, stream, name, opts.ToArray());

// The result is a stream of LDJSON objects.
// See https://github.com/ipfs/go-ipfs/issues/4852
FileSystemNode fsn = null;
using (var sr = new StringReader(json))
using (var sr = new StreamReader(response))
using (var jr = new JsonTextReader(sr) { SupportMultipleContent = true })
{
while (jr.Read())
Expand Down
45 changes: 45 additions & 0 deletions src/IpfsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,51 @@ public async Task<String> UploadAsync(string command, CancellationToken cancel,
return json;
}
}
/// <summary>
/// Perform an <see href="https://ipfs.io/docs/api/">IPFS API command</see> that
/// requires uploading of a "file".
/// </summary>
/// <param name="command">
/// The <see href="https://ipfs.io/docs/api/">IPFS API command</see>, such as
/// <see href="https://ipfs.io/docs/api/#apiv0add">"add"</see>.
/// </param>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
/// <param name="data">
/// A <see cref="Stream"/> containing the data to upload.
/// </param>
/// <param name="name">
/// The name associated with the <paramref name="data"/>, can be <b>null</b>.
/// Typically a filename, such as "hello.txt".
/// </param>
/// <param name="options">
/// The optional flags to the command.
/// </param>
/// <returns>
/// A task that represents the asynchronous operation. The task's value is
/// the HTTP response as a <see cref="Stream"/>.
/// </returns>
/// <exception cref="HttpRequestException">
/// When the IPFS server indicates an error.
/// </exception>
public async Task<Stream> Upload2Async(string command, CancellationToken cancel, Stream data, string name, params string[] options)
{
var content = new MultipartFormDataContent();
var streamContent = new StreamContent(data);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
if (string.IsNullOrEmpty(name))
content.Add(streamContent, "file", unknownFilename);
else
content.Add(streamContent, "file", name);

var url = BuildCommand(command, null, options);
if (log.IsDebugEnabled)
log.Debug("POST " + url.ToString());
var response = await Api().PostAsync(url, content, cancel);
await ThrowOnErrorAsync(response);
return await response.Content.ReadAsStreamAsync();
}

/// <summary>
/// TODO
Expand Down

0 comments on commit 5fad85d

Please sign in to comment.