Browse Source

Rename info_hash method and use it where adequate

master
Jørgen Lien Sellæg 9 years ago
parent
commit
27c5f66879
  1. 7
      toREST/include/torrent.hpp
  2. 18
      toREST/include/torrents.hpp
  3. 4
      toREST/include/util.hpp

7
toREST/include/torrent.hpp

@ -11,13 +11,10 @@ namespace tr {
namespace torrent { namespace torrent {
template <class torrent_t> template <class torrent_t>
static nlohmann::json to_json(torrent_t torrent) { 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( const auto status = torrent.status(
torrent.query_name | torrent.query_name |
torrent.query_save_path); torrent.query_save_path);
return {{"info_hash", hash}, return {{"info_hash", util::sha1::string(torrent.info_hash())},
{"paused", status.paused}, {"paused", status.paused},
{"seeding", status.is_seeding}, {"seeding", status.is_seeding},
{"state", status.state}, {"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 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()) { if (!torrent.is_valid()) {
return respond(http::bad_request); return respond(http::bad_request);

18
toREST/include/torrents.hpp

@ -18,18 +18,23 @@ void get(torrent_session &session, response resp, request req) {
http_response.set_status(response_code.first); http_response.set_status(response_code.first);
*resp << http_response; *resp << http_response;
}; };
if (!session.is_valid()) { if (!session.is_valid()) {
return respond(http::service_unavailable); return respond(http::service_unavailable);
} }
auto torrents = session.get_torrents();
auto response_object = nlohmann::json::object(); auto response_object = nlohmann::json::object();
auto torrents = session.get_torrents();
auto torrents_json = nlohmann::json::array(); auto torrents_json = nlohmann::json::array();
for (auto &torrent : torrents) { for (auto &torrent : torrents) {
if (torrent.is_valid()) { 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; response_object["torrents"] = torrents_json;
http_response.set_status(http::ok); http_response.set_status(http::ok);
http_response.set_body(response_object); http_response.set_body(response_object);
*resp << http_response; *resp << http_response;
@ -82,8 +87,8 @@ void del(torrent_session &session, response resp, request req) {
} }
options = remove_files; 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()) { if (handle.is_valid()) {
session.remove_torrent(handle, options); session.remove_torrent(handle, options);
@ -194,12 +199,9 @@ void post(settings opts, torrent_session &session, response resp, request req) {
params.name = obj; params.name = obj;
} }
std::stringstream ss;
ss << params.info_hash;
session.async_add_torrent(params); 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); return respond(http::created);
} }
} }

4
toREST/include/util.hpp

@ -41,14 +41,14 @@ public:
}; };
struct sha1 { struct sha1 {
static auto string(const std::string &hex_str) { static auto info_hash(const std::string &hex_str) {
libtorrent::sha1_hash sha1_hash; libtorrent::sha1_hash sha1_hash;
std::stringstream ss; std::stringstream ss;
ss << hex_str; ss << hex_str;
ss >> sha1_hash; ss >> sha1_hash;
return 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; std::stringstream ss;
ss << info_hash; ss << info_hash;
std::string res = ss.str(); std::string res = ss.str();

Loading…
Cancel
Save