6
6
import java .util .stream .*;
7
7
8
8
public class IPFS {
9
-
9
+ public enum PinType { all , direct , indirect , recursive }
10
10
public List <String > ObjectTemplates = Arrays .asList ("unixfs-dir" );
11
11
12
12
public final String host ;
@@ -99,17 +99,30 @@ public List<Multihash> local() throws IOException {
99
99
/* Pinning an object ensures a local copy of it is kept.
100
100
*/
101
101
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 ()));
104
117
}
105
118
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 );
108
121
}
109
122
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 ());
113
126
}
114
127
}
115
128
@@ -124,8 +137,8 @@ public Object gc() throws IOException {
124
137
/* 'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
125
138
*/
126
139
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 );
129
142
}
130
143
131
144
public List <MerkleNode > put (List <byte []> data ) throws IOException {
@@ -136,8 +149,8 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
136
149
return JSONParser .parseStream (res ).stream ().map (x -> MerkleNode .fromJSON ((Map <String , Object >) x )).collect (Collectors .toList ());
137
150
}
138
151
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 );
141
154
}
142
155
}
143
156
@@ -152,23 +165,23 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
152
165
return JSONParser .parseStream (res ).stream ().map (x -> MerkleNode .fromJSON ((Map <String , Object >) x )).collect (Collectors .toList ());
153
166
}
154
167
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 ());
158
171
return MerkleNode .fromJSON (json );
159
172
}
160
173
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 );
163
176
return MerkleNode .fromJSON (json );
164
177
}
165
178
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 );
168
181
}
169
182
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 );
172
185
}
173
186
174
187
public MerkleNode _new (Optional <String > template ) throws IOException {
@@ -182,16 +195,16 @@ public MerkleNode _new(Optional<String> template) throws IOException {
182
195
}
183
196
184
197
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 );
187
200
}
188
201
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 );
191
204
}
192
205
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 );
195
208
return (String )res .get ("Path" );
196
209
}
197
210
}
0 commit comments