Skip to content

Commit f55b580

Browse files
feat(pin): get a list of pinned objects
1 parent 1b16dbd commit f55b580

File tree

7 files changed

+234
-0
lines changed

7 files changed

+234
-0
lines changed

src/IpfsApi.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
<Compile Include="Add.cs" />
6565
<Compile Include="IpfsException.cs" />
6666
<Compile Include="MerkleNode.cs" />
67+
<Compile Include="PinMode.cs" />
68+
<Compile Include="PinnedObject.cs" />
69+
<Compile Include="PinnedCollection .cs" />
6770
<Compile Include="TrustedPeerCollection.cs" />
6871
<Compile Include="Id.cs" />
6972
<Compile Include="PeerNode.cs" />

src/IpfsClient.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public IpfsClient()
4545
var version = Assembly.GetExecutingAssembly().GetName().Version;
4646
UserAgent = string.Format("net-ipfs/{0}.{1}", version.Major, version.Minor);
4747
TrustedPeers = new TruestedPeerCollection(this);
48+
PinnedObjects = new PinnedCollection(this);
4849
}
4950

5051
/// <summary>
@@ -83,6 +84,14 @@ public IpfsClient(string host)
8384
/// </remarks>
8485
public TruestedPeerCollection TrustedPeers { get; private set; }
8586

