Skip to content

Commit c7d62eb

Browse files
committed
Fix regression in multipart refactor
1 parent e897614 commit c7d62eb

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public List<MerkleNode> add(List<NamedStreamable> files) throws IOException {
8080
Multipart m = new Multipart("http://" + host + ":" + port + version + "add", "UTF-8");
8181
for (NamedStreamable file: files) {
8282
if (file.isDirectory()) {
83-
m.addSubtree(Paths.get("/"), file);
83+
m.addSubtree(Paths.get(""), file);
8484
} else
85-
m.addFilePart("file", file);
85+
m.addFilePart("file", Paths.get(""), file);
8686
};
8787
String res = m.finish();
8888
return JSONParser.parseStream(res).stream()
@@ -265,7 +265,7 @@ public List<MerkleNode> put(List<byte[]> data, Optional<String> format) throws I
265265
public MerkleNode put(byte[] data, Optional<String> format) {
266266
String fmt = format.map(f -> "&format=" + f).orElse("");
267267
Multipart m = new Multipart("http://" + host + ":" + port + version+"block/put?stream-channels=true" + fmt, "UTF-8");
268-
m.addFilePart("file", new NamedStreamable.ByteArrayWrapper(data));
268+
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data));
269269
String res = m.finish();
270270
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).findFirst().get();
271271
}
@@ -281,7 +281,7 @@ public class IPFSObject {
281281
public List<MerkleNode> put(List<byte[]> data) throws IOException {
282282
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/put?stream-channels=true", "UTF-8");
283283
for (byte[] f : data)
284-
m.addFilePart("file", new NamedStreamable.ByteArrayWrapper(f));
284+
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
285285
String res = m.finish();
286286
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).collect(Collectors.toList());
287287
}
@@ -291,7 +291,7 @@ public List<MerkleNode> put(String encoding, List<byte[]> data) throws IOExcepti
291291
throw new IllegalArgumentException("Encoding must be json or protobuf");
292292
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/put?stream-channels=true&encoding="+encoding, "UTF-8");
293293
for (byte[] f : data)
294-
m.addFilePart("file", new NamedStreamable.ByteArrayWrapper(f));
294+
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(f));
295295
String res = m.finish();
296296
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).collect(Collectors.toList());
297297
}
@@ -344,7 +344,7 @@ public MerkleNode patch(Multihash base, String command, Optional<byte[]> data, O
344344
if (!data.isPresent())
345345
throw new IllegalStateException("set-data requires data!");
346346
Multipart m = new Multipart("http://" + host + ":" + port + version+"object/patch/"+command+"?arg="+base.toBase58()+"&stream-channels=true", "UTF-8");
347-
m.addFilePart("file", new NamedStreamable.ByteArrayWrapper(data.get()));
347+
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(data.get()));
348348
String res = m.finish();
349349
return MerkleNode.fromJSON(JSONParser.parse(res));
350350

@@ -468,7 +468,7 @@ public MerkleNode put(String inputFormat, byte[] object, String outputFormat) th
468468
block.put(Arrays.asList(object));
469469
String prefix = "http://" + host + ":" + port + version;
470470
Multipart m = new Multipart(prefix + "block/put/?stream-channels=true&input-enc=" + inputFormat + "&f=" + outputFormat, "UTF-8");
471-
m.addFilePart("file", new NamedStreamable.ByteArrayWrapper(object));
471+
m.addFilePart("file", Paths.get(""), new NamedStreamable.ByteArrayWrapper(object));
472472
String res = m.finish();
473473
return MerkleNode.fromJSON(JSONParser.parse(res));
474474
}
@@ -519,7 +519,7 @@ public Map show() throws IOException {
519519

520520
public void replace(NamedStreamable file) throws IOException {
521521
Multipart m = new Multipart("http://" + host + ":" + port + version+"config/replace?stream-channels=true", "UTF-8");
522-
m.addFilePart("file", file);
522+
m.addFilePart("file", Paths.get(""), file);
523523
String res = m.finish();
524524
}
525525

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void addSubtree(Path parentPath, NamedStreamable dir) throws IOException
6060
if (f.isDirectory())
6161
addSubtree(dirPath, f);
6262
else
63-
addFilePart("file", f);
63+
addFilePart("file", dirPath, f);
6464
}
6565
}
6666

@@ -82,8 +82,8 @@ private static String encode(String in) {
8282
}
8383
}
8484

