2020-12-06 16:15:31 -08:00
|
|
|
#include "ipfs.h"
|
|
|
|
|
2021-03-27 10:53:19 -07:00
|
|
|
/**
|
|
|
|
* \brief IPFS Contructor, connect to IPFS
|
2021-04-02 17:13:50 -07:00
|
|
|
* \param host IPFS host (eg. localhost)
|
|
|
|
* \param port IPFS port number (5001)
|
|
|
|
* \param timeout IPFS time-out (which is a string, eg. "6s" for 6 seconds)
|
2021-03-27 10:53:19 -07:00
|
|
|
*/
|
2022-01-21 15:39:53 -08:00
|
|
|
IPFS::IPFS(const std::string& host, int port, const std::string& timeout)
|
2022-05-25 14:22:16 -07:00
|
|
|
: host_(host),
|
|
|
|
port_(port),
|
|
|
|
timeout_(timeout),
|
|
|
|
client_(this->host_, this->port_, this->timeout_)
|
2022-01-21 15:39:53 -08:00
|
|
|
{
|
|
|
|
}
|
2021-03-27 10:53:19 -07:00
|
|
|
|
2021-03-27 16:50:04 -07:00
|
|
|
/**
|
2022-01-27 14:25:48 -08:00
|
|
|
* \brief Get the number of IPFS peers.
|
2022-01-21 15:39:53 -08:00
|
|
|
* \return number of peers as size_t
|
2021-03-27 16:50:04 -07:00
|
|
|
*/
|
2022-05-25 14:22:16 -07:00
|
|
|
std::size_t IPFS::get_nr_peers()
|
2020-12-06 16:15:31 -08:00
|
|
|
{
|
2022-01-21 15:39:53 -08:00
|
|
|
ipfs::Json peers;
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.SwarmPeers(&peers);
|
2022-01-21 15:39:53 -08:00
|
|
|
return peers["Peers"].size();
|
2021-03-27 10:53:19 -07:00
|
|
|
}
|
|
|
|
|
2021-04-03 15:46:41 -07:00
|
|
|
/**
|
2022-01-27 14:25:48 -08:00
|
|
|
* \brief Retrieve your IPFS client ID.
|
2022-01-21 15:39:53 -08:00
|
|
|
* \return ID as string
|
2021-04-03 15:46:41 -07:00
|
|
|
*/
|
2022-05-25 14:22:16 -07:00
|
|
|
std::string IPFS::get_client_id()
|
2021-04-03 15:46:41 -07:00
|
|
|
{
|
2022-01-21 15:39:53 -08:00
|
|
|
ipfs::Json id;
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.Id(&id);
|
2022-01-21 15:39:53 -08:00
|
|
|
return id["ID"];
|
2021-04-03 15:46:41 -07:00
|
|
|
}
|
|
|
|
|
2021-04-03 16:00:41 -07:00
|
|
|
/**
|
2022-01-27 14:25:48 -08:00
|
|
|
* \brief Retrieve your IPFS Public Key.
|
2022-01-21 15:39:53 -08:00
|
|
|
* \return Public key string
|
2021-04-03 16:00:41 -07:00
|
|
|
*/
|
2022-05-25 14:22:16 -07:00
|
|
|
std::string IPFS::get_client_public_key()
|
2021-04-03 16:00:41 -07:00
|
|
|
{
|
2022-01-21 15:39:53 -08:00
|
|
|
ipfs::Json id;
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.Id(&id);
|
2022-01-21 15:39:53 -08:00
|
|
|
return id["PublicKey"];
|
2021-04-03 16:00:41 -07:00
|
|
|
}
|
|
|
|
|
2021-04-05 08:46:37 -07:00
|
|
|
/**
|
2022-01-27 14:25:48 -08:00
|
|
|
* \brief Retrieve the Go IPFS daemon version.
|
2022-01-21 15:39:53 -08:00
|
|
|
* \return Version string
|
2021-04-05 08:46:37 -07:00
|
|
|
*/
|
2022-05-25 14:22:16 -07:00
|
|
|
std::string IPFS::get_version()
|
2021-04-05 08:46:37 -07:00
|
|
|
{
|
2022-01-21 15:39:53 -08:00
|
|
|
ipfs::Json version;
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.Version(&version);
|
2022-01-21 15:39:53 -08:00
|
|
|
return version["Version"];
|
2021-04-05 08:46:37 -07:00
|
|
|
}
|
|
|
|
|
2021-03-27 16:50:04 -07:00
|
|
|
/**
|
2022-01-27 14:25:48 -08:00
|
|
|
* \brief Get the number of IPFS peers.
|
2021-03-27 16:50:04 -07:00
|
|
|
* \return Map with bandwidth information (with keys: 'in' and 'out')
|
|
|
|
*/
|
2022-05-25 14:22:16 -07:00
|
|
|
std::map<std::string, float> IPFS::get_bandwidth_rates()
|
2021-03-27 16:50:04 -07:00
|
|
|
{
|
2022-05-25 14:22:16 -07:00
|
|
|
std::map<std::string, float> bandwidth_rates;
|
2022-01-21 15:39:53 -08:00
|
|
|
ipfs::Json bandwidth_info;
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.StatsBw(&bandwidth_info);
|
2022-01-21 15:39:53 -08:00
|
|
|
float in = bandwidth_info["RateIn"];
|
|
|
|
float out = bandwidth_info["RateOut"];
|
2022-05-25 14:22:16 -07:00
|
|
|
bandwidth_rates.insert(std::pair<std::string, float>("in", in));
|
|
|
|
bandwidth_rates.insert(std::pair<std::string, float>("out", out));
|
|
|
|
return bandwidth_rates;
|
2021-03-27 16:50:04 -07:00
|
|
|
}
|
|
|
|
|
2021-08-17 09:42:22 -07:00
|
|
|
/**
|
2022-01-27 14:25:48 -08:00
|
|
|
* \brief Get the stats of the current Repo.
|
2022-01-21 15:39:53 -08:00
|
|
|
* \return Map with repo stats (with keys: 'repo-size' and 'path')
|
2021-08-17 09:42:22 -07:00
|
|
|
*/
|
2022-05-25 14:22:16 -07:00
|
|
|
std::map<std::string, std::variant<int, std::string>> IPFS::get_repo_stats()
|
2021-08-17 09:42:22 -07:00
|
|
|
{
|
2022-05-25 14:22:16 -07:00
|
|
|
std::map<std::string, std::variant<int, std::string>> repo_stats;
|
|
|
|
ipfs::Json repo_stats_info;
|
|
|
|
client_.StatsRepo(&repo_stats_info);
|
|
|
|
int repo_size = (int)repo_stats_info["RepoSize"] / 1000000; // Convert from bytes to MB
|
|
|
|
std::string repo_path = repo_stats_info["RepoPath"];
|
|
|
|
repo_stats.insert(std::pair<std::string, int>("repo-size", repo_size));
|
|
|
|
repo_stats.insert(std::pair<std::string, std::string>("path", repo_path));
|
|
|
|
return repo_stats;
|
2021-08-17 09:42:22 -07:00
|
|
|
}
|
|
|
|
|
2021-03-30 16:46:33 -07:00
|
|
|
/**
|
2022-01-21 15:39:53 -08:00
|
|
|
* \brief Fetch file from IFPS network
|
2021-03-30 16:46:33 -07:00
|
|
|
* \param path File path
|
2022-01-21 15:39:53 -08:00
|
|
|
* \param contents File contents as iostream
|
|
|
|
* \throw std::runtime_error when there is a
|
|
|
|
* connection-time/something goes wrong while trying to get the file
|
2021-03-30 16:46:33 -07:00
|
|
|
*/
|
2022-01-21 15:39:53 -08:00
|
|
|
void IPFS::fetch(const std::string& path, std::iostream* contents)
|
2021-03-30 16:46:33 -07:00
|
|
|
{
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.FilesGet(path, contents);
|
2021-03-30 16:46:33 -07:00
|
|
|
}
|
|
|
|
|
2021-03-27 11:36:00 -07:00
|
|
|
/**
|
2022-01-21 15:39:53 -08:00
|
|
|
* \brief Add a file to IPFS network
|
|
|
|
* \param path File path where the file could be stored in IPFS (like putting a file inside a directory within IPFS)
|
2021-03-30 16:35:42 -07:00
|
|
|
* \param content Content that needs to be written to the IPFS network
|
2022-01-21 15:39:53 -08:00
|
|
|
* \throw std::runtime_error when there is a connection-time/something goes wrong while adding the file
|
2021-03-30 17:23:51 -07:00
|
|
|
* \return IPFS content-addressed identifier (CID) hash
|
2021-03-29 13:12:31 -07:00
|
|
|
*/
|
2022-01-21 15:39:53 -08:00
|
|
|
std::string IPFS::add(const std::string& path, const std::string& content)
|
2021-03-27 11:36:00 -07:00
|
|
|
{
|
2022-01-21 15:39:53 -08:00
|
|
|
ipfs::Json result;
|
|
|
|
std::string hash;
|
|
|
|
// Publish a single file
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.FilesAdd({{path, ipfs::http::FileUpload::Type::kFileContents, content}}, &result);
|
2022-01-21 15:39:53 -08:00
|
|
|
if (result.is_array() && result.size() > 0)
|
|
|
|
{
|
|
|
|
for (const auto& files : result.items())
|
2021-03-30 17:23:51 -07:00
|
|
|
{
|
2022-01-21 15:39:53 -08:00
|
|
|
hash = files.value()["hash"];
|
|
|
|
break;
|
2021-03-30 17:23:51 -07:00
|
|
|
}
|
2022-01-21 15:39:53 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("File is not added, result is incorrect.");
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abort the request abruptly. Used for stopping the thread.
|
|
|
|
*/
|
|
|
|
void IPFS::abort()
|
|
|
|
{
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.Abort();
|
2022-01-21 15:39:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the state, to allow for new API IPFS requests. Used after the thread.join() and abort() call.
|
|
|
|
*/
|
|
|
|
void IPFS::reset()
|
|
|
|
{
|
2022-05-25 14:22:16 -07:00
|
|
|
client_.Reset();
|
2021-03-27 11:36:00 -07:00
|
|
|
}
|