Expose peer and piece state to web UI
This commit is contained in:
parent
f4f4e86be4
commit
6ae72907b0
6 changed files with 213 additions and 11 deletions
|
|
@ -442,6 +442,12 @@ static uint64_t json_u64(const json_t *obj, const char *key) {
|
|||
? (uint64_t)json_integer_value(value) : 0;
|
||||
}
|
||||
|
||||
static double json_number_or(const json_t *obj, const char *key,
|
||||
double fallback) {
|
||||
json_t *value = json_object_get(obj, key);
|
||||
return json_is_number(value) ? json_number_value(value) : fallback;
|
||||
}
|
||||
|
||||
static const char *base_name(const char *path) {
|
||||
if (!path || !*path) return "torrent";
|
||||
const char *slash = strrchr(path, '/');
|
||||
|
|
@ -568,6 +574,33 @@ static json_t *tracker_hosts(json_t *trackers) {
|
|||
return hosts;
|
||||
}
|
||||
|
||||
static json_t *map_peer_list(json_t *torrent) {
|
||||
json_t *out = json_array();
|
||||
json_t *peers = json_object_get(torrent, "peer_list");
|
||||
if (!out || !json_is_array(peers)) return out;
|
||||
size_t index;
|
||||
json_t *peer;
|
||||
json_array_foreach(peers, index, peer) {
|
||||
if (!json_is_object(peer)) continue;
|
||||
json_t *item = json_pack(
|
||||
"{s:s,s:s,s:i,s:s,s:s,s:s,s:f,s:i,s:i,s:I,s:I,s:f}",
|
||||
"country", "",
|
||||
"ip", json_string_or(peer, "ip", ""),
|
||||
"port", (int)json_u64(peer, "port"),
|
||||
"client", json_string_or(peer, "client", "Unknown"),
|
||||
"connection", json_string_or(peer, "connection", "TCP"),
|
||||
"flags", json_string_or(peer, "flags", ""),
|
||||
"progress", json_number_or(peer, "progress", 0.0),
|
||||
"dlspeed", (int)json_u64(peer, "dlspeed"),
|
||||
"upspeed", (int)json_u64(peer, "upspeed"),
|
||||
"downloaded", (json_int_t)json_u64(peer, "downloaded"),
|
||||
"uploaded", (json_int_t)json_u64(peer, "uploaded"),
|
||||
"relevance", json_number_or(peer, "relevance", 0.0));
|
||||
if (item) json_array_append_new(out, item);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static double torrent_progress_ratio(json_t *torrent, uint64_t *done_out,
|
||||
uint64_t *total_out) {
|
||||
uint64_t done = json_u64(torrent, "bytes_done");
|
||||
|
|
@ -781,7 +814,7 @@ static json_t *map_torrent(json_t *torrent, bool detail, double dlspeed) {
|
|||
"{s:s,s:I,s:f,s:i,s:f}", "name", name,
|
||||
"size", (json_int_t)total, "progress", progress,
|
||||
"priority", 1, "availability", 1.0));
|
||||
peers_list = json_array();
|
||||
peers_list = map_peer_list(torrent);
|
||||
hosts = tracker_hosts(trackers);
|
||||
}
|
||||
|
||||
|
|
@ -844,6 +877,11 @@ static json_t *map_torrent(json_t *torrent, bool detail, double dlspeed) {
|
|||
json_string(json_string_or(torrent, "source", "")));
|
||||
json_object_set_new(out, "pieceCount", json_integer((json_int_t)pieces));
|
||||
json_object_set_new(out, "piecesDone", json_integer((json_int_t)pieces_done));
|
||||
json_t *piece_states = json_object_get(torrent, "piece_states");
|
||||
json_object_set_new(out, "pieceStates",
|
||||
json_is_array(piece_states)
|
||||
? json_deep_copy(piece_states)
|
||||
: json_array());
|
||||
json_object_set_new(out, "trackers", trackers ? trackers : json_array());
|
||||
json_object_set_new(out, "peersList", peers_list ? peers_list : json_array());
|
||||
json_object_set_new(out, "files", files ? files : json_array());
|
||||
|
|
@ -1056,9 +1094,12 @@ static void api_torrent_detail(int fd, const char *tail) {
|
|||
} else if (strcmp(tab, "pieces") == 0) {
|
||||
uint64_t count = json_u64(torrent, "pieceCount");
|
||||
uint64_t done = json_u64(torrent, "piecesDone");
|
||||
json_t *pieces = json_array();
|
||||
for (uint64_t i = 0; pieces && i < count && i < 4000; i++)
|
||||
json_array_append_new(pieces, json_integer(i < done ? 2 : 0));
|
||||
json_t *states = json_object_get(torrent, "pieceStates");
|
||||
json_t *pieces = json_is_array(states) ? json_deep_copy(states)
|
||||
: json_array();
|
||||
if (pieces && json_array_size(pieces) == 0)
|
||||
for (uint64_t i = 0; i < count && i < 4000; i++)
|
||||
json_array_append_new(pieces, json_integer(i < done ? 2 : 0));
|
||||
json_t *value = json_pack("{s:I,s:I,s:o}",
|
||||
"pieceSize", json_u64(torrent, "pieceSize"),
|
||||
"pieceCount", count, "pieces", pieces);
|
||||
|
|
@ -1085,9 +1126,15 @@ static void api_add(int fd, const char *body, size_t len) {
|
|||
if (!save_path || !*save_path) save_path = ".";
|
||||
if (!source) source = magnet;
|
||||
if ((!source || !*source) && (!data || !*data)) {
|
||||
/* A request carrying only parsed metadata (name/files) but no bytes is
|
||||
* the tell-tale of a stale UI that predates base64 upload support. */
|
||||
bool looks_stale = json_object_get(req, "name") ||
|
||||
json_object_get(req, "files");
|
||||
json_decref(req);
|
||||
http_text(fd, 400, "Bad Request",
|
||||
"torrent-ui must send a magnet, source, or torrent data");
|
||||
http_text(fd, 400, "Bad Request", looks_stale
|
||||
? "no torrent bytes in request: the page is running an old UI. "
|
||||
"Hard-reload the browser (Ctrl+Shift+R) and add the file again."
|
||||
: "send a magnet, a source path, or uploaded torrent bytes");
|
||||
return;
|
||||
}
|
||||
json_t *params = json_object();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue