Skip to content

Commit 05c314c

Browse files
authored
Merge pull request #211 from ipfs-shipyard/feat/demos
sample app to demo usage of MFS files API
2 parents cdf5d45 + 540052b commit 05c314c

File tree

4 files changed

+109
-9
lines changed

4 files changed

+109
-9
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -681,18 +681,18 @@ public Map flush(String path) throws IOException {
681681
return retrieveMap("files/flush?arg=" + arg);
682682
}
683683

684-
public Map ls() throws IOException {
685-
return retrieveMap("files/ls");
684+
public List<Map> ls() throws IOException {
685+
return (List<Map>)retrieveMap("files/ls").get("Entries");
686686
}
687687

688-
public Map ls(String path) throws IOException {
688+
public List<Map> ls(String path) throws IOException {
689689
String arg = URLEncoder.encode(path, "UTF-8");
690-
return retrieveMap("files/ls?arg=" + arg);
690+
return (List<Map>)retrieveMap("files/ls?arg=" + arg).get("Entries");
691691
}
692692

693-
public Map ls(String path, boolean longListing, boolean u) throws IOException {
693+
public List<Map> ls(String path, boolean longListing, boolean u) throws IOException {
694694
String arg = URLEncoder.encode(path, "UTF-8");
695-
return retrieveMap("files/ls?arg=" + arg + "&long=" + longListing + "&U=" + u);
695+
return (List<Map>)retrieveMap("files/ls?arg=" + arg + "&long=" + longListing + "&U=" + u).get("Entries");
696696
}
697697

698698
public String mkdir(String path, boolean parents) throws IOException {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
.setParents()
1212
.build();
1313
*/
14-
final class WriteFilesArgs {
14+
final public class WriteFilesArgs {
1515

1616
private final Map<String, String> args = new HashMap<>();
1717

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package io.ipfs.api.demo;
2+
3+
import io.ipfs.api.IPFS;
4+
import io.ipfs.api.NamedStreamable;
5+
import io.ipfs.api.WriteFilesArgs;
6+
import io.ipfs.multiaddr.MultiAddress;
7+
8+
import java.io.IOException;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
/*
13+
From MFS api documentation: https://github.com/ipfs/js-ipfs/blob/master/docs/core-api/FILES.md#the-mutable-files-api
14+
The Mutable File System (MFS) is a virtual file system on top of IPFS that exposes a Unix like API over a virtual directory.
15+
It enables users to write and read from paths without having to worry about updating the graph.
16+
17+
Useful links:
18+
rpc api - https://docs.ipfs.tech/reference/kubo/rpc/#getting-started
19+
proto.school - https://proto.school/mutable-file-system/01
20+
ipfs.tech - https://docs.ipfs.tech/concepts/file-systems/#mutable-file-system-mfs
21+
22+
*/
23+
public class UsageMFSFilesAPI {
24+
25+
public UsageMFSFilesAPI(IPFS ipfsClient) {
26+
try {
27+
run(ipfsClient);
28+
} catch (IOException ioe) {
29+
ioe.printStackTrace();
30+
}
31+
}
32+
private void run(IPFS ipfs) throws IOException {
33+
34+
// remove 'my' directory to clean up from a previous run
35+
ipfs.files.rm("/my", true, true);
36+
37+
// To create a new directory nested under others that don't yet exist, you need to explicitly set the value of parents to true
38+
ipfs.files.mkdir("/my/directory/example", true);
39+
40+
// Check directory status
41+
String directoryPath = "/my/directory/example";
42+
Map exampleDirectory = ipfs.files.stat(directoryPath);
43+
//{Hash=QmV1a2QoUnB9fPzjZd1GunGR53isuhcWWNCS5Bg3mJyv8N, Size=0, CumulativeSize=57, Blocks=1, Type=directory}
44+
45+
// Add a file
46+
String contents = "hello world!";
47+
String filename = "hello.txt";
48+
String filePath = directoryPath + "/" + filename;
49+
NamedStreamable ns = new NamedStreamable.ByteArrayWrapper(filename, contents.getBytes());
50+
ipfs.files.write(filePath, ns, true, true);
51+
52+
// Read contents of a file
53+
String fileContents = new String(ipfs.files.read(filePath));
54+
System.out.println(fileContents);
55+
56+
// Write a file using builder pattern
57+
String ipfsFilename = "ipfs.txt";
58+
String fullIpfsPath = directoryPath + "/" + ipfsFilename;
59+
NamedStreamable ipfsFile = new NamedStreamable.ByteArrayWrapper(ipfsFilename, "ipfs says hello".getBytes());
60+
WriteFilesArgs args = WriteFilesArgs.Builder.newInstance()
61+
.setCreate()
62+
.setParents()
63+
.build();
64+
ipfs.files.write(fullIpfsPath, ipfsFile, args);
65+
66+
// List directory contents
67+
List<Map> ls = ipfs.files.ls(directoryPath);
68+
for(Map entry : ls) {
69+
System.out.println(entry.get("Name"));
70+
}
71+
72+
// Copy file to another directory
73+
String copyDirectoryPath = "/my/copy/";
74+
ipfs.files.cp(filePath, copyDirectoryPath + filename, true);
75+
ls = ipfs.files.ls(copyDirectoryPath);
76+
for(Map entry : ls) {
77+
System.out.println(entry.get("Name"));
78+
}
79+
80+
// Move file to another directory
81+
String duplicateDirectoryPath = "/my/duplicate/";
82+
ipfs.files.mkdir(duplicateDirectoryPath, false);
83+
ipfs.files.mv(copyDirectoryPath + filename, duplicateDirectoryPath + filename);
84+
ls = ipfs.files.ls(duplicateDirectoryPath);
85+
for(Map entry : ls) {
86+
System.out.println(entry.get("Name"));
87+
}
88+
89+
// Remove a directory
90+
ipfs.files.rm(copyDirectoryPath, true, true);
91+
ls = ipfs.files.ls("/my");
92+
for(Map entry : ls) {
93+
System.out.println(entry.get("Name"));
94+
}
95+
}
96+
public static void main(String[] args) {
97+
IPFS ipfsClient = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001"));
98+
new UsageMFSFilesAPI(ipfsClient);
99+
}
100+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public void filesTest() throws IOException {
255255
res = ipfs.files.write(tempPath, tempFile, true, false);
256256
res = ipfs.files.mv(tempPath, "/" + tempFilename);
257257
stat = ipfs.files.stat("/" + tempFilename);
258-
Map lsMap = ipfs.files.ls("/");
259-
Map lsMap2 = ipfs.files.ls("/", true, false);
258+
List<Map> lsMap = ipfs.files.ls("/");
259+
List<Map> lsMap2 = ipfs.files.ls("/", true, false);
260260

261261
String flushFolder = "/filesTest/f/l/u/s/h";
262262
res = ipfs.files.mkdir(flushFolder, true);

0 commit comments

Comments
 (0)