Skip to content

Commit 5ad58ce

Browse files
feat(FileSystemApi): add progress reporting
1 parent 88cf587 commit 5ad58ce

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

src/CoreApi/FileSystemApi.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ internal FileSystemApi(IpfsClient ipfs)
5555
opts.Add("only-hash=true");
5656
if (options.Trickle)
5757
opts.Add("trickle=true");
58+
if (options.Progress != null)
59+
opts.Add("progress=true");
5860
if (options.Hash != MultiHash.DefaultAlgorithmName)
5961
opts.Add($"hash=${options.Hash}");
6062
if (options.Encoding != MultiBase.DefaultAlgorithmName)
@@ -74,16 +76,32 @@ internal FileSystemApi(IpfsClient ipfs)
7476
while (jr.Read())
7577
{
7678
var r = await JObject.LoadAsync(jr, cancel);
77-
fsn = new FileSystemNode
79+
80+
// If a progress report.
81+
if (r.ContainsKey("Bytes"))
82+
{
83+
Console.WriteLine("progress");
84+
options.Progress?.Report(new TransferProgress
85+
{
86+
Name = (string)r["Name"],
87+
Bytes = (ulong)r["Bytes"]
88+
});
89+
}
90+
91+
// Else must be an added file.
92+
else
7893
{
79-
Id = (string)r["Hash"],
80-
Size = long.Parse((string)r["Size"]),
81-
IsDirectory = false,
82-
Name = name,
83-
IpfsClient = ipfs
84-
};
85-
if (log.IsDebugEnabled)
86-
log.Debug("added " + fsn.Id + " " + fsn.Name);
94+
fsn = new FileSystemNode
95+
{
96+
Id = (string)r["Hash"],
97+
Size = long.Parse((string)r["Size"]),
98+
IsDirectory = false,
99+
Name = name,
100+
IpfsClient = ipfs
101+
};
102+
if (log.IsDebugEnabled)
103+
log.Debug("added " + fsn.Id + " " + fsn.Name);
104+
}
87105
}
88106
}
89107

src/IpfsHttpClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<PackageReference Include="Ipfs.Core" Version="0.39.0" />
30+
<PackageReference Include="Ipfs.Core" Version="0.40.0" />
3131
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
3232
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'netstandard14'" />
3333
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'net45'" />

test/CoreApi/FileSystemApiTest.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,44 @@ public async Task GetTar_EmptyDirectory()
302302
{
303303
DeleteTemp(temp);
304304
}
305-
}
305+
}
306+
307+
308+
[TestMethod]
309+
public async Task AddFile_WithProgress()
310+
{
311+
var path = Path.GetTempFileName();
312+
File.WriteAllText(path, "hello world");
313+
try
314+
{
315+
var ipfs = TestFixture.Ipfs;
316+
var bytesTransferred = 0UL;
317+
var options = new AddFileOptions
318+
{
319+
Progress = new Progress<TransferProgress>(t =>
320+
{
321+
Console.WriteLine("got it");
322+
bytesTransferred += t.Bytes;
323+
})
324+
};
325+
var result = await ipfs.FileSystem.AddFileAsync(path, options);
326+
Assert.AreEqual("Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD", (string)result.Id);
327+
328+
// Progress reports get posted on another synchronisation context.
329+
var stop = DateTime.Now.AddSeconds(3);
330+
while (DateTime.Now < stop)
331+
{
332+
if (bytesTransferred == 11UL)
333+
break;
334+
await Task.Delay(10);
335+
}
336+
Assert.AreEqual(11UL, bytesTransferred);
337+
}
338+
finally
339+
{
340+
File.Delete(path);
341+
}
342+
}
306343

307344
void DeleteTemp(string temp)
308345
{

0 commit comments

Comments
 (0)