Skip to content

Commit 77ef5cf

Browse files
committed
attempt at Files API
1 parent 4cfe5d7 commit 77ef5cf

File tree

2 files changed

+134
-2
lines changed

2 files changed

+134
-2
lines changed

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public enum PinType {all, direct, indirect, recursive}
4242
public final Update update = new Update();
4343
public final DHT dht = new DHT();
4444
public final File file = new File();
45+
public final Files files = new Files();
4546
public final Stats stats = new Stats();
4647
public final Name name = new Name();
4748
public final Pubsub pubsub = new Pubsub();
@@ -477,8 +478,93 @@ public Map ls(Multihash path) throws IOException {
477478
}
478479
}
479480

480-
// Network commands
481+
public class Files {
482+
483+
public String chcid() throws IOException {
484+
return retrieveString("files/chcid");
485+
}
486+
487+
public String chcid(String path) throws IOException {
488+
String arg = URLEncoder.encode(path, "UTF-8");
489+
return retrieveString("files/chcid?args=" + arg);
490+
}
491+
492+
public String cp(String source, String dest, boolean parents) throws IOException {
493+
return retrieveString("files/cp?arg=" + URLEncoder.encode(source, "UTF-8") + "&arg=" + URLEncoder.encode(dest, "UTF-8") + "&parents=" + parents);
494+
}
495+
496+
public Map flush() throws IOException {
497+
return retrieveMap("files/flush");
498+
}
499+
500+
public Map flush(String path) throws IOException {
501+
String arg = URLEncoder.encode(path, "UTF-8");
502+
return retrieveMap("files/flush?arg=" + arg);
503+
}
504+
505+
public Map ls() throws IOException {
506+
return retrieveMap("files/ls");
507+
}
508+
509+
public Map ls(String path) throws IOException {
510+
String arg = URLEncoder.encode(path, "UTF-8");
511+
return retrieveMap("files/ls?arg=" + arg);
512+
}
481513

514+
public Map ls(String path, boolean longListing, boolean u) throws IOException {
515+
String arg = URLEncoder.encode(path, "UTF-8");
516+
return retrieveMap("files/ls?arg=" + arg + "&long=" + longListing + "&U=" + u);
517+
}
518+
519+
public String mkdir(String path, boolean parents) throws IOException {
520+
String arg = URLEncoder.encode(path, "UTF-8");
521+
return retrieveString("files/mkdir?arg=" + arg + "&parents=" + parents);
522+
}
523+
524+
public String mkdir(String path, boolean parents, int cidVersion, Multihash hash) throws IOException {
525+
String arg = URLEncoder.encode(path, "UTF-8");
526+
return retrieveString("files/mkdir?arg=" + arg + "&parents=" + parents + "&cid-version=" + cidVersion + "&hash=" + hash);
527+
}
528+
529+
public String mv(String source, String dest) throws IOException {
530+
return retrieveString("files/mv?arg=" + URLEncoder.encode(source, "UTF-8") + "&arg=" + URLEncoder.encode(dest, "UTF-8"));
531+
}
532+
533+
public byte[] read(String path) throws IOException {
534+
String arg = URLEncoder.encode(path, "UTF-8");
535+
return retrieve("files/read?arg=" + arg);
536+
}
537+
538+
public byte[] read(String path, int offset, int count) throws IOException {
539+
String arg = URLEncoder.encode(path, "UTF-8");
540+
return retrieve("files/read?arg=" + arg + "&offset=" + offset + "&count=" + count);
541+
}
542+
543+
public String rm(String path, boolean recursive, boolean force) throws IOException {
544+
String arg = URLEncoder.encode(path, "UTF-8");
545+
return retrieveString("files/rm?arg=" + arg + "&recursive=" + recursive + "&force=" + force);
546+
}
547+
548+
public Map stat(String path) throws IOException {
549+
String arg = URLEncoder.encode(path, "UTF-8");
550+
return retrieveMap("files/stat?arg=" + arg);
551+
}
552+
553+
public String write(String path, NamedStreamable uploadFile, boolean create, boolean parents) throws IOException {
554+
String arg = URLEncoder.encode(path, "UTF-8");
555+
String rpcParams = "files/write?arg=" + arg + "&create=" + create + "&parents=" + parents;
556+
URL target = new URL(protocol,host,port,version + rpcParams);
557+
Multipart m = new Multipart(target.toString(),"UTF-8");
558+
if (uploadFile.isDirectory()) {
559+
throw new IllegalArgumentException("Input must be a file");
560+
} else {
561+
m.addFilePart("file", Paths.get(""), uploadFile);
562+
}
563+
return m.finish();
564+
}
565+
}
566+
567+
// Network commands
482568
public List<MultiAddress> bootstrap() throws IOException {
483569
return ((List<String>)retrieveMap("bootstrap/").get("Peers"))
484570
.stream()
@@ -693,6 +779,11 @@ private void retrieveAndParseStream(String path, Consumer<Object> results, Consu
693779
getObjectStream(retrieveStream(path), d -> results.accept(JSONParser.parse(new String(d))), err);
694780
}
695781

782+
private String retrieveString(String path) throws IOException {
783+
URL target = new URL(protocol, host, port, version + path);
784+
return new String(IPFS.get(target, connectTimeoutMillis, readTimeoutMillis));
785+
}
786+
696787
private byte[] retrieve(String path) throws IOException {
697788
URL target = new URL(protocol, host, port, version + path);
698789
return IPFS.get(target, connectTimeoutMillis, readTimeoutMillis);

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,48 @@ public void fileTest(NamedStreamable file) throws IOException{
215215
throw new IllegalStateException("Didn't remove file!");
216216
Object gc = ipfs.repo.gc();
217217
}
218-
218+
@Test
219+
public void filesTest() throws IOException {
220+
221+
ipfs.files.rm("/filesTest", true, true);
222+
String filename = "hello.txt";
223+
String folder = "/filesTest/one/two";
224+
String path = folder + "/" + filename;
225+
String contents = "hello world!";
226+
NamedStreamable ns = new NamedStreamable.ByteArrayWrapper(filename, contents.getBytes());
227+
String res = ipfs.files.write(path, ns, true, true);
228+
Map stat = ipfs.files.stat( path);
229+
String readContents = new String(ipfs.files.read(path));
230+
Assert.assertTrue("Should be equals", contents.equals(readContents));
231+
res = ipfs.files.rm(path, false, false);
232+
233+
String tempFilename = "temp.txt";
234+
String tempFolder = "/filesTest/a/b/c";
235+
String tempPath = tempFolder + "/" + tempFilename;
236+
String mkdir = ipfs.files.mkdir(tempFolder, true);
237+
stat = ipfs.files.stat(tempFolder);
238+
NamedStreamable tempFile = new NamedStreamable.ByteArrayWrapper(tempFilename, contents.getBytes());
239+
res = ipfs.files.write(tempPath, tempFile, true, false);
240+
res = ipfs.files.mv(tempPath, "/" + tempFilename);
241+
stat = ipfs.files.stat("/" + tempFilename);
242+
Map lsMap = ipfs.files.ls("/");
243+
244+
String flushFolder = "/filesTest/f/l/u/s/h";
245+
res = ipfs.files.mkdir(flushFolder, true);
246+
Map flushMap = ipfs.files.flush(flushFolder);
247+
248+
String copyFilename = "copy.txt";
249+
String copyFromFolder = "/filesTest/fromThere";
250+
String copyToFolder = "/filesTest/toHere";
251+
String copyFromPath = copyFromFolder + "/" + copyFilename;
252+
String copyToPath = copyToFolder + "/" + copyFilename;
253+
NamedStreamable copyFile = new NamedStreamable.ByteArrayWrapper(copyFilename, "copy".getBytes());
254+
res = ipfs.files.write(copyFromPath, copyFile, true, true);
255+
res = ipfs.files.cp(copyFromPath, copyToPath, true);
256+
stat = ipfs.files.stat(copyToPath);
257+
String cid = ipfs.files.chcid(copyToPath);
258+
ipfs.files.rm("/filesTest", true, true);
259+
}
219260
@Test
220261
public void pinTest() throws IOException {
221262
MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes())).get(0);

0 commit comments

Comments
 (0)