diff --git a/core/api.c b/core/api.c index cf382af..35a6fcb 100644 --- a/core/api.c +++ b/core/api.c @@ -276,8 +276,44 @@ size_t boundary_size(char *str, char *boundary, size_t limit) */ int get_object(char *path, unsigned char **obj, size_t *size) { - // TODO: implement. - return 0; // fail. + FILE* memstream_file = NULL; + char* memstream_char = NULL; + size_t memstream_size = 0; + struct Cid* cid = NULL; + + // convert hash to cid + if ( ipfs_cid_decode_hash_from_base58((unsigned char*)path, strlen(path), &cid) == 0) { + return 0; + } + + // find block + struct HashtableNode* read_node = NULL; + if (!ipfs_exporter_get_node(local_node, cid->hash, cid->hash_length, &read_node)) { + ipfs_cid_free(cid); + return 0; + } + + // open a memory stream + memstream_file = open_memstream(&memstream_char, &memstream_size); + if (memstream_file == NULL) { + libp2p_logger_error("api", "get_object: Unable to open a memory stream.\n"); + ipfs_cid_free(cid); + return 0; + } + + // throw everything (including links) into the memory stream + ifps_exporter_cat_node(read_node, local_node, memstream_file); + + fclose(memstream_file); + + // no longer need these + ipfs_cid_free(cid); + libp2p_hashtable_node_free(read_node); + + *size = memstream_size; + *obj = memstream_char; + + return 1; } /**