diff --git a/src/CoreApi/FileSystemApi.cs b/src/CoreApi/FileSystemApi.cs index f9d777e..678e093 100644 --- a/src/CoreApi/FileSystemApi.cs +++ b/src/CoreApi/FileSystemApi.cs @@ -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()) diff --git a/src/IpfsClient.cs b/src/IpfsClient.cs index a7a1fb3..22be202 100644 --- a/src/IpfsClient.cs +++ b/src/IpfsClient.cs @@ -458,6 +458,51 @@ public async Task UploadAsync(string command, CancellationToken cancel, return json; } } + /// + /// Perform an IPFS API command that + /// requires uploading of a "file". + /// + /// + /// The IPFS API command, such as + /// "add". + /// + /// + /// Is used to stop the task. When cancelled, the is raised. + /// + /// + /// A containing the data to upload. + /// + /// + /// The name associated with the , can be null. + /// Typically a filename, such as "hello.txt". + /// + /// + /// The optional flags to the command. + /// + /// + /// A task that represents the asynchronous operation. The task's value is + /// the HTTP response as a . + /// + /// + /// When the IPFS server indicates an error. + /// + public async Task 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(); + } /// /// TODO