From 27c5f66879b35b2c37e0f70407e6dd77c480949a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 15 May 2017 16:53:29 +0200 Subject: [PATCH] Rename info_hash method and use it where adequate --- toREST/include/torrent.hpp | 7 ++----- toREST/include/torrents.hpp | 18 ++++++++++-------- toREST/include/util.hpp | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/toREST/include/torrent.hpp b/toREST/include/torrent.hpp index 2cf9b78..35eac56 100644 --- a/toREST/include/torrent.hpp +++ b/toREST/include/torrent.hpp @@ -11,13 +11,10 @@ namespace tr { namespace torrent { template static nlohmann::json to_json(torrent_t torrent) { - std::stringstream ss; // TODO optimize - ss << torrent.info_hash(); - const std::string hash = ss.str(); const auto status = torrent.status( torrent.query_name | torrent.query_save_path); - return {{"info_hash", hash}, + return {{"info_hash", util::sha1::string(torrent.info_hash())}, {"paused", status.paused}, {"seeding", status.is_seeding}, {"state", status.state}, @@ -46,7 +43,7 @@ void get(torrent_session &session, response resp, request req) { } const auto hash = req->path.substr(18, req->path.size() - 18); //TODO hacky - const auto torrent = session.find_torrent(util::sha1::string(hash)); + const auto torrent = session.find_torrent(util::sha1::info_hash(hash)); if (!torrent.is_valid()) { return respond(http::bad_request); diff --git a/toREST/include/torrents.hpp b/toREST/include/torrents.hpp index 3f513b6..64ffc76 100644 --- a/toREST/include/torrents.hpp +++ b/toREST/include/torrents.hpp @@ -18,18 +18,23 @@ void get(torrent_session &session, response resp, request req) { http_response.set_status(response_code.first); *resp << http_response; }; + if (!session.is_valid()) { return respond(http::service_unavailable); } - auto torrents = session.get_torrents(); + auto response_object = nlohmann::json::object(); + + auto torrents = session.get_torrents(); auto torrents_json = nlohmann::json::array(); for (auto &torrent : torrents) { if (torrent.is_valid()) { - torrents_json.push_back(tr::torrent::to_json(torrent)); + torrents_json.push_back(torrent::to_json(torrent)); } } + response_object["torrents"] = torrents_json; + http_response.set_status(http::ok); http_response.set_body(response_object); *resp << http_response; @@ -82,8 +87,8 @@ void del(torrent_session &session, response resp, request req) { } options = remove_files; } - - const auto handle = session.find_torrent(util::sha1::string(obj)); + + const auto handle = session.find_torrent(util::sha1::info_hash(obj)); if (handle.is_valid()) { session.remove_torrent(handle, options); @@ -194,12 +199,9 @@ void post(settings opts, torrent_session &session, response resp, request req) { params.name = obj; } - std::stringstream ss; - ss << params.info_hash; - session.async_add_torrent(params); - http_response.add_header({"Location", "/session/torrents/" + ss.str()}); + http_response.add_header({"Location", "/session/torrents/" + util::sha1::string(params.info_hash)}); return respond(http::created); } } diff --git a/toREST/include/util.hpp b/toREST/include/util.hpp index 3f6787d..ee9e243 100644 --- a/toREST/include/util.hpp +++ b/toREST/include/util.hpp @@ -41,14 +41,14 @@ public: }; struct sha1 { - static auto string(const std::string &hex_str) { + static auto info_hash(const std::string &hex_str) { libtorrent::sha1_hash sha1_hash; std::stringstream ss; ss << hex_str; ss >> sha1_hash; return sha1_hash; }; - static auto info_hash(const libtorrent::sha1_hash &info_hash) { + static auto string(const libtorrent::sha1_hash &info_hash) { std::stringstream ss; ss << info_hash; std::string res = ss.str();