66import java .util .stream .*;
77
88public class IPFS {
9-
9+ public enum PinType { all , direct , indirect , recursive }
1010 public List <String > ObjectTemplates = Arrays .asList ("unixfs-dir" );
1111
1212 public final String host ;
@@ -99,17 +99,30 @@ public List<Multihash> local() throws IOException {
9999 /* Pinning an object ensures a local copy of it is kept.
100100 */
101101 class Pin {
102- public Object add (MerkleNode merkleObject ) throws IOException {
103- return retrieveAndParse ("pin/add?stream-channels=true&arg=" + merkleObject .hash );
102+ public List <Multihash > add (Multihash hash ) throws IOException {
103+ return ((List <Object >)((Map )retrieveAndParse ("pin/add?stream-channels=true&arg=" + hash )).get ("Pinned" ))
104+ .stream ()
105+ .map (x -> Multihash .fromBase58 ((String )x ))
106+ .collect (Collectors .toList ());
107+ }
108+
109+ public Map <Multihash , Object > ls () throws IOException {
110+ return ls (PinType .direct );
111+ }
112+
113+ public Map <Multihash , Object > ls (PinType type ) throws IOException {
114+ return ((Map <String , Object >)(((Map )retrieveAndParse ("pin/ls?stream-channels=true&t=" +type .name ())).get ("Keys" ))).entrySet ()
115+ .stream ()
116+ .collect (Collectors .toMap (x -> Multihash .fromBase58 (x .getKey ()), x -> x .getValue ()));
104117 }
105118
106- public Object ls ( ) throws IOException {
107- return retrieveAndParse ( "pin/ls?stream-channels= true" );
119+ public List < Multihash > rm ( Multihash hash ) throws IOException {
120+ return rm ( hash , true );
108121 }
109122
110- public List <MerkleNode > rm (MerkleNode merkleObject , boolean recursive ) throws IOException {
111- Map json = retrieveMap ("pin/rm?stream-channels=true&r=" + recursive + "&arg=" + merkleObject . hash );
112- return ((List <Object >) json .get ("Pinned" )).stream ().map (x -> new MerkleNode ((String ) x )).collect (Collectors .toList ());
123+ public List <Multihash > rm (Multihash hash , boolean recursive ) throws IOException {
124+ Map json = retrieveMap ("pin/rm?stream-channels=true&r=" + recursive + "&arg=" + hash );
125+ return ((List <Object >) json .get ("Pinned" )).stream ().map (x -> Multihash . fromBase58 ((String ) x )).collect (Collectors .toList ());
113126 }
114127 }
115128
@@ -124,8 +137,8 @@ public Object gc() throws IOException {
124137 /* 'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
125138 */
126139 class Block {
127- public byte [] get (MerkleNode merkleObject ) throws IOException {
128- return retrieve ("block/get?stream-channels=true&arg=" + merkleObject . hash );
140+ public byte [] get (Multihash hash ) throws IOException {
141+ return retrieve ("block/get?stream-channels=true&arg=" + hash );
129142 }
130143
131144 public List <MerkleNode > put (List <byte []> data ) throws IOException {
@@ -136,8 +149,8 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
136149 return JSONParser .parseStream (res ).stream ().map (x -> MerkleNode .fromJSON ((Map <String , Object >) x )).collect (Collectors .toList ());
137150 }
138151
139- public Map stat (MerkleNode merkleObject ) throws IOException {
140- return retrieveMap ("block/stat?stream-channels=true&arg=" + merkleObject . hash );
152+ public Map stat (Multihash hash ) throws IOException {
153+ return retrieveMap ("block/stat?stream-channels=true&arg=" + hash );
141154 }
142155 }
143156
@@ -152,23 +165,23 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
152165 return JSONParser .parseStream (res ).stream ().map (x -> MerkleNode .fromJSON ((Map <String , Object >) x )).collect (Collectors .toList ());
153166 }
154167
155- public MerkleNode get (MerkleNode merkleObject ) throws IOException {
156- Map json = retrieveMap ("object/get?stream-channels=true&arg=" + merkleObject . hash );
157- json .put ("Hash" , merkleObject . hash .toBase58 ());
168+ public MerkleNode get (Multihash hash ) throws IOException {
169+ Map json = retrieveMap ("object/get?stream-channels=true&arg=" + hash );
170+ json .put ("Hash" , hash .toBase58 ());
158171 return MerkleNode .fromJSON (json );
159172 }
160173
161- public MerkleNode links (MerkleNode merkleObject ) throws IOException {
162- Map json = retrieveMap ("object/links?stream-channels=true&arg=" + merkleObject . hash );
174+ public MerkleNode links (Multihash hash ) throws IOException {
175+ Map json = retrieveMap ("object/links?stream-channels=true&arg=" + hash );
163176 return MerkleNode .fromJSON (json );
164177 }
165178
166- public Map <String , Object > stat (MerkleNode merkleObject ) throws IOException {
167- return retrieveMap ("object/stat?stream-channels=true&arg=" + merkleObject . hash );
179+ public Map <String , Object > stat (Multihash hash ) throws IOException {
180+ return retrieveMap ("object/stat?stream-channels=true&arg=" + hash );
168181 }
169182
170- public byte [] data (MerkleNode merkleObject ) throws IOException {
171- return retrieve ("object/data?stream-channels=true&arg=" + merkleObject . hash );
183+ public byte [] data (Multihash hash ) throws IOException {
184+ return retrieve ("object/data?stream-channels=true&arg=" + hash );
172185 }
173186
174187 public MerkleNode _new (Optional <String > template ) throws IOException {
@@ -182,16 +195,16 @@ public MerkleNode _new(Optional<String> template) throws IOException {
182195 }
183196
184197 class Name {
185- public Map publish (MerkleNode node ) throws IOException {
186- return publish (Optional .empty (), node );
198+ public Map publish (Multihash hash ) throws IOException {
199+ return publish (Optional .empty (), hash );
187200 }
188201
189- public Map publish (Optional <String > id , MerkleNode node ) throws IOException {
190- return retrieveMap ("name/publish?arg=" + (id .isPresent () ? id +"&arg=" : "" ) + "/ipfs/" +node . hash );
202+ public Map publish (Optional <String > id , Multihash hash ) throws IOException {
203+ return retrieveMap ("name/publish?arg=" + (id .isPresent () ? id +"&arg=" : "" ) + "/ipfs/" +hash );
191204 }
192205
193- public String resolve (Multihash addr ) throws IOException {
194- Map res = (Map ) retrieveAndParse ("name/resolve?arg=" + addr );
206+ public String resolve (Multihash hash ) throws IOException {
207+ Map res = (Map ) retrieveAndParse ("name/resolve?arg=" + hash );
195208 return (String )res .get ("Path" );
196209 }
197210 }
0 commit comments