handle magnetLink field

This commit is contained in:
orignal
2026-09-30 22:03:43 -04:00
parent 8756184af8
commit 15b75e2bb7
+25
View File
@@ -11,6 +11,9 @@
#include <boost/version.hpp>
#if !defined(ANDROID) && (BOOST_VERSION >= 108100) // boost::json since 1.75, we allow it since 1.81 due to std::string_view compatibility
#include <boost/json.hpp>
#include <boost/url.hpp>
#include <boost/url/encode.hpp>
#include <boost/url/grammar/alnum_chars.hpp>
#define JSON_SUPPORTED
#endif
#include <boost/algorithm/hex.hpp>
@@ -330,6 +333,28 @@ namespace torrents
return boost::json::value(hexHash);
}
},
{
"magnetLink", [](std::shared_ptr<Torrent> torrent)
{
std::string hexHash;
boost::algorithm::hex (torrent->GetInfoHash ().begin(), torrent->GetInfoHash ().end(), std::back_inserter(hexHash));
std::string magnet("magnet:?xt=urn:btih:");
magnet += hexHash;
if (!torrent->GetName ().empty ())
{
magnet += "&dn=";
boost::urls::encoding_opts opts;
opts.space_as_plus = true;
magnet += boost::urls::encode (torrent->GetName (), boost::urls::grammar::alnum_chars, opts);
}
if (!torrent->GetAnnounce ().empty ())
{
magnet += "&tr=";
magnet += boost::core::string_view (boost::urls::url (torrent->GetAnnounce ()));
}
return boost::json::value(magnet);
}
},
{ "pieces", [](std::shared_ptr<Torrent> torrent)
{
auto [bitfield, have] = torrent->CreateBitfield ();