LibreWeb-Browser/src/ipfs.cc

114 lines
3.1 KiB
C++
Raw Normal View History

2020-12-06 16:15:31 -08:00
#include "ipfs.h"
#include <sstream>
2020-12-06 16:15:31 -08:00
/**
* \brief IPFS Contructor, connect to IPFS
* \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)
*/
IPFS::IPFS(const std::string &host, int port, const std::string &timeout)
: host(host),
port(port),
timeout(timeout),
client(this->host, this->port, this->timeout) {}
2021-03-27 16:50:04 -07:00
/**
* \brief Get the number of IPFS peers
* \return number of peers
*/
std::size_t IPFS::getNrPeers()
2020-12-06 16:15:31 -08:00
{
2021-03-27 11:36:00 -07:00
try
{
ipfs::Json peers;
client.SwarmPeers(&peers);
return peers["Peers"].size();
}
catch (const std::runtime_error &error)
{
// ignore connection issues
}
return 0;
}
/**
* \brief Retrieve your IPFS client ID
* \return ID
*/
std::string const IPFS::getClientID()
{
try
{
ipfs::Json id;
client.Id(&id);
return id["ID"];
}
catch (const std::runtime_error &error)
{
// ignore connection issues
}
return "";
}
2021-03-27 16:50:04 -07:00
/**
* \brief Get the number of IPFS peers
* \return Map with bandwidth information (with keys: 'in' and 'out')
*/
std::map<std::string, float> IPFS::getBandwidthRates()
{
std::map<std::string, float> bandwidthRates;
try
{
ipfs::Json bandwidth_info;
client.StatsBw(&bandwidth_info);
float in = bandwidth_info["RateIn"];
float out = bandwidth_info["RateOut"];
bandwidthRates.insert(std::pair<std::string, float>("in", in));
bandwidthRates.insert(std::pair<std::string, float>("out", out));
}
catch (const std::runtime_error &error)
{
// ignore connection issues
}
return bandwidthRates;
}
/**
2021-03-30 17:23:51 -07:00
* \brief Fetch file from IFPS network (create a new client object each time - which is thread-safe), static method
* \param path File path
2021-03-30 17:23:51 -07:00
* \throw std::runtime_error when there is a connection-time/something goes wrong while trying to get the file
* \return content as string
*/
std::string const IPFS::fetch(const std::string &path)
{
// Create new client each time for thread-safety
ipfs::Client client(this->host, this->port, this->timeout);
std::stringstream contents;
client.FilesGet(path, &contents);
return contents.str();
}
2021-03-27 11:36:00 -07:00
/**
2021-03-31 14:55:19 -07:00
* \brief Add a file to IPFS network (not thread-safe)
* \param path File path where the file could be stored in IPFS (like puting a file inside a directory within IPFS)
* \param content Content that needs to be written to the IPFS network
2021-03-31 14:55:19 -07:00
* \throw std::runtime_error when there is a connection-time/something goes wrong while trying to get 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
*/
2021-03-31 14:55:19 -07:00
std::string const IPFS::add(const std::string &path, const std::string &content)
2021-03-27 11:36:00 -07:00
{
2021-03-31 14:55:19 -07:00
ipfs::Json result;
// Publish a single file
client.FilesAdd({{path, ipfs::http::FileUpload::Type::kFileContents, content}}, &result);
if (result.is_array())
2021-03-30 17:23:51 -07:00
{
2021-03-31 14:55:19 -07:00
for (const auto &files : result.items())
2021-03-30 17:23:51 -07:00
{
2021-03-31 14:55:19 -07:00
return files.value()["hash"];
2021-03-30 17:23:51 -07:00
}
}
2021-03-31 14:55:19 -07:00
// something is wrong, fallback
return "";
2021-03-27 11:36:00 -07:00
}