87+
/// <summary>
88+
/// The list of objects that are permanently stored on the local host.
89+
/// </summary>
90+
/// <remarks>
91+
/// This is equilivent to <c>ipfs pin ls</c>.
92+
/// </remarks>
93+
public PinnedCollection PinnedObjects { get; private set; }
94+
8695
Uri BuildCommand(string command, string arg = null, params string[] options)
8796
{
8897
var url = "/api/v0/" + command;

src/PinMode.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Ipfs.Api
8+
{
9+
/// <summary>
10+
/// The method used to pin an IPFS object.
11+
/// </summary>
12+
[Flags]
13+
public enum PinMode
14+
{
15+
/// <summary>
16+
/// Pin the specific object, and indirectly pin all its decendants
17+
/// </summary>
18+
Recursive = 1,
19+
20+
/// <summary>
21+
/// Pin the specific object.
22+
/// </summary>
23+
Direct = 2,
24+
25+
/// <summary>
26+
/// Pinned indirectly by an ancestor (like a refcount)
27+
/// </summary>
28+
Indirect = 4,
29+
30+
/// <summary>
31+
/// Any
32+
/// </summary>
33+
Any = 7
34+
}
35+
}

src/PinnedCollection .cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.IO;
8+
using System.Net;
9+
using Newtonsoft.Json.Linq;
10+
11+
namespace Ipfs.Api
12+
{
13+
/// <summary>
14+
/// A list of pinned object.
15+
/// </summary>
16+
/// <remarks>
17+
/// This is the list of objects that are permanently stored on the local host.
18+
/// <see cref="https://ipfs.io/ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/example#/ipfs/QmThrNbvLj7afQZhxH72m5Nn1qiVn3eMKWFYV49Zp2mv9B/pin/readme.md"/>ipfs pin</see> command.
19+
/// </remarks>
20+
/// <returns>
21+
/// A series of <see cref="PinnedObject"/>.
22+
/// </returns>
23+
public class PinnedCollection : ICollection<PinnedObject>
24+
{
25+
IpfsClient ipfs;
26+
PinnedObject[] pins;
27+
28+
internal PinnedCollection(IpfsClient ipfs)
29+
{
30+
this.ipfs = ipfs;
31+
}
32+
33+
PinnedObject[] Pins
34+
{
35+
get
36+
{
37+
if (pins == null)
38+
{
39+
Refresh();
40+
}
41+
return pins;
42+
}
43+
}
44+
45+
/// <inheritdoc />
46+
public void Add(PinnedObject obj)
47+
{
48+
if (obj == null)
49+
throw new ArgumentNullException();
50+
51+
throw new NotImplementedException();
52+
}
53+
54+
/// <summary>
55+
/// Remove all the trusted peers.
56+
/// </summary>
57+
/// <remarks>
58+
/// Equivalent to <c>ipfs bootstrap rm --all</c>.
59+
/// </remarks>
60+
public void Clear()
61+
{
62+
throw new NotImplementedException();
63+
}
64+
65+
/// <inheritdoc />
66+
public bool Contains(PinnedObject item)
67+
{
68+
return Pins.Contains(item);
69+
}
70+
71+
/// <inheritdoc />
72+
public void CopyTo(PinnedObject[] array, int index)
73+
{
74+
Pins.CopyTo(array, index);
75+
}
76+
77+
/// <inheritdoc />
78+
public int Count
79+
{
80+
get
81+
{
82+
return Pins.Length;
83+
}
84+
}
85+
86+
/// <inheritdoc />
87+
public bool IsReadOnly
88+
{
89+
get { return false; }
90+
}
91+
92+
/// <summary>
93+
/// Remove the trusted peer.
94+
/// </summary>
95+
/// <remarks>
96+
/// Equivalent to <c>ipfs bootstrap rm <i>peer</i></c>.
97+
/// </remarks>
98+
public bool Remove(PinnedObject peer)
99+
{
100+
if (peer == null)
101+
throw new ArgumentNullException();
102+
103+
throw new NotImplementedException();
104+
}
105+
106+
/// <inheritdoc />
107+
public IEnumerator<PinnedObject> GetEnumerator()
108+
{
109+
return ((IEnumerable<PinnedObject>) Pins).GetEnumerator();
110+
}
111+
112+
/// <inheritdoc />
113+
IEnumerator IEnumerable.GetEnumerator()
114+
{
115+
return Pins.GetEnumerator();
116+
}
117+
118+
/// <summary>
119+
/// Ask IPFS for the pinned the objects.
120+
/// </summary>
121+
public void Refresh()
122+
{
123+
var json = ipfs.DoCommand("pin/ls");
124+
var keys = (JObject) (JObject.Parse(json)["Keys"]);
125+
pins = keys
126+
.Properties()
127+
.Select(p => new PinnedObject
128+
{
129+
Id = p.Name,
130+
Mode = (PinMode)Enum.Parse(typeof(PinMode), (string) keys[p.Name]["Type"], true)
131+
})
132+
.ToArray();
133+
}
134+
}
135+
}

src/PinnedObject.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Ipfs.Api
7+
{
8+
/// <summary>
9+
/// An IPFS object that is permanently stored on the local host.
10+
/// </summary>
11+
public class PinnedObject
12+
{
13+
/// <summary>
14+
/// The <see cref="MultiHash"/> of the object.
15+
/// </summary>
16+
public string Id { get; set; }
17+
18+
/// <summary>
19+
/// The method used to pin the object.
20+
/// </summary>
21+
public PinMode Mode { get; set; }
22+
23+
/// <inheritdoc />
24+
public override string ToString()
25+
{
26+
return Id + " " + Mode.ToString();
27+
}
28+
29+
}
30+
}

test/IpfsApiTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="AddTest.cs" />
6969
<Compile Include="MerkleNodeTest.cs" />
7070
<Compile Include="ExceptionAssert.cs" />
71+
<Compile Include="PinTest.cs" />
7172
<Compile Include="TrustedPeersTest.cs" />
7273
<Compile Include="IdTest.cs" />
7374
<Compile Include="VersionTest.cs" />

test/PinTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Ipfs.Api;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.Linq;
5+
6+
namespace Ipfs.Api
7+
{
8+
9+
public partial class IpfsClientTest
10+
{
11+
12+
[TestMethod]
13+
public void Pin_List()
14+
{
15+
var ipfs = new IpfsClient();
16+
Assert.IsNotNull(ipfs.PinnedObjects);
17+
Assert.IsTrue(ipfs.PinnedObjects.Count > 0);
18+
}
19+
20+
}
21+
}

0 commit comments

Comments
 (0)