diff --git a/libi2pd_client/TorrentsRPC.cpp b/libi2pd_client/TorrentsRPC.cpp index 66cfec66..78f3ff8e 100644 --- a/libi2pd_client/TorrentsRPC.cpp +++ b/libi2pd_client/TorrentsRPC.cpp @@ -109,7 +109,7 @@ namespace torrents else torrentIds.push_back (ids.as_int64 ()); } - return torrentIds;; + return torrentIds; } std::string JSONRPCHandler::HandleRequest (std::string_view request) @@ -201,8 +201,14 @@ namespace torrents bool deleteFiles = false; if (arguments.contains ("delete-local-data")) deleteFiles = arguments.at ("delete-local-data").as_bool (); - for (auto id: GetTorrentIds (arguments)) - m_Tunnel->RemoveTorrent (id, deleteFiles); + auto torrentIds = GetTorrentIds (arguments); + if (!torrentIds.empty ()) + { + for (auto id: torrentIds) + m_Tunnel->RemoveTorrent (id, deleteFiles); + } + else + m_Tunnel->RemoveAllTorrents (deleteFiles); boost::json::object response; // always empty return SuccessResponse (GetTag (jsonRequest), std::move (response)); } diff --git a/libi2pd_client/TorrentsTunnel.cpp b/libi2pd_client/TorrentsTunnel.cpp index f5cfa59e..cd65ec70 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -451,6 +451,17 @@ namespace torrents return true; } + void TorrentsTunnel::RemoveAllTorrents (bool deleteFiles) + { + boost::asio::post (GetService (), [this, deleteFiles]() + { + for (const auto& it: m_Torrents) + RemoveTorrent (it.second, deleteFiles); + m_Torrents.clear (); + m_TorrentsByID.clear (); + }); + } + void TorrentsTunnel::RemoveTorrent (std::shared_ptr torrent, bool deleteFiles) { if (!torrent) return; diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index d6032218..4fb8e35a 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -110,6 +110,7 @@ namespace torrents std::pair, int> AddMagnet (std::string_view magnet); // return (torrent, id) void UpdateTorrentInfo (std::shared_ptr torrent, std::string_view info); // magnet bool RemoveTorrent (int id, bool deleteFiles); + void RemoveAllTorrents (bool deleteFiles); bool StopTorrent (int id); bool StartTorrent (int id); void ConnectToNewPeers (std::shared_ptr torrent, std::unordered_set& newPeers, PeerConnectionOrigin origin);