Skip to content

Commit

Permalink
Support a non multipart api for block puts
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed Oct 2, 2024
1 parent d9018ae commit b024aea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/peergos/AggregatedMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private static Counter build(String name, String help) {
public static final Counter API_VERSION = build("api_version", "Total calls to version.");
public static final Counter API_BLOCK_GET = build("api_block_get", "Total calls to block/get.");
public static final Counter API_BLOCK_PUT = build("api_block_put", "Total calls to block/put.");
public static final Counter API_BLOCK_PUT_BULK = build("api_block_put_bulk", "Total calls to block/put/bulk.");
public static final Counter API_BLOCK_RM = build("api_block_rm", "Total calls to block/rm.");
public static final Counter API_BLOCK_RM_BULK = build("api_block_rm_bulk", "Total calls to block/rm/bulk.");
public static final Counter API_BLOCK_STAT = build("api_block_stat", "Total calls to block/stat.");
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/peergos/net/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class APIHandler extends Handler {
public static final String VERSION = "version";
public static final String GET = "block/get";
public static final String PUT = "block/put";
public static final String BULK_PUT = "block/put/bulk";
public static final String RM = "block/rm";
public static final String RM_BULK = "block/rm/bulk";
public static final String STAT = "block/stat";
Expand Down Expand Up @@ -160,6 +161,30 @@ public void handleCallToAPI(HttpExchange httpExchange) {
replyJson(httpExchange, JSONParser.toString(res));
break;
}
case BULK_PUT: {
AggregatedMetrics.API_BLOCK_PUT_BULK.inc();
List<String> format = params.get("format");
Optional<String> formatOpt = format !=null && format.size() == 1 ? Optional.of(format.get(0)) : Optional.empty();
if (formatOpt.isEmpty()) {
throw new APIException("argument \"format\" is required");
}
String reqFormat = formatOpt.get().toLowerCase();
int size = Integer.parseInt(httpExchange.getRequestHeaders().get("Content-Length").get(0));

List<byte[]> data = ((CborObject.CborList)CborObject.fromByteArray(httpExchange.getRequestBody().readNBytes(size))).map(b -> ((CborObject.CborByteArray)b).value);
if (data.size() != 1) {
throw new APIException("Multiple input not supported");
}
byte[] block = data.get(0);
if (block.length > maxBlockSize) {
throw new APIException("Block too large");
}
Cid cid = ipfs.blockstore.put(block, Cid.Codec.lookupIPLDName(reqFormat)).join();
Map res = new HashMap<>();
res.put("Hash", cid.toString());
replyJson(httpExchange, JSONParser.toString(res));
break;
}
case RM: { // https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-block-rm
AggregatedMetrics.API_BLOCK_RM.inc();
if (args == null || args.size() != 1) {
Expand Down

0 comments on commit b024aea

Please sign in to comment.