Skip to content

Commit

Permalink
feat(FileSystemApi): add progress reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jan 4, 2019
1 parent 88cf587 commit 5ad58ce
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
36 changes: 27 additions & 9 deletions src/CoreApi/FileSystemApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ internal FileSystemApi(IpfsClient ipfs)
opts.Add("only-hash=true");
if (options.Trickle)
opts.Add("trickle=true");
if (options.Progress != null)
opts.Add("progress=true");
if (options.Hash != MultiHash.DefaultAlgorithmName)
opts.Add($"hash=${options.Hash}");
if (options.Encoding != MultiBase.DefaultAlgorithmName)
Expand All @@ -74,16 +76,32 @@ internal FileSystemApi(IpfsClient ipfs)
while (jr.Read())
{
var r = await JObject.LoadAsync(jr, cancel);
fsn = new FileSystemNode

// If a progress report.
if (r.ContainsKey("Bytes"))
{
Console.WriteLine("progress");
options.Progress?.Report(new TransferProgress
{
Name = (string)r["Name"],
Bytes = (ulong)r["Bytes"]
});
}

// Else must be an added file.
else
{
Id = (string)r["Hash"],
Size = long.Parse((string)r["Size"]),
IsDirectory = false,
Name = name,
IpfsClient = ipfs
};
if (log.IsDebugEnabled)
log.Debug("added " + fsn.Id + " " + fsn.Name);
fsn = new FileSystemNode
{
Id = (string)r["Hash"],
Size = long.Parse((string)r["Size"]),
IsDirectory = false,
Name = name,
IpfsClient = ipfs
};
if (log.IsDebugEnabled)
log.Debug("added " + fsn.Id + " " + fsn.Name);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/IpfsHttpClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ipfs.Core" Version="0.39.0" />
<PackageReference Include="Ipfs.Core" Version="0.40.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'netstandard14'" />
<PackageReference Include="System.Net.Http" Version="4.3.3" Condition="'$(TargetFramework)' == 'net45'" />
Expand Down
39 changes: 38 additions & 1 deletion test/CoreApi/FileSystemApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,44 @@ public async Task GetTar_EmptyDirectory()
{
DeleteTemp(temp);
}
}
}


[TestMethod]
public async Task AddFile_WithProgress()
{
var path = Path.GetTempFileName();
File.WriteAllText(path, "hello world");
try
{
var ipfs = TestFixture.Ipfs;
var bytesTransferred = 0UL;
var options = new AddFileOptions
{
Progress = new Progress<TransferProgress>(t =>
{
Console.WriteLine("got it");
bytesTransferred += t.Bytes;
})
};
var result = await ipfs.FileSystem.AddFileAsync(path, options);
Assert.AreEqual("Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD", (string)result.Id);

// Progress reports get posted on another synchronisation context.
var stop = DateTime.Now.AddSeconds(3);
while (DateTime.Now < stop)
{
if (bytesTransferred == 11UL)
break;
await Task.Delay(10);
}
Assert.AreEqual(11UL, bytesTransferred);
}
finally
{
File.Delete(path);
}
}

void DeleteTemp(string temp)
{
Expand Down

0 comments on commit 5ad58ce

Please sign in to comment.