Add new webUI API. Closes #6457.

Now getting piece information for a specific torrent is possible via:

* Returns an array of states (integers) of pieces in order. Defined as:
  "0=not downloaded", "1=downloading", "2=downloaded".
  GET /query/getPieceStates/<torrent_hash>

* Returns an array of hashes (strings) of pieces in order:
  GET /query/getPieceHashes/<torrent_hash>
This commit is contained in:
Chocobo1 2017-04-06 00:36:00 +08:00
parent 47960b2592
commit 3933790bda
6 changed files with 88 additions and 1 deletions

View file

@ -654,6 +654,59 @@ QByteArray btjson::getFilesForTorrent(const QString& hash)
return json::toJson(fileList);
}
/**
* Returns an array of hashes (of each pieces respectively) for a torrent in JSON format.
*
* The return value is a JSON-formatted array of strings (hex strings).
*/
QByteArray btjson::getPieceHashesForTorrent(const QString &hash)
{
CACHED_VARIABLE_FOR_HASH(QVariantList, pieceHashes, CACHE_DURATION_MS, hash);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent) {
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
return QByteArray();
}
const QVector<QByteArray> hashes = torrent->info().pieceHashes();
pieceHashes.reserve(hashes.size());
foreach (const QByteArray &hash, hashes)
pieceHashes.append(hash.toHex());
return json::toJson(pieceHashes);
}
/**
* Returns an array of states (of each pieces respectively) for a torrent in JSON format.
*
* The return value is a JSON-formatted array of ints.
* 0: piece not downloaded
* 1: piece requested or downloading
* 2: piece already downloaded
*/
QByteArray btjson::getPieceStatesForTorrent(const QString &hash)
{
CACHED_VARIABLE_FOR_HASH(QVariantList, pieceStates, CACHE_DURATION_MS, hash);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent) {
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
return QByteArray();
}
const QBitArray states = torrent->pieces();
pieceStates.reserve(states.size());
for (int i = 0; i < states.size(); ++i)
pieceStates.append(static_cast<int>(states[i]) * 2);
const QBitArray dlstates = torrent->downloadingPieces();
for (int i = 0; i < states.size(); ++i) {
if (dlstates[i])
pieceStates[i] = 1;
}
return json::toJson(pieceStates);
}
/**
* Returns the global transfer information in JSON format.
*