diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index fb89d0922..062c51763 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -158,7 +158,7 @@ namespace void applyToTorrents(const QStringList &idList, Func func) requires std::invocable { - if ((idList.size() == 1) && (idList[0] == u"all")) + if ((idList.size() == 1) && (idList[0] == u"all" || idList[0] == u"*")) { for (BitTorrent::Torrent *const torrent : asConst(BitTorrent::Session::instance()->torrents())) func(torrent); @@ -1116,31 +1116,10 @@ void TorrentsController::addAction() void TorrentsController::addTrackersAction() { requireParams({u"hash"_s, u"urls"_s}); - const QString hashParam = params()[u"hash"_s]; - QList torrents; - const QList entries = BitTorrent::parseTrackerEntries(params()[u"urls"_s]); + const QList trackers = BitTorrent::parseTrackerEntries(params()[u"urls"_s]); + const QStringList idStrings = params()[u"hash"_s].split(u'|'); - const QStringList idStrings = hashParam.split(u'|', Qt::SkipEmptyParts); - for (const QString &hash : idStrings) - { - if (hash == u"*"_s) - continue; - const auto id = BitTorrent::TorrentID::fromString(hash); - BitTorrent::Torrent *const torrent = BitTorrent::Session::instance()->getTorrent(id); - if (!torrent) - throw APIError(APIErrorType::NotFound); - - torrents.append(torrent); - } - - if (hashParam == u"*"_s) - { - // add this tracker to all torrents - torrents = BitTorrent::Session::instance()->torrents(); - } - - for (BitTorrent::Torrent *const torrent : asConst(torrents)) - torrent->addTrackers(entries); + applyToTorrents(idStrings, [&trackers](BitTorrent::Torrent *const torrent) { torrent->addTrackers(trackers); }); } void TorrentsController::editTrackerAction() @@ -1202,7 +1181,7 @@ void TorrentsController::removeTrackersAction() { requireParams({u"hash"_s, u"urls"_s}); - const QString hashParam = params()[u"hash"_s]; + const QStringList idStrings = params()[u"hash"_s].split(u'|', Qt::SkipEmptyParts); const QStringList urlsParam = params()[u"urls"_s].split(u'|', Qt::SkipEmptyParts); QStringList urls; @@ -1210,31 +1189,7 @@ void TorrentsController::removeTrackersAction() for (const QString &urlStr : urlsParam) urls << QUrl::fromPercentEncoding(urlStr.toLatin1()); - QList torrents; - - const QStringList idStrings = hashParam.split(u'|', Qt::SkipEmptyParts); - for (const QString &hash : idStrings) - { - if (hash == u"*"_s) - continue; - const auto id = BitTorrent::TorrentID::fromString(hash); - BitTorrent::Torrent *const torrent = BitTorrent::Session::instance()->getTorrent(id); - if (!torrent) - throw APIError(APIErrorType::NotFound); - - torrents.append(torrent); - } - - if (hashParam == u"*"_s) - { - // remove trackers from all torrents - torrents = BitTorrent::Session::instance()->torrents(); - } - - for (BitTorrent::Torrent *const torrent : asConst(torrents)) - torrent->removeTrackers(urls); - - setResult(QString()); + applyToTorrents(idStrings, [&urls](BitTorrent::Torrent *const torrent) { torrent->removeTrackers(urls); }); } void TorrentsController::addPeersAction()