Skip to content

Commit 2b6190a

Browse files
committed
support wrapping files in add command
1 parent 2cd94df commit 2b6190a

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

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

+10-12
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,16 @@ public IPFS(String host, int port, String version) {
6666
}
6767
}
6868

69-
public MerkleNode add(NamedStreamable file) throws IOException {
70-
List<MerkleNode> addParts = add(Collections.singletonList(file));
71-
Optional<MerkleNode> sameName = addParts.stream()
72-
.filter(node -> node.name.equals(file.getName()))
73-
.findAny();
74-
if (sameName.isPresent())
75-
return sameName.get();
76-
return addParts.get(0);
77-
}
78-
79-
public List<MerkleNode> add(List<NamedStreamable> files) throws IOException {
80-
Multipart m = new Multipart("http://" + host + ":" + port + version + "add", "UTF-8");
69+
public List<MerkleNode> add(NamedStreamable file) throws IOException {
70+
return add(file, false);
71+
}
72+
73+
public List<MerkleNode> add(NamedStreamable file, boolean wrap) throws IOException {
74+
return add(Collections.singletonList(file), wrap);
75+
}
76+
77+
public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap) throws IOException {
78+
Multipart m = new Multipart("http://" + host + ":" + port + version + "add?w="+wrap, "UTF-8");
8179
for (NamedStreamable file: files) {
8280
if (file.isDirectory()) {
8381
m.addSubtree(Paths.get(""), file);

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

+27-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.junit.*;
88

99
import java.io.*;
10-
import java.net.*;
1110
import java.nio.file.*;
1211
import java.util.*;
1312
import java.util.function.*;
@@ -82,10 +81,30 @@ public void singleFileTest() {
8281
fileTest(file);
8382
}
8483

84+
@Test
85+
public void wrappedSingleFileTest() {
86+
NamedStreamable.ByteArrayWrapper file = new NamedStreamable.ByteArrayWrapper("hello.txt", "G'day world! IPFS rocks!".getBytes());
87+
try {
88+
List<MerkleNode> addParts = ipfs.add(file, true);
89+
MerkleNode filePart = addParts.get(0);
90+
MerkleNode dirPart = addParts.get(1);
91+
byte[] catResult = ipfs.cat(filePart.hash);
92+
byte[] getResult = ipfs.get(filePart.hash);
93+
if (!Arrays.equals(catResult, file.getContents()))
94+
throw new IllegalStateException("Different contents!");
95+
List<Multihash> pinRm = ipfs.pin.rm(dirPart.hash, true);
96+
if (!pinRm.get(0).equals(dirPart.hash))
97+
throw new IllegalStateException("Didn't remove file!");
98+
Object gc = ipfs.repo.gc();
99+
} catch (IOException e) {
100+
throw new RuntimeException(e);
101+
}
102+
}
103+
85104
@Test
86105
public void dirTest() throws IOException {
87106
NamedStreamable.DirWrapper dir = new NamedStreamable.DirWrapper("root", Arrays.asList());
88-
MerkleNode addResult = ipfs.add(dir);
107+
MerkleNode addResult = ipfs.add(dir).get(0);
89108
List<MerkleNode> ls = ipfs.ls(addResult.hash);
90109
Assert.assertTrue(ls.size() > 0);
91110
}
@@ -115,7 +134,8 @@ public void directoryTest() throws IOException {
115134
fout2.flush();
116135
fout2.close();
117136

118-
MerkleNode addResult = ipfs.add(new NamedStreamable.FileWrapper(tmpDir.toFile()));
137+
List<MerkleNode> addParts = ipfs.add(new NamedStreamable.FileWrapper(tmpDir.toFile()));
138+
MerkleNode addResult = addParts.get(addParts.size() - 1);
119139
List<MerkleNode> lsResult = ipfs.ls(addResult.hash);
120140
if (lsResult.size() != 1)
121141
throw new IllegalStateException("Incorrect number of objects in ls!");
@@ -144,7 +164,7 @@ public void hugeFileStreamTest() {
144164
new Random(1).nextBytes(hugeData);
145165
NamedStreamable.ByteArrayWrapper largeFile = new NamedStreamable.ByteArrayWrapper("massive.txt", hugeData);
146166
try {
147-
MerkleNode addResult = ipfs.add(largeFile);
167+
MerkleNode addResult = ipfs.add(largeFile).get(0);
148168
InputStream in = ipfs.catStream(addResult.hash);
149169

150170
byte[] res = new byte[hugeData.length];
@@ -179,7 +199,7 @@ public void hostFileTest() throws IOException {
179199

180200
public void fileTest(NamedStreamable file) {
181201
try {
182-
MerkleNode addResult = ipfs.add(file);
202+
MerkleNode addResult = ipfs.add(file).get(0);
183203
byte[] catResult = ipfs.cat(addResult.hash);
184204
byte[] getResult = ipfs.get(addResult.hash);
185205
if (!Arrays.equals(catResult, file.getContents()))
@@ -196,7 +216,7 @@ public void fileTest(NamedStreamable file) {
196216
@Test
197217
public void pinTest() {
198218
try {
199-
MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes()));
219+
MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes())).get(0);
200220
Multihash hash = file.hash;
201221
Map<Multihash, Object> ls1 = ipfs.pin.ls(IPFS.PinType.all);
202222
boolean pinned = ls1.containsKey(hash);
@@ -220,7 +240,7 @@ public void pinTest() {
220240
@Test
221241
public void pinUpdate() {
222242
try {
223-
MerkleNode child1 = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes()));
243+
MerkleNode child1 = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes())).get(0);
224244
Multihash hashChild1 = child1.hash;
225245
System.out.println("child1: " + hashChild1);
226246

0 commit comments

Comments
 (0)