|
| 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 | +} |
0 commit comments