torrent-daemon: watchdir can use .magnet files (#1328)

This commit is contained in:
Pierre Carru 2022-04-04 04:14:44 +02:00 committed by GitHub
parent d948e49da7
commit 32cff69213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 6 deletions

View File

@ -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>
@ -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;
@ -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 });
}
}