Browse Source

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>
adaptive-webui-19844
Chocobo1 8 years ago
parent
commit
3933790bda
  1. 15
      src/base/bittorrent/torrentinfo.cpp
  2. 1
      src/base/bittorrent/torrentinfo.h
  3. 53
      src/webui/btjson.cpp
  4. 2
      src/webui/btjson.h
  5. 16
      src/webui/webapplication.cpp
  6. 2
      src/webui/webapplication.h

15
src/base/bittorrent/torrentinfo.cpp

@ -246,6 +246,21 @@ QVector<int> TorrentInfo::fileIndicesForPiece(int pieceIndex) const @@ -246,6 +246,21 @@ QVector<int> TorrentInfo::fileIndicesForPiece(int pieceIndex) const
return res;
}
QVector<QByteArray> TorrentInfo::pieceHashes() const
{
if (!isValid())
return {};
const int count = piecesCount();
QVector<QByteArray> hashes;
hashes.reserve(count);
for (int i = 0; i < count; ++i)
hashes += { m_nativeInfo->hash_for_piece_ptr(i), libtorrent::sha1_hash::size };
return hashes;
}
TorrentInfo::PieceRange TorrentInfo::filePieces(const QString& file) const
{
if (!isValid()) // if we do not check here the debug message will be printed, which would be not correct

1
src/base/bittorrent/torrentinfo.h

@ -91,6 +91,7 @@ namespace BitTorrent @@ -91,6 +91,7 @@ namespace BitTorrent
QByteArray metadata() const;
QStringList filesForPiece(int pieceIndex) const;
QVector<int> fileIndicesForPiece(int pieceIndex) const;
QVector<QByteArray> pieceHashes() const;
using PieceRange = IndexRange<int>;
// returns pair of the first and the last pieces into which

53
src/webui/btjson.cpp

@ -654,6 +654,59 @@ QByteArray btjson::getFilesForTorrent(const QString& hash) @@ -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.
*

2
src/webui/btjson.h

@ -51,6 +51,8 @@ public: @@ -51,6 +51,8 @@ public:
static QByteArray getWebSeedsForTorrent(const QString& hash);
static QByteArray getPropertiesForTorrent(const QString& hash);
static QByteArray getFilesForTorrent(const QString& hash);
static QByteArray getPieceHashesForTorrent(const QString &hash);
static QByteArray getPieceStatesForTorrent(const QString &hash);
static QByteArray getTransferInfo();
static QByteArray getTorrentsRatesLimits(QStringList& hashes, bool downloadLimits);
static QByteArray getLog(bool normal, bool info, bool warning, bool critical, int lastKnownId);

16
src/webui/webapplication.cpp

@ -49,7 +49,7 @@ @@ -49,7 +49,7 @@
#include "websessiondata.h"
#include "webapplication.h"
static const int API_VERSION = 13;
static const int API_VERSION = 14;
static const int API_VERSION_MIN = 13;
const QString WWW_FOLDER = ":/www/public/";
@ -83,6 +83,8 @@ QMap<QString, QMap<QString, WebApplication::Action> > WebApplication::initialize @@ -83,6 +83,8 @@ QMap<QString, QMap<QString, WebApplication::Action> > WebApplication::initialize
ADD_ACTION(query, propertiesFiles);
ADD_ACTION(query, getLog);
ADD_ACTION(query, getPeerLog);
ADD_ACTION(query, getPieceHashes);
ADD_ACTION(query, getPieceStates);
ADD_ACTION(sync, maindata);
ADD_ACTION(sync, torrent_peers);
ADD_ACTION(command, shutdown);
@ -310,6 +312,18 @@ void WebApplication::action_query_getPeerLog() @@ -310,6 +312,18 @@ void WebApplication::action_query_getPeerLog()
print(btjson::getPeerLog(lastKnownId), Http::CONTENT_TYPE_JSON);
}
void WebApplication::action_query_getPieceHashes()
{
CHECK_URI(1);
print(btjson::getPieceHashesForTorrent(args_.front()), Http::CONTENT_TYPE_JSON);
}
void WebApplication::action_query_getPieceStates()
{
CHECK_URI(1);
print(btjson::getPieceStatesForTorrent(args_.front()), Http::CONTENT_TYPE_JSON);
}
// GET param:
// - rid (int): last response id
void WebApplication::action_sync_maindata()

2
src/webui/webapplication.h

@ -56,6 +56,8 @@ private: @@ -56,6 +56,8 @@ private:
void action_query_propertiesFiles();
void action_query_getLog();
void action_query_getPeerLog();
void action_query_getPieceHashes();
void action_query_getPieceStates();
void action_sync_maindata();
void action_sync_torrent_peers();
void action_command_shutdown();

Loading…
Cancel
Save