refactor: getFiles fileIndexes usage

This commit is contained in:
ShanaryS 2025-05-22 13:01:38 -04:00
commit 965bcce312

View file

@ -278,11 +278,21 @@ namespace
return trackerList; return trackerList;
} }
QJsonArray getFiles(const BitTorrent::Torrent* const torrent, const QList<int> fileIndexes) QJsonArray getFiles(const BitTorrent::Torrent *const torrent, QList<int> &fileIndexes)
{ {
Q_ASSERT(torrent->hasMetadata());
if (!torrent->hasMetadata()) [[unlikely]]
return {};
if (fileIndexes.isEmpty())
{
const int filesCount = torrent->filesCount();
fileIndexes.reserve(filesCount);
for (int i = 0; i < filesCount; ++i)
fileIndexes.append(i);
}
QJsonArray fileList; QJsonArray fileList;
if (torrent->hasMetadata())
{
const QList<BitTorrent::DownloadPriority> priorities = torrent->filePriorities(); const QList<BitTorrent::DownloadPriority> priorities = torrent->filePriorities();
const QList<qreal> fp = torrent->filesProgress(); const QList<qreal> fp = torrent->filesProgress();
const QList<qreal> fileAvailability = torrent->fetchAvailableFileFractions().takeResult(); const QList<qreal> fileAvailability = torrent->fetchAvailableFileFractions().takeResult();
@ -308,7 +318,6 @@ namespace
fileList.append(fileDict); fileList.append(fileDict);
} }
}
return fileList; return fileList;
} }
@ -408,11 +417,7 @@ void TorrentsController::infoAction()
if (includeFiles) if (includeFiles)
{ {
const int filesCount = torrent->filesCount();
QList<int> fileIndexes; QList<int> fileIndexes;
fileIndexes.reserve(filesCount);
for (int i = 0; i < filesCount; ++i)
fileIndexes.append(i);
serializedTorrent.insert(KEY_PROP_FILES, getFiles(torrent, fileIndexes)); serializedTorrent.insert(KEY_PROP_FILES, getFiles(torrent, fileIndexes));
} }
if (includeTrackers) if (includeTrackers)
@ -746,11 +751,11 @@ void TorrentsController::filesAction()
if (!torrent) if (!torrent)
throw APIError(APIErrorType::NotFound); throw APIError(APIErrorType::NotFound);
const int filesCount = torrent->filesCount();
QList<int> fileIndexes; QList<int> fileIndexes;
const auto idxIt = params().constFind(u"indexes"_s); const auto idxIt = params().constFind(u"indexes"_s);
if (idxIt != params().cend()) if (idxIt != params().cend())
{ {
const int filesCount = torrent->filesCount();
const QStringList indexStrings = idxIt.value().split(u'|'); const QStringList indexStrings = idxIt.value().split(u'|');
fileIndexes.reserve(indexStrings.size()); fileIndexes.reserve(indexStrings.size());
std::transform(indexStrings.cbegin(), indexStrings.cend(), std::back_inserter(fileIndexes) std::transform(indexStrings.cbegin(), indexStrings.cend(), std::back_inserter(fileIndexes)
@ -765,12 +770,6 @@ void TorrentsController::filesAction()
return index; return index;
}); });
} }
else
{
fileIndexes.reserve(filesCount);
for (int i = 0; i < filesCount; ++i)
fileIndexes.append(i);
}
setResult(getFiles(torrent, fileIndexes)); setResult(getFiles(torrent, fileIndexes));
} }