Skip to content

Commit 91c56b9

Browse files
fix(FileSystemNode): can now be serialized
1 parent 37e260f commit 91c56b9

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

src/FileSystemNode.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Runtime.Serialization;
56
using System.Text;
67
using System.Threading.Tasks;
78

89
namespace Ipfs.Api
910
{
1011
/// <inheritdoc />
12+
[DataContract]
1113
public class FileSystemNode : IFileSystemNode
1214
{
1315
IpfsClient ipfsClient;
@@ -20,11 +22,16 @@ public byte[] DataBytes
2022
{
2123
get
2224
{
23-
using (var stream = DataStream)
24-
using (var data = new MemoryStream())
25-
{
26-
stream.CopyTo(data);
27-
return data.ToArray();
25+
using (var stream = DataStream)
26+
{
27+
if (DataStream == null)
28+
return null;
29+
30+
using (var data = new MemoryStream())
31+
{
32+
stream.CopyTo(data);
33+
return data.ToArray();
34+
}
2835
}
2936
}
3037
}
@@ -34,14 +41,16 @@ public Stream DataStream
3441
{
3542
get
3643
{
37-
return IpfsClient.FileSystem.ReadFileAsync(Id).Result;
44+
return IpfsClient?.FileSystem.ReadFileAsync(Id).Result;
3845
}
3946
}
4047

4148
/// <inheritdoc />
49+
[DataMember]
4250
public Cid Id { get; set; }
4351

4452
/// <inheritdoc />
53+
[DataMember]
4554
public IEnumerable<IFileSystemLink> Links {
4655
get
4756
{
@@ -61,6 +70,7 @@ public IEnumerable<IFileSystemLink> Links {
6170
/// This is the size of the file not the raw encoded contents
6271
/// of the block.
6372
/// </value>
73+
[DataMember]
6474
public long Size
6575
{
6676
get
@@ -81,6 +91,7 @@ public long Size
8191
/// <b>true</b> if the link is a directory; Otherwise <b>false</b>,
8292
/// the link is some type of a file.
8393
/// </value>
94+
[DataMember]
8495
public bool IsDirectory
8596
{
8697
get
@@ -97,6 +108,7 @@ public bool IsDirectory
97108
/// <summary>
98109
/// The file name of the IPFS node.
99110
/// </summary>
111+
[DataMember]
100112
public string Name { get; set; }
101113

102114
/// <inheritdoc />

src/IpfsApi.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.30.0" />
30+
<PackageReference Include="Ipfs.Core" Version="0.33.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/FileSystemNodeTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Ipfs.Api;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.Linq;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
using Newtonsoft.Json;
8+
9+
namespace Ipfs.Api
10+
{
11+
12+
[TestClass]
13+
public class FileSystemNodeTest
14+
{
15+
[TestMethod]
16+
public async Task Serialization()
17+
{
18+
var ipfs = TestFixture.Ipfs;
19+
var a = await ipfs.FileSystem.AddTextAsync("hello world");
20+
Assert.AreEqual("Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD", (string)a.Id);
21+
22+
var b = await ipfs.FileSystem.ListFileAsync(a.Id);
23+
var json = JsonConvert.SerializeObject(b);
24+
var c = JsonConvert.DeserializeObject<FileSystemNode>(json);
25+
Assert.AreEqual(b.Id, c.Id);
26+
Assert.AreEqual(b.IsDirectory, c.IsDirectory);
27+
Assert.AreEqual(b.Size, c.Size);
28+
CollectionAssert.AreEqual(b.Links.ToArray(), c.Links.ToArray());
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)