Skip to content

#186: add possibilty to add more options/arguments when using the htt… #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions src/main/java/io/ipfs/api/IPFS.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.ipfs.api;

import io.ipfs.cid.*;
import io.ipfs.multibase.binary.StringUtils;
import io.ipfs.multihash.Multihash;
import io.ipfs.multiaddr.MultiAddress;

Expand Down Expand Up @@ -107,8 +108,32 @@ public List<MerkleNode> add(NamedStreamable file, boolean wrap, boolean hashOnly
return add(Collections.singletonList(file), wrap, hashOnly);
}

public List<MerkleNode> add(NamedStreamable file, Map<String, Object> arguments) throws IOException {
return add(file, false, arguments);
}

public List<MerkleNode> add(NamedStreamable file, boolean wrap, Map<String, Object> arguments) throws IOException {
return add(file, wrap, false, arguments);
}

public List<MerkleNode> add(NamedStreamable file, boolean wrap, boolean hashOnly, Map<String, Object> arguments) throws IOException {
return add(Collections.singletonList(file), wrap, hashOnly, arguments);
}

public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean hashOnly) throws IOException {
Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "add?stream-channels=true&w="+wrap + "&n="+hashOnly, "UTF-8");
return add(files, wrap, hashOnly, null);
}

public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean hashOnly, Map<String, Object> arguments) throws IOException {
final StringBuilder extraArgsBuilder = new StringBuilder();
Optional.ofNullable(arguments).ifPresent( args -> {
for (Map.Entry<String, Object> entry: args.entrySet()) {
extraArgsBuilder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
});
String extraArgs = extraArgsBuilder.toString().trim();

Multipart m = new Multipart(protocol + "://" + host + ":" + port + version + "add?stream-channels=true&w="+wrap + "&n="+hashOnly + extraArgs, "UTF-8");
for (NamedStreamable file: files) {
if (file.isDirectory()) {
m.addSubtree(Paths.get(""), file);
Expand All @@ -117,8 +142,8 @@ public List<MerkleNode> add(List<NamedStreamable> files, boolean wrap, boolean h
};
String res = m.finish();
return JSONParser.parseStream(res).stream()
.map(x -> MerkleNode.fromJSON((Map<String, Object>) x))
.collect(Collectors.toList());
.map(x -> MerkleNode.fromJSON((Map<String, Object>) x))
.collect(Collectors.toList());
}

public List<MerkleNode> ls(Multihash hash) throws IOException {
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/io/ipfs/api/Options.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.ipfs.api;

/**
* Constant values used to set operation arguments.
*
* See https://docs.ipfs.io/reference/http/api/#api-v0-file-ls
*/
public interface Options {

/** quiet [bool]: Write minimal output. Required: no. **/
public String QUIET = "quiet";
/** quieter [bool]: Write only final hash. Required: no. **/
public String QUIETER = "quieter";
/** silent [bool]: Write no output. Required: no. **/
public String SILENT = "silent";
/** progress [bool]: Stream progress data. Required: no. **/
public String PROGRESS = "progress";
/** trickle [bool]: Use trickle-dag format for dag generation. Required: no. **/
public String TRICKLE = "trickle";
/** only-hash [bool]: Only chunk and hash - do not write to disk. Required: no. **/
public String ONLY_HASH = "only-hash";
/** wrap-with-directory [bool]: Wrap files with a directory object. Required: no. **/
public String WRAP_WITH_DIRECTORY = "wrap-with-directory";
/** chunker [string]: Chunking algorithm, size-[bytes], rabin-[min]-[avg]-[max] or buzhash. Default: size-262144. Required: no. **/
public String CHUNKER = "chunker";
/** pin [bool]: Pin this object when adding. Default: true. Required: no. **/
public String PIN = "pin";
/** raw-leaves [bool]: Use raw blocks for leaf nodes. (experimental). Required: no. **/
public String RAW_LEAVES = "raw-leaves";
/** nocopy [bool]: Add the file using filestore. Implies raw-leaves. (experimental). Required: no. **/
public String NOCOPY = "nocopy";
/** fscache [bool]: Check the filestore for pre-existing blocks. (experimental). Required: no. **/
public String FSCACHE = "fscache";
/** cid-version [int]: CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental). Required: no. **/
public String CID_VERSION = "cid-version";
/** hash [string]: Hash function to use. Implies CIDv1 if not sha2-256. (experimental). Default: sha2-256. Required: no. **/
public String HASH = "hash";
/** inline [bool]: Inline small blocks into CIDs. (experimental). Required: no. **/
public String INLINE = "inline";
/** inline-limit [int]: Maximum block size to inline. (experimental). Default: 32. Required: no. **/
public String INLINE_LIMIT = "inline-limit";

}