retrun relative pacth to torrent's files

This commit is contained in:
orignal
2026-09-01 16:37:16 -04:00
parent 39d6178f33
commit 7d69f74e29
2 changed files with 34 additions and 29 deletions
+33 -29
View File
@@ -54,6 +54,7 @@ namespace torrents
boost::json::array GetPeers (std::shared_ptr<Torrent> torrent) const;
boost::json::array GetTrackers (std::shared_ptr<Torrent> torrent) const;
boost::json::array GetTrackerStats (std::shared_ptr<Torrent> torrent) const;
boost::json::array GetFiles (std::shared_ptr<Torrent> torrent) const;
static std::string_view RecognizeClientByPeerID (const PeerConnection::PeerID& peerID);
std::string HandleTorrentAdd (boost::json::object&& jsonRequest);
@@ -244,6 +245,8 @@ namespace torrents
t["trackers"] = GetTrackers (torrent);
else if (field == "trackerStats")
t["trackerStats"] = GetTrackerStats (torrent);
else if (field == "files")
t["files"] = GetFiles (torrent);
else
{
auto fieldValue = GetFieldValue (field.as_string (), torrent);
@@ -296,35 +299,6 @@ namespace torrents
return boost::json::value(b64pieces);
}
},
{ "files", [](std::shared_ptr<Torrent> torrent)
{
boost::json::array files;
const auto& torrentFiles = torrent->GetFiles ();
if (torrentFiles.empty ())
{
boost::json::object file;
file["name"] = torrent->GetFullPath ().string ();
file["length"] = torrent->GetLength ();
file["bytesCompleted"] = torrent->GetLength () - torrent->GetLeft ();
files.push_back (file);
}
else
{
auto filesCompleted = torrent->GetFilesCompleted ();
size_t ind = 0;
for (const auto& [filePath, fileSize]: torrentFiles)
{
boost::json::object file;
file["name"] = filePath.string ();
file["length"] = fileSize;
file["bytesCompleted"] = (ind < filesCompleted.size ()) ? filesCompleted[ind] : 0;
files.push_back (file);
ind++;
}
}
return files;
}
},
{ "wanted", [](std::shared_ptr<Torrent> torrent)
{
boost::json::array wanted;
@@ -470,6 +444,36 @@ namespace torrents
return trackers;
}
boost::json::array JSONRPCHandler::GetFiles (std::shared_ptr<Torrent> torrent) const
{
boost::json::array files;
const auto& torrentFiles = torrent->GetFiles ();
if (torrentFiles.empty ())
{
boost::json::object file;
file["name"] = torrent->GetName ();
file["length"] = torrent->GetLength ();
file["bytesCompleted"] = torrent->GetLength () - torrent->GetLeft ();
files.push_back (file);
}
else
{
const auto& torrentsDir = m_Tunnel->GetTorrentsDir ();
auto filesCompleted = torrent->GetFilesCompleted ();
size_t ind = 0;
for (const auto& [filePath, fileSize]: torrentFiles)
{
boost::json::object file;
file["name"] = std::filesystem::relative (filePath, torrentsDir).string ();
file["length"] = fileSize;
file["bytesCompleted"] = (ind < filesCompleted.size ()) ? filesCompleted[ind] : 0;
files.push_back (file);
ind++;
}
}
return files;
}
std::string_view JSONRPCHandler::RecognizeClientByPeerID (const PeerConnection::PeerID& peerID)
{
static constexpr std::array<uint8_t,12> i2psnark { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03 };
+1
View File
@@ -90,6 +90,7 @@ namespace torrents
auto& GetDiskIOService () { return m_DiskIOService.GetService (); };
const std::string& GetPeerID () const { return m_PeerID; }
const std::filesystem::path& GetTorrentsDir () const { return m_TorrentsDir; }
std::string GetTrackerAnnounce (size_t id) const { return (id < m_Trackers.size ()) ? std::get<0>(m_Trackers[id]) : ""; }
size_t GetNumTrackers () const { return m_Trackers.size (); }
std::shared_ptr<Torrent> FindTorrent (const Torrent::InfoHash& infoHash) const;