Skip to content

Commit

Permalink
watch: can use .magnet files
Browse files Browse the repository at this point in the history
  • Loading branch information
piec committed Feb 21, 2022
1 parent 8e7f993 commit 3b33907
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions daemon/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,39 +217,68 @@ 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))

bool retry = false;

if (tr_str_has_suffix(name, ".torrent"))
{
if (!tr_ctorSetMetainfoFromFile(ctor, filename.c_str(), nullptr))
{
retry = true;
}
}
else if (tr_str_has_suffix(name, ".magnet"))
{
auto content = std::vector<char>{};
if (!tr_loadFile(content, filename))
{
tr_logAddInfo("cannot read file \"%s\"", name);
retry = true;
}
else
{
content.push_back(0); // string terminal 0
auto data = content.data();
if (!tr_ctorSetMetainfoFromMagnetLink(ctor, data, nullptr))
{
retry = true;
}
}
}

if (retry)
{
tr_ctorFree(ctor);
return TR_WATCHDIR_RETRY;
}

if (tr_torrentNew(ctor, nullptr) == nullptr)
{
tr_logAddError("Unable to add .torrent file \"%s\"", name);
tr_logAddError("Unable to add watched file \"%s\"", name);
}
else
{
bool trash = false;
bool const test = tr_ctorGetDeleteSource(ctor, &trash);

tr_logAddInfo("Parsing .torrent file successful \"%s\"", name);
tr_logAddInfo("Parsing watched file successful \"%s\"", name);

if (test && trash)
{
tr_error* error = nullptr;

tr_logAddInfo("Deleting input .torrent file \"%s\"", name);
tr_logAddInfo("Deleting input watched file \"%s\"", name);

if (!tr_sys_path_remove(filename.c_str(), &error))
{
tr_logAddError("Error deleting .torrent file: %s", error->message);
tr_logAddError("Error deleting watched file: %s", error->message);
tr_error_free(error);
}
}
Expand Down

0 comments on commit 3b33907

Please sign in to comment.