Skip to content

Commit b12d106

Browse files
committed
added object.new
1 parent edacdb2 commit b12d106

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/org/ipfs/IPFS.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
public class IPFS {
99

10+
public List<String> ObjectTemplates = Arrays.asList("unixfs-dir");
11+
1012
public final String host;
1113
public final int port;
1214
private final String version;
@@ -164,7 +166,14 @@ public byte[] data(MerkleNode merkleObject) throws IOException {
164166
return retrieve("object/data?stream-channels=true&arg=" + merkleObject.hash);
165167
}
166168

167-
// TODO new, patch
169+
public MerkleNode _new(Optional<String> template) throws IOException {
170+
if (template.isPresent() && !ObjectTemplates.contains(template.get()))
171+
throw new IllegalStateException("Unrecognised template: "+template.get());
172+
Map json = retrieveMap("object/new?stream-channels=true"+(template.isPresent() ? "&arg=" + template.get() : ""));
173+
return MerkleNode.fromJSON(json);
174+
}
175+
176+
// TODO patch
168177
}
169178

170179
class Name {

src/org/ipfs/MerkleNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ public static MerkleNode fromJSON(Object rawjson) {
4848
String hash = (String)json.get("Hash");
4949
if (hash == null)
5050
hash = (String)json.get("Key");
51-
Optional<String> name = json.containsKey("Name") ? Optional.of((String)json.get("Name")): Optional.empty();
51+
Optional<String> name = json.containsKey("Name") ? Optional.of((String) json.get("Name")): Optional.empty();
5252
Optional<Integer> size = json.containsKey("Size") ? Optional.<Integer>empty().of((Integer) json.get("Size")): Optional.<Integer>empty().empty();
5353
Optional<Integer> type = json.containsKey("Type") ? Optional.<Integer>empty().of((Integer) json.get("Type")): Optional.<Integer>empty().empty();
54-
List<MerkleNode> links = json.containsKey("Links") ? ((List<Object>)json.get("Links")).stream().map(x -> MerkleNode.fromJSON(x)).collect(Collectors.toList()) : Arrays.asList();
54+
List<Object> linksRaw = (List<Object>) json.get("Links");
55+
List<MerkleNode> links = linksRaw == null ? Collections.EMPTY_LIST : linksRaw.stream().map(x -> MerkleNode.fromJSON(x)).collect(Collectors.toList());
5556
Optional<byte[]> data = json.containsKey("Data") ? Optional.of(((String)json.get("Data")).getBytes()): Optional.empty();
5657
return new MerkleNode(hash, name, size, type, links, data);
5758
}

test/org/ipfs/Test.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public void refsTest() {
6868
@org.junit.Test
6969
public void objectTest() {
7070
try {
71+
MerkleNode _new = ipfs.object._new(Optional.empty());
7172
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
7273
MerkleNode object = ipfs.object.get(pointer);
7374
List<MerkleNode> newPointer = ipfs.object.put(Arrays.asList(object.toJSONString().getBytes()));

0 commit comments

Comments
 (0)