Skip to content

Commit 3c46b7f

Browse files
committed
add builder for arguments to ipfs.add
1 parent b88ac41 commit 3c46b7f

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package io.ipfs.api;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
6+
/*
7+
Example usage:
8+
AddArgs args = AddArgs.Builder.newInstance()
9+
.setInline()
10+
.setCidVersion(1)
11+
.build();
12+
*/
13+
final class AddArgs {
14+
15+
private final Map<String, String> args = new HashMap<>();
16+
17+
public AddArgs(Builder builder)
18+
{
19+
args.putAll(builder.args);
20+
}
21+
@Override
22+
public String toString()
23+
{
24+
List<String> asList = args.entrySet()
25+
.stream()
26+
.sorted(Comparator.comparing(Map.Entry::getKey))
27+
.map(e -> e.getKey() + " = " + e.getValue()).collect(Collectors.toList());
28+
return Arrays.toString(asList.toArray());
29+
}
30+
public String toQueryString()
31+
{
32+
StringBuilder sb = new StringBuilder();
33+
for (Map.Entry<String, String> entry: args.entrySet()) {
34+
sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
35+
}
36+
return sb.length() > 0 ? sb.toString().substring(1) : sb.toString();
37+
}
38+
public static class Builder {
39+
private static final String TRUE = "true";
40+
private final Map<String, String> args = new HashMap<>();
41+
private Builder() {}
42+
public static Builder newInstance()
43+
{
44+
return new Builder();
45+
}
46+
public Builder setQuiet() {
47+
args.put("quiet", TRUE);
48+
return this;
49+
}
50+
public Builder setQuieter() {
51+
args.put("quieter", TRUE);
52+
return this;
53+
}
54+
public Builder setSilent() {
55+
args.put("silent", TRUE);
56+
return this;
57+
}
58+
public Builder setTrickle() {
59+
args.put("trickle", TRUE);
60+
return this;
61+
}
62+
public Builder setOnlyHash() {
63+
args.put("only-hash", TRUE);
64+
return this;
65+
}
66+
public Builder setWrapWithDirectory() {
67+
args.put("wrap-with-directory", TRUE);
68+
return this;
69+
}
70+
public Builder setChunker(String chunker) {
71+
args.put("chunker", chunker);
72+
return this;
73+
}
74+
public Builder setRawLeaves() {
75+
args.put("raw-leaves", TRUE);
76+
return this;
77+
}
78+
public Builder setNocopy() {
79+
args.put("nocopy", TRUE);
80+
return this;
81+
}
82+
public Builder setFscache() {
83+
args.put("fscache", TRUE);
84+
return this;
85+
}
86+
public Builder setCidVersion(int version) {
87+
args.put("cid-version", String.valueOf(version));
88+
return this;
89+
}
90+
public Builder setHash(String hashFunction) {
91+
args.put("hash", hashFunction);
92+
return this;
93+
}
94+
public Builder setInline() {
95+
args.put("inline", TRUE);
96+
return this;
97+
}
98+
public Builder setInlineLimit(int maxBlockSize) {
99+
args.put("inline-limit", String.valueOf(maxBlockSize));
100+
return this;
101+
}
102+
public Builder setPin() {
103+
args.put("pin", TRUE);
104+
return this;
105+
}
106+
public Builder setToFiles(String path) {
107+
args.put("to-files", path);
108+
return this;
109+
}
110+
public AddArgs build()
111+
{
112+
return new AddArgs(this);
113+
}
114+
}
115+
}

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean h
134134
.collect(Collectors.toList());
135135
}
136136

137+
public List<MerkleNode> add(NamedStreamable file, AddArgs args) throws IOException {
138+
return add(Collections.singletonList(file), args);
139+
}
140+
141+
public List<MerkleNode> add(List<NamedStreamable> files, AddArgs args) throws IOException {
142+
Multipart m = new Multipart(protocol + "://" + host + ":" + port + apiVersion + "add?stream-channels=true&"+ args.toQueryString(), "UTF-8");
143+
for (NamedStreamable file: files) {
144+
if (file.isDirectory()) {
145+
m.addSubtree(Paths.get(""), file);
146+
} else
147+
m.addFilePart("file", Paths.get(""), file);
148+
};
149+
String res = m.finish();
150+
return JSONParser.parseStream(res).stream()
151+
.map(x -> MerkleNode.fromJSON((Map<String, Object>) x))
152+
.collect(Collectors.toList());
153+
}
154+
137155
public List<MerkleNode> ls(Multihash hash) throws IOException {
138156
Map reply = retrieveMap("ls?arg=" + hash);
139157
return ((List<Object>) reply.get("Objects"))

src/test/java/io/ipfs/api/APITest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,18 @@ public void testTimeoutOK() throws IOException {
908908
ipfs.cat(Multihash.fromBase58("Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a"));
909909
}
910910

911+
@Test
912+
public void addArgsTest() {
913+
AddArgs args = AddArgs.Builder.newInstance()
914+
.setInline()
915+
.setCidVersion(1)
916+
.build();
917+
String res = args.toString();
918+
Assert.assertTrue("args toString() format", res.equals("[cid-version = 1, inline = true]"));
919+
String queryStr = args.toQueryString();
920+
Assert.assertTrue("args toQueryString() format", queryStr.equals("inline=true&cid-version=1"));
921+
}
922+
911923
// this api is disabled until deployment over IPFS is enabled
912924
public void updateTest() throws IOException {
913925
Object check = ipfs.update.check();

src/test/java/io/ipfs/api/SimpleAddTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ public void testSingle() throws Exception {
4343
Assert.assertEquals(cids.get("index.html"), tree.get(0).hash.toBase58());
4444
}
4545

46+
@Test
47+
public void testAddArgs() throws Exception {
48+
Path path = Paths.get("src/test/resources/html/index.html");
49+
NamedStreamable file = new FileWrapper(path.toFile());
50+
AddArgs args = AddArgs.Builder.newInstance()
51+
.setInline()
52+
.setCidVersion(1)
53+
.build();
54+
List<MerkleNode> tree = ipfs.add(file, args);
55+
56+
Assert.assertEquals(1, tree.size());
57+
Assert.assertEquals("index.html", tree.get(0).name.get());
58+
}
59+
4660
@Test
4761
public void testSingleWrapped() throws Exception {
4862

0 commit comments

Comments
 (0)