85-
public void addFilePart(String fieldName, NamedStreamable uploadFile) {
86-
Optional<String> fileName = uploadFile.getName().map(n -> encode(n));
85+
public void addFilePart(String fieldName, Path parent, NamedStreamable uploadFile) {
86+
Optional<String> fileName = uploadFile.getName().map(n -> encode(parent.resolve(n).toString()));
8787
writer.append("--" + boundary).append(LINE_FEED);
8888
if (!fileName.isPresent())
8989
writer.append("Content-Disposition: file; name=\"" + fieldName + "\";").append(LINE_FEED);

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

+32-32
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class APITest {
1919

2020
private final IPFS ipfs = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001"));
2121

22-
@org.junit.Test
22+
@Test
2323
public void dag() throws IOException {
2424
byte[] object = "{\"data\":1234}".getBytes();
2525
MerkleNode put = ipfs.dag.put("json", object);
@@ -33,7 +33,7 @@ public void dag() throws IOException {
3333
Assert.assertTrue("Raw data equal", Arrays.equals(object, get));
3434
}
3535

36-
@org.junit.Test
36+
@Test
3737
public void dagCbor() throws IOException {
3838
Map<String, CborObject> tmp = new TreeMap<>();
3939
tmp.put("data", new CborObject.CborByteArray("G'day mate!".getBytes()));
@@ -76,21 +76,21 @@ public void ipldNode() {
7676
Assert.assertTrue("Correct tree", tree.equals(Arrays.asList("/a/b", "/c")));
7777
}
7878

79-
@org.junit.Test
79+
@Test
8080
public void singleFileTest() {
8181
NamedStreamable.ByteArrayWrapper file = new NamedStreamable.ByteArrayWrapper("hello.txt", "G'day world! IPFS rocks!".getBytes());
8282
fileTest(file);
8383
}
8484

85-
@org.junit.Test
85+
@Test
8686
public void dirTest() throws IOException {
8787
NamedStreamable.DirWrapper dir = new NamedStreamable.DirWrapper("root", Arrays.asList());
8888
MerkleNode addResult = ipfs.add(dir);
8989
List<MerkleNode> ls = ipfs.ls(addResult.hash);
9090
Assert.assertTrue(ls.size() > 0);
9191
}
9292

93-
@org.junit.Test
93+
@Test
9494
public void directoryTest() throws IOException {
9595
Random rnd = new Random();
9696
String dirName = "folder" + rnd.nextInt(100);
@@ -130,15 +130,15 @@ public void directoryTest() throws IOException {
130130
throw new IllegalStateException("Different contents!");
131131
}
132132

133-
// @org.junit.Test
133+
// @Test
134134
public void largeFileTest() {
135135
byte[] largerData = new byte[100*1024*1024];
136136
new Random(1).nextBytes(largerData);
137137
NamedStreamable.ByteArrayWrapper largeFile = new NamedStreamable.ByteArrayWrapper("nontrivial.txt", largerData);
138138
fileTest(largeFile);
139139
}
140140

141-
// @org.junit.Test
141+
// @Test
142142
public void hugeFileStreamTest() {
143143
byte[] hugeData = new byte[1000*1024*1024];
144144
new Random(1).nextBytes(hugeData);
@@ -166,7 +166,7 @@ public void hugeFileStreamTest() {
166166
}
167167
}
168168

169-
@org.junit.Test
169+
@Test
170170
public void hostFileTest() throws IOException {
171171
Path tempFile = Files.createTempFile("IPFS", "tmp");
172172
BufferedWriter w = new BufferedWriter(new FileWriter(tempFile.toFile()));
@@ -193,7 +193,7 @@ public void fileTest(NamedStreamable file) {
193193
}
194194
}
195195

196-
@org.junit.Test
196+
@Test
197197
public void pinTest() {
198198
try {
199199
MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes()));
@@ -217,7 +217,7 @@ public void pinTest() {
217217
}
218218
}
219219

220-
@org.junit.Test
220+
@Test
221221
public void pinUpdate() {
222222
try {
223223
MerkleNode child1 = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes()));
@@ -248,7 +248,7 @@ public void pinUpdate() {
248248
}
249249
}
250250

251-
@org.junit.Test
251+
@Test
252252
public void rawLeafNodePinUpdate() {
253253
try {
254254
MerkleNode child1 = ipfs.block.put("some data".getBytes(), Optional.of("raw"));
@@ -275,7 +275,7 @@ public void rawLeafNodePinUpdate() {
275275
}
276276
}
277277

278-
@org.junit.Test
278+
@Test
279279
public void indirectPinTest() {
280280
try {
281281
Multihash EMPTY = ipfs.object._new(Optional.empty()).hash;
@@ -306,7 +306,7 @@ public void indirectPinTest() {
306306
}
307307
}
308308

309-
@org.junit.Test
309+
@Test
310310
public void objectPatch() {
311311
try {
312312
MerkleNode obj = ipfs.object._new(Optional.empty());
@@ -343,7 +343,7 @@ public void objectPatch() {
343343
}
344344
}
345345

346-
@org.junit.Test
346+
@Test
347347
public void refsTest() {
348348
try {
349349
List<Multihash> local = ipfs.refs.local();
@@ -355,7 +355,7 @@ public void refsTest() {
355355
}
356356
}
357357

358-
@org.junit.Test
358+
@Test
359359
public void objectTest() {
360360
try {
361361
MerkleNode _new = ipfs.object._new(Optional.empty());
@@ -371,7 +371,7 @@ public void objectTest() {
371371
}
372372
}
373373

374-
@org.junit.Test
374+
@Test
375375
public void blockTest() {
376376
try {
377377
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
@@ -383,7 +383,7 @@ public void blockTest() {
383383
}
384384
}
385385

386-
@org.junit.Test
386+
@Test
387387
public void bulkBlockTest() {
388388
try {
389389
CborObject cbor = new CborObject.CborString("G'day IPFS!");
@@ -421,7 +421,7 @@ private static String toEscapedHex(byte[] in) throws IOException {
421421
/**
422422
* Test that merkle links in values of a cbor map are followed during recursive pins
423423
*/
424-
@org.junit.Test
424+
@Test
425425
public void merkleLinkInMap() {
426426
try {
427427
Random r = new Random();
@@ -457,7 +457,7 @@ public void merkleLinkInMap() {
457457
}
458458
}
459459

460-
@org.junit.Test
460+
@Test
461461
public void recursiveRefs() {
462462
try {
463463
CborObject.CborByteArray leaf1 = new CborObject.CborByteArray(("G'day IPFS!").getBytes());
@@ -498,7 +498,7 @@ public void recursiveRefs() {
498498
/**
499499
* Test that merkle links as a root object are followed during recursive pins
500500
*/
501-
@org.junit.Test
501+
@Test
502502
public void rootMerkleLink() {
503503
try {
504504
Random r = new Random();
@@ -531,9 +531,9 @@ public void rootMerkleLink() {
531531
}
532532

533533
/**
534-
* Test that merkle links as a root object are followed during recursive pins
534+
* Test that a cbor null is allowed as an object root
535535
*/
536-
@org.junit.Test
536+
@Test
537537
public void rootNull() {
538538
try {
539539
CborObject.CborNull cbor = new CborObject.CborNull();
@@ -557,7 +557,7 @@ public void rootNull() {
557557
/**
558558
* Test that merkle links in a cbor list are followed during recursive pins
559559
*/
560-
@org.junit.Test
560+
@Test
561561
public void merkleLinkInList() {
562562
try {
563563
Random r = new Random();
@@ -585,7 +585,7 @@ public void merkleLinkInList() {
585585
}
586586
}
587587

588-
@org.junit.Test
588+
@Test
589589
public void fileContentsTest() {
590590
try {
591591
ipfs.repo.gc();
@@ -601,7 +601,7 @@ public void fileContentsTest() {
601601
}
602602
}
603603

604-
@org.junit.Test
604+
@Test
605605
public void nameTest() {
606606
try {
607607
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
@@ -615,7 +615,7 @@ public void nameTest() {
615615
}
616616
}
617617

618-
@org.junit.Test
618+
@Test
619619
public void dnsTest() {
620620
try {
621621
String domain = "ipfs.io";
@@ -633,7 +633,7 @@ public void mountTest() {
633633
}
634634
}
635635

636-
@org.junit.Test
636+
@Test
637637
public void dhtTest() {
638638
try {
639639
Multihash pointer = Multihash.fromBase58("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
@@ -648,7 +648,7 @@ public void dhtTest() {
648648
}
649649
}
650650

651-
@org.junit.Test
651+
@Test
652652
public void statsTest() {
653653
try {
654654
Map stats = ipfs.stats.bw();
@@ -666,7 +666,7 @@ public void resolveTest() {
666666
}
667667
}
668668

669-
@org.junit.Test
669+
@Test
670670
public void swarmTest() {
671671
try {
672672
String multiaddr = "/ip4/127.0.0.1/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ";
@@ -695,7 +695,7 @@ public void swarmTest() {
695695
}
696696
}
697697

698-
@org.junit.Test
698+
@Test
699699
public void bootstrapTest() {
700700
try {
701701
List<MultiAddress> bootstrap = ipfs.bootstrap.list();
@@ -708,7 +708,7 @@ public void bootstrapTest() {
708708
}
709709
}
710710

711-
@org.junit.Test
711+
@Test
712712
public void diagTest() {
713713
try {
714714
Map config = ipfs.config.show();
@@ -723,7 +723,7 @@ public void diagTest() {
723723
}
724724
}
725725

726-
@org.junit.Test
726+
@Test
727727
public void toolsTest() {
728728
try {
729729
String version = ipfs.version();

0 commit comments

Comments
 (0)