|
| 1 | +package org.ipfsbox.battery.api; |
| 2 | + |
| 3 | +import io.ipfs.cid.*; |
| 4 | +import org.ipfsbox.battery.api.http.Pins; |
| 5 | +import io.ipfs.multihash.Multihash; |
| 6 | +import io.ipfs.multiaddr.MultiAddress; |
| 7 | + |
| 8 | +import java.io.*; |
| 9 | +import java.net.*; |
| 10 | +import java.util.*; |
| 11 | +import java.util.stream.*; |
| 12 | + |
| 13 | +public class IPFSCluster { |
| 14 | + public static final Version MIN_VERSION = Version.parse("0.3.4"); |
| 15 | + public enum PinType {all, direct, indirect, recursive} |
| 16 | + public List<String> ObjectTemplates = Arrays.asList("unixfs-dir"); |
| 17 | + public List<String> ObjectPatchTypes = Arrays.asList("add-link", "rm-link", "set-data", "append-data"); |
| 18 | + |
| 19 | + public final String host; |
| 20 | + public final int port; |
| 21 | + private final String version; |
| 22 | + public final Pin pin = new Pin(); |
| 23 | + public final Peers peers = new Peers(); |
| 24 | + |
| 25 | + /* Pinning an object ensures a local copy of it is kept. |
| 26 | + */ |
| 27 | + public class Pin { |
| 28 | + public List<Multihash> add(Multihash hash) throws IOException { |
| 29 | + return ((List<Object>)((Map)retrieveAndParse("pins/add?stream-channels=true&arg=" + hash)).get("Pins")) |
| 30 | + .stream() |
| 31 | + .map(x -> Cid.decode((String) x)) |
| 32 | + .collect(Collectors.toList()); |
| 33 | + } |
| 34 | + |
| 35 | + public Map<Multihash, Object> ls() throws IOException { |
| 36 | + return ls(PinType.direct); |
| 37 | + } |
| 38 | + |
| 39 | + public Map<Multihash, Object> ls(PinType type) throws IOException { |
| 40 | + return ((Map<String, Object>)(((Map)retrieveAndParse("pins?stream-channels=true&t="+type.name())).get("Keys"))).entrySet() |
| 41 | + .stream() |
| 42 | + .collect(Collectors.toMap(x -> Cid.decode(x.getKey()), x-> x.getValue())); |
| 43 | + } |
| 44 | + |
| 45 | + public List<Multihash> rm(Multihash hash) throws IOException { |
| 46 | + return rm(hash, true); |
| 47 | + } |
| 48 | + |
| 49 | + public List<Multihash> rm(Multihash hash, boolean recursive) throws IOException { |
| 50 | + Map json = retrieveMap("pins/rm?stream-channels=true&r=" + recursive + "&arg=" + hash); |
| 51 | + return ((List<Object>) json.get("Pins")).stream().map(x -> Cid.decode((String) x)).collect(Collectors.toList()); |
| 52 | + } |
| 53 | + } |
| 54 | + public Pins pins(String cid) throws MalformedURLException { |
| 55 | + return new Pins("http://"+host+":"+port+"/pins/"+cid); |
| 56 | + } |
| 57 | + public class Peers { |
| 58 | + public List<Multihash> add(Multihash hash) throws IOException { |
| 59 | + return ((List<Object>)((Map)retrieveAndParse("peers/add?stream-channels=true&arg=" + hash)).get("Pins")) |
| 60 | + .stream() |
| 61 | + .map(x -> Cid.decode((String) x)) |
| 62 | + .collect(Collectors.toList()); |
| 63 | + } |
| 64 | + |
| 65 | + public List<Map<String, Object>> ls() throws IOException { |
| 66 | + List<Map<String, Object>> map = ((List<Map<String, Object>>)retrieveAndParse("peers")); |
| 67 | + return map; |
| 68 | + } |
| 69 | + |
| 70 | +// public Map<Multihash, Object> ls() throws IOException { |
| 71 | +// return ls(PinType.direct); |
| 72 | +// } |
| 73 | + |
| 74 | +// public Map<Multihash, Object> ls(PinType type) throws IOException { |
| 75 | +// return ((Map<String, Object>)(((Map)retrieveAndParse("peers")).get("Keys"))).entrySet() |
| 76 | +// .stream() |
| 77 | +// .collect(Collectors.toMap(x -> Cid.decode(x.getKey()), x-> x.getValue())); |
| 78 | +// } |
| 79 | + |
| 80 | + public List<Multihash> rm(Multihash hash) throws IOException { |
| 81 | + return rm(hash, true); |
| 82 | + } |
| 83 | + |
| 84 | + public List<Multihash> rm(Multihash hash, boolean recursive) throws IOException { |
| 85 | + Map json = retrieveMap("peers/rm?stream-channels=true&r=" + recursive + "&arg=" + hash); |
| 86 | + return ((List<Object>) json.get("Peers")).stream().map(x -> Cid.decode((String) x)).collect(Collectors.toList()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public IPFSCluster(String host, int port) { |
| 91 | + this(host, port, "/api/v0/"); |
| 92 | + } |
| 93 | + |
| 94 | + public IPFSCluster(String multiaddr) { |
| 95 | + this(new MultiAddress(multiaddr)); |
| 96 | + } |
| 97 | + |
| 98 | + public IPFSCluster(MultiAddress addr) { |
| 99 | + this(addr.getHost(), addr.getTCPPort(), "/api/v0/"); |
| 100 | + } |
| 101 | + |
| 102 | + public IPFSCluster(String host, int port, String version) { |
| 103 | + this.host = host; |
| 104 | + this.port = port; |
| 105 | + this.version = version; |
| 106 | + // Check IPFS is sufficiently recent |
| 107 | + try { |
| 108 | + Version detected = Version.parse(version()); |
| 109 | + if (detected.isBefore(MIN_VERSION)) |
| 110 | + throw new IllegalStateException("You need to use a more recent version of IPFSCluster! >= " + MIN_VERSION); |
| 111 | + } catch (IOException e) { |
| 112 | + throw new RuntimeException(e); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public Map id() throws IOException { |
| 117 | + return retrieveMap("id"); |
| 118 | + } |
| 119 | + |
| 120 | + public Map sync() throws IOException { |
| 121 | + return retrieveMap("sync"); |
| 122 | + } |
| 123 | + |
| 124 | + public Map recover() throws IOException { |
| 125 | + return retrieveMap("recover"); |
| 126 | + } |
| 127 | + |
| 128 | + public String version() throws IOException { |
| 129 | + Map m = (Map)retrieveAndParse("version"); |
| 130 | + return (String)m.get("Version"); |
| 131 | + } |
| 132 | + |
| 133 | + private Map retrieveMap(String path) throws IOException { |
| 134 | + return (Map)retrieveAndParse(path); |
| 135 | + } |
| 136 | + |
| 137 | + private Object retrieveAndParse(String path) throws IOException { |
| 138 | + byte[] res = retrieve(path); |
| 139 | + return JSONParser.parse(new String(res)); |
| 140 | + } |
| 141 | + |
| 142 | + private byte[] retrieve(String path) throws IOException { |
| 143 | + URL target = new URL("http", host, port, "/" + path); |
| 144 | + return IPFSCluster.get(target); |
| 145 | + } |
| 146 | + |
| 147 | + private static byte[] get(URL target) throws IOException { |
| 148 | + HttpURLConnection conn = (HttpURLConnection) target.openConnection(); |
| 149 | + conn.setRequestMethod("GET"); |
| 150 | + conn.setRequestProperty("Content-Type", "application/json"); |
| 151 | + |
| 152 | + try { |
| 153 | + InputStream in = conn.getInputStream(); |
| 154 | + ByteArrayOutputStream resp = new ByteArrayOutputStream(); |
| 155 | + |
| 156 | + byte[] buf = new byte[4096]; |
| 157 | + int r; |
| 158 | + while ((r = in.read(buf)) >= 0) |
| 159 | + resp.write(buf, 0, r); |
| 160 | + return resp.toByteArray(); |
| 161 | + } catch (ConnectException e) { |
| 162 | + throw new RuntimeException("Couldn't connect to IPFSCluster daemon at "+target+"\n Is IPFSCluster running?"); |
| 163 | + } catch (IOException e) { |
| 164 | + String err = new String(readFully(conn.getErrorStream())); |
| 165 | + throw new RuntimeException("IOException contacting IPFSCluster daemon.\nTrailer: " + conn.getHeaderFields().get("Trailer") + " " + err, e); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private static final byte[] readFully(InputStream in) throws IOException { |
| 170 | + ByteArrayOutputStream resp = new ByteArrayOutputStream(); |
| 171 | + byte[] buf = new byte[4096]; |
| 172 | + int r; |
| 173 | + while ((r=in.read(buf)) >= 0) |
| 174 | + resp.write(buf, 0, r); |
| 175 | + return resp.toByteArray(); |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | +} |
0 commit comments