mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-23 22:55:23 -07:00
refactor: getFiles fileIndexes usage
This commit is contained in:
parent
b5ff7b8b8f
commit
965bcce312
1 changed files with 34 additions and 35 deletions
|
@ -278,36 +278,45 @@ 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)
|
||||||
{
|
{
|
||||||
QJsonArray fileList;
|
Q_ASSERT(torrent->hasMetadata());
|
||||||
if (torrent->hasMetadata())
|
if (!torrent->hasMetadata()) [[unlikely]]
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if (fileIndexes.isEmpty())
|
||||||
{
|
{
|
||||||
const QList<BitTorrent::DownloadPriority> priorities = torrent->filePriorities();
|
const int filesCount = torrent->filesCount();
|
||||||
const QList<qreal> fp = torrent->filesProgress();
|
fileIndexes.reserve(filesCount);
|
||||||
const QList<qreal> fileAvailability = torrent->fetchAvailableFileFractions().takeResult();
|
for (int i = 0; i < filesCount; ++i)
|
||||||
const BitTorrent::TorrentInfo info = torrent->info();
|
fileIndexes.append(i);
|
||||||
for (const int index : asConst(fileIndexes))
|
}
|
||||||
|
|
||||||
|
QJsonArray fileList;
|
||||||
|
const QList<BitTorrent::DownloadPriority> priorities = torrent->filePriorities();
|
||||||
|
const QList<qreal> fp = torrent->filesProgress();
|
||||||
|
const QList<qreal> fileAvailability = torrent->fetchAvailableFileFractions().takeResult();
|
||||||
|
const BitTorrent::TorrentInfo info = torrent->info();
|
||||||
|
for (const int index : asConst(fileIndexes))
|
||||||
|
{
|
||||||
|
QJsonObject fileDict =
|
||||||
{
|
{
|
||||||
QJsonObject fileDict =
|
{KEY_FILE_INDEX, index},
|
||||||
{
|
{KEY_FILE_PROGRESS, fp[index]},
|
||||||
{KEY_FILE_INDEX, index},
|
{KEY_FILE_PRIORITY, static_cast<int>(priorities[index])},
|
||||||
{KEY_FILE_PROGRESS, fp[index]},
|
{KEY_FILE_SIZE, torrent->fileSize(index)},
|
||||||
{KEY_FILE_PRIORITY, static_cast<int>(priorities[index])},
|
{KEY_FILE_AVAILABILITY, fileAvailability[index]},
|
||||||
{KEY_FILE_SIZE, torrent->fileSize(index)},
|
// need to provide paths using a platform-independent separator format
|
||||||
{KEY_FILE_AVAILABILITY, fileAvailability[index]},
|
{KEY_FILE_NAME, torrent->filePath(index).data()}
|
||||||
// need to provide paths using a platform-independent separator format
|
};
|
||||||
{KEY_FILE_NAME, torrent->filePath(index).data()}
|
|
||||||
};
|
|
||||||
|
|
||||||
const BitTorrent::TorrentInfo::PieceRange idx = info.filePieces(index);
|
const BitTorrent::TorrentInfo::PieceRange idx = info.filePieces(index);
|
||||||
fileDict[KEY_FILE_PIECE_RANGE] = QJsonArray{idx.first(), idx.last()};
|
fileDict[KEY_FILE_PIECE_RANGE] = QJsonArray{idx.first(), idx.last()};
|
||||||
|
|
||||||
if (index == 0)
|
if (index == 0)
|
||||||
fileDict[KEY_FILE_IS_SEED] = torrent->isFinished();
|
fileDict[KEY_FILE_IS_SEED] = torrent->isFinished();
|
||||||
|
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue