From 4c190b0d4f1b908ac5fd97e61c86cf439f054354 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Thu, 5 Jun 2025 21:15:44 -0700 Subject: [PATCH] Move priority parsing into helper function --- src/webui/api/torrentscontroller.cpp | 34 ++++++++++++++++------------ 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index 6f74fee36..f7c6118a0 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -456,6 +456,17 @@ namespace return info; } + + nonstd::expected parseDownloadPriority(const QString &priorityStr) + { + bool ok = false; + const auto priority = static_cast(priorityStr.toInt(&ok)); + if (!ok) + return nonstd::make_unexpected(TorrentsController::tr("Priority must be an integer")); + if (!isValidDownloadPriority(priority)) + return nonstd::make_unexpected(TorrentsController::tr("Priority is not valid")); + return priority; + } } TorrentsController::TorrentsController(IApplication *app, QObject *parent) @@ -1004,14 +1015,10 @@ void TorrentsController::addAction() filePriorities.reserve(filePrioritiesParam.size()); for (const QString &priorityStr : filePrioritiesParam) { - bool ok = false; - const auto priority = static_cast(priorityStr.toInt(&ok)); - if (!ok) - throw APIError(APIErrorType::BadParams, tr("Priority must be an integer")); - if (!BitTorrent::isValidDownloadPriority(priority)) - throw APIError(APIErrorType::BadParams, tr("Priority is not valid")); - - filePriorities << priority; + const nonstd::expected result = parseDownloadPriority(priorityStr); + if (!result) + throw APIError(APIErrorType::BadParams, result.error()); + filePriorities << result.value(); } } @@ -1275,13 +1282,11 @@ void TorrentsController::filePrioAction() requireParams({u"hash"_s, u"id"_s, u"priority"_s}); const auto id = BitTorrent::TorrentID::fromString(params()[u"hash"_s]); - bool ok = false; - const auto priority = static_cast(params()[u"priority"_s].toInt(&ok)); - if (!ok) - throw APIError(APIErrorType::BadParams, tr("Priority must be an integer")); + const nonstd::expected result = parseDownloadPriority(params()[u"priority"_s]); + if (!result) + throw APIError(APIErrorType::BadParams, result.error()); - if (!BitTorrent::isValidDownloadPriority(priority)) - throw APIError(APIErrorType::BadParams, tr("Priority is not valid")); + const BitTorrent::DownloadPriority priority = result.value(); BitTorrent::Torrent *const torrent = BitTorrent::Session::instance()->getTorrent(id); if (!torrent) @@ -1294,6 +1299,7 @@ void TorrentsController::filePrioAction() bool priorityChanged = false; for (const QString &fileID : params()[u"id"_s].split(u'|')) { + bool ok = false; const int id = fileID.toInt(&ok); if (!ok) throw APIError(APIErrorType::BadParams, tr("File IDs must be integers"));