Skip to content

Commit

Permalink
torrent-daemon: watchdir can use .magnet files (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
piec committed Apr 4, 2022
1 parent d948e49 commit 32cff69
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions daemon/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <libtransmission/log.h>
#include <libtransmission/tr-getopt.h>
#include <libtransmission/tr-macros.h>
#include <libtransmission/tr-strbuf.h>
#include <libtransmission/utils.h>
#include <libtransmission/variant.h>
#include <libtransmission/version.h>
Expand Down Expand Up @@ -228,14 +229,49 @@ static auto onFileAdded(tr_watchdir_t dir, char const* name, void* vsession)
{
auto const* session = static_cast<tr_session const*>(vsession);

if (!tr_str_has_suffix(name, ".torrent"))
if (!tr_str_has_suffix(name, ".torrent") && !tr_str_has_suffix(name, ".magnet"))
{
return TR_WATCHDIR_IGNORE;
}

auto filename = tr_strvPath(tr_watchdir_get_path(dir), name);
tr_ctor* ctor = tr_ctorNew(session);
if (!tr_ctorSetMetainfoFromFile(ctor, filename.c_str(), nullptr))
auto const filename = tr_pathbuf{ tr_watchdir_get_path(dir), '/', name };
tr_ctor* const ctor = tr_ctorNew(session);

bool retry = false;

if (tr_str_has_suffix(name, ".torrent"))
{
if (!tr_ctorSetMetainfoFromFile(ctor, filename, nullptr))
{
retry = true;
}
}
else // ".magnet" suffix
{
auto content = std::vector<char>{};
tr_error* error = nullptr;
if (!tr_loadFile(filename, content, &error))
{
tr_logAddWarn(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),
fmt::arg("path", name),
fmt::arg("error", error->message),
fmt::arg("error_code", error->code)));
tr_error_free(error);
retry = true;
}
else
{
content.push_back('\0'); // zero-terminated string
auto const* data = std::data(content);
if (!tr_ctorSetMetainfoFromMagnetLink(ctor, data, nullptr))
{
retry = true;
}
}
}

if (retry)
{
tr_ctorFree(ctor);
return TR_WATCHDIR_RETRY;
Expand Down Expand Up @@ -268,8 +304,7 @@ static auto onFileAdded(tr_watchdir_t dir, char const* name, void* vsession)
}
else
{
auto const new_filename = filename + ".added";
tr_sys_path_rename(filename.c_str(), new_filename.c_str());
tr_sys_path_rename(filename, tr_pathbuf{ filename, ".added"sv });
}
}

Expand Down

0 comments on commit 32cff69

Please sign in to comment.