From 7d69f74e29d4f33edbe47fb0790095faaac204f3 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 1 Sep 2026 16:37:16 -0400 Subject: [PATCH] retrun relative pacth to torrent's files --- libi2pd_client/TorrentsRPC.cpp | 62 ++++++++++++++++++--------------- libi2pd_client/TorrentsTunnel.h | 1 + 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/libi2pd_client/TorrentsRPC.cpp b/libi2pd_client/TorrentsRPC.cpp index 58a38a0a..45041a54 100644 --- a/libi2pd_client/TorrentsRPC.cpp +++ b/libi2pd_client/TorrentsRPC.cpp @@ -54,6 +54,7 @@ namespace torrents boost::json::array GetPeers (std::shared_ptr torrent) const; boost::json::array GetTrackers (std::shared_ptr torrent) const; boost::json::array GetTrackerStats (std::shared_ptr torrent) const; + boost::json::array GetFiles (std::shared_ptr 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) - { - 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) { boost::json::array wanted; @@ -470,6 +444,36 @@ namespace torrents return trackers; } + boost::json::array JSONRPCHandler::GetFiles (std::shared_ptr 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 i2psnark { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03 }; diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index a7dcaa49..755b5714 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -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 FindTorrent (const Torrent::InfoHash& infoHash) const;