From 895d10ed238df6e4fa79b78fbd3cb3181df97fc2 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 23 Aug 2026 14:32:20 -0400 Subject: [PATCH] bytesCompleted for torrents with multiple files --- libi2pd_client/Torrents.cpp | 27 +++++++++++++++++++++++++++ libi2pd_client/Torrents.h | 1 + libi2pd_client/TorrentsRPC.cpp | 7 ++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/libi2pd_client/Torrents.cpp b/libi2pd_client/Torrents.cpp index fa889dcb..c30fce55 100644 --- a/libi2pd_client/Torrents.cpp +++ b/libi2pd_client/Torrents.cpp @@ -659,6 +659,33 @@ namespace torrents } } + std::vector Torrent::GetFilesCompleted () const + { + std::vector completed; + if (!m_Files.empty ()) + { + auto filesIT = m_Files.begin (); + size_t currentSize = 0, currentCompletedSize = 0; + for (const auto& piece: m_Pieces) + { + if (currentSize + m_PieceLength < filesIT->second) + { + currentSize += m_PieceLength; + if (piece.IsComplete ()) currentCompletedSize += m_PieceLength; + } + else + { + if (piece.IsComplete ()) currentCompletedSize += (filesIT->second - currentSize); + completed.push_back (currentCompletedSize); + currentSize = 0; currentCompletedSize = 0; + filesIT++; + if (filesIT == m_Files.end ()) break; + } + } + } + return completed; + } + PeerConnection::PeerConnection (std::shared_ptr owner, std::shared_ptr stream): i2p::client::I2PServiceHandler (owner), m_Stream (stream), m_ReceiveBufferOffset (0), m_NextMsgLength (0), diff --git a/libi2pd_client/Torrents.h b/libi2pd_client/Torrents.h index 196b433a..ce92c046 100644 --- a/libi2pd_client/Torrents.h +++ b/libi2pd_client/Torrents.h @@ -186,6 +186,7 @@ namespace torrents const std::unordered_set& GetPeers () const { return m_Peers; } std::tuple GetNextBlockToRequest (std::shared_ptr conn, bool skipRequested = true); // return (index, offset, len) std::vector GetPieceFileFragments (int index) const; + std::vector GetFilesCompleted () const; // completed size per file bool UpdateStatus (uint64_t ts); // return true if complete uint64_t GetNextTrackerRequestTime () const { return m_NextTrackerRequestTime; } diff --git a/libi2pd_client/TorrentsRPC.cpp b/libi2pd_client/TorrentsRPC.cpp index 48fc2c48..ca139fe5 100644 --- a/libi2pd_client/TorrentsRPC.cpp +++ b/libi2pd_client/TorrentsRPC.cpp @@ -292,14 +292,19 @@ namespace torrents 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"] = 0; // TODO: + file["bytesCompleted"] = (ind < filesCompleted.size ()) ? filesCompleted[ind] : 0; files.push_back (file); + ind++; } + } return files; } },