Skip to content

Commit 5fad85d

Browse files
fix(FileSystemApi): immediately report progress
1 parent 9d83d5a commit 5fad85d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/CoreApi/FileSystemApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ internal FileSystemApi(IpfsClient ipfs)
6565
opts.Add($"protect={options.ProtectionKey}");
6666
opts.Add($"chunker=size-{options.ChunkSize}");
6767

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

7070
// The result is a stream of LDJSON objects.
7171
// See https://github.com/ipfs/go-ipfs/issues/4852
7272
FileSystemNode fsn = null;
73-
using (var sr = new StringReader(json))
73+
using (var sr = new StreamReader(response))
7474
using (var jr = new JsonTextReader(sr) { SupportMultipleContent = true })
7575
{
7676
while (jr.Read())

src/IpfsClient.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,51 @@ public async Task<String> UploadAsync(string command, CancellationToken cancel,
458458
return json;
459459
}
460460
}
461+
/// <summary>
462+
/// Perform an <see href="https://ipfs.io/docs/api/">IPFS API command</see> that
463+
/// requires uploading of a "file".
464+
/// </summary>
465+
/// <param name="command">
466+
/// The <see href="https://ipfs.io/docs/api/">IPFS API command</see>, such as
467+
/// <see href="https://ipfs.io/docs/api/#apiv0add">"add"</see>.
468+
/// </param>
469+
/// <param name="cancel">
470+
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
471+
/// </param>
472+
/// <param name="data">
473+
/// A <see cref="Stream"/> containing the data to upload.
474+
/// </param>
475+
/// <param name="name">
476+
/// The name associated with the <paramref name="data"/>, can be <b>null</b>.
477+
/// Typically a filename, such as "hello.txt".
478+
/// </param>
479+
/// <param name="options">
480+
/// The optional flags to the command.
481+
/// </param>
482+
/// <returns>
483+
/// A task that represents the asynchronous operation. The task's value is
484+
/// the HTTP response as a <see cref="Stream"/>.
485+
/// </returns>
486+
/// <exception cref="HttpRequestException">
487+
/// When the IPFS server indicates an error.
488+
/// </exception>
489+
public async Task<Stream> Upload2Async(string command, CancellationToken cancel, Stream data, string name, params string[] options)
490+
{
491+
var content = new MultipartFormDataContent();
492+
var streamContent = new StreamContent(data);
493+
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
494+
if (string.IsNullOrEmpty(name))
495+
content.Add(streamContent, "file", unknownFilename);
496+
else
497+
content.Add(streamContent, "file", name);
498+
499+
var url = BuildCommand(command, null, options);
500+
if (log.IsDebugEnabled)
501+
log.Debug("POST " + url.ToString());
502+
var response = await Api().PostAsync(url, content, cancel);
503+
await ThrowOnErrorAsync(response);
504+
return await response.Content.ReadAsStreamAsync();
505+
}
461506

462507
/// <summary>
463508
/// TODO

0 commit comments

Comments
 (0)