Add unified class to represent parsed torrent metadata

* Add unified class to represent parsed torrent metadata
* Unify startup logic of "Add new torrent dialog"

PR #19301.
This commit is contained in:
Vladimir Golovnev
2023-07-21 08:40:16 +03:00
committed by GitHub
parent d554f4d44a
commit f27f2c20e0
18 changed files with 498 additions and 439 deletions

View File

@@ -21,7 +21,6 @@ add_library(qbt_base STATIC
bittorrent/loadtorrentparams.h
bittorrent/ltqbitarray.h
bittorrent/lttypecast.h
bittorrent/magneturi.h
bittorrent/nativesessionextension.h
bittorrent/nativetorrentextension.h
bittorrent/peeraddress.h
@@ -36,6 +35,7 @@ add_library(qbt_base STATIC
bittorrent/torrentcontenthandler.h
bittorrent/torrentcontentlayout.h
bittorrent/torrentcreatorthread.h
bittorrent/torrentdescriptor.h
bittorrent/torrentimpl.h
bittorrent/torrentinfo.h
bittorrent/tracker.h
@@ -120,7 +120,6 @@ add_library(qbt_base STATIC
bittorrent/filterparserthread.cpp
bittorrent/infohash.cpp
bittorrent/ltqbitarray.cpp
bittorrent/magneturi.cpp
bittorrent/nativesessionextension.cpp
bittorrent/nativetorrentextension.cpp
bittorrent/peeraddress.cpp
@@ -132,6 +131,7 @@ add_library(qbt_base STATIC
bittorrent/torrent.cpp
bittorrent/torrentcontenthandler.cpp
bittorrent/torrentcreatorthread.cpp
bittorrent/torrentdescriptor.cpp
bittorrent/torrentimpl.cpp
bittorrent/torrentinfo.cpp
bittorrent/tracker.cpp

View File

@@ -1,150 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "magneturi.h"
#include <libtorrent/bencode.hpp>
#include <libtorrent/error_code.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/sha1_hash.hpp>
#include <QRegularExpression>
#include "base/global.h"
#include "infohash.h"
namespace
{
// BEP9 Extension for Peers to Send Metadata Files
bool isV1Hash(const QString &string)
{
// There are 2 representations for BitTorrent v1 info hash:
// 1. 40 chars hex-encoded string
// == 20 (SHA-1 length in bytes) * 2 (each byte maps to 2 hex characters)
// 2. 32 chars Base32 encoded string
// == 20 (SHA-1 length in bytes) * 1.6 (the efficiency of Base32 encoding)
const int V1_HEX_SIZE = SHA1Hash::length() * 2;
const int V1_BASE32_SIZE = SHA1Hash::length() * 1.6;
return ((((string.size() == V1_HEX_SIZE))
&& !string.contains(QRegularExpression(u"[^0-9A-Fa-f]"_s)))
|| ((string.size() == V1_BASE32_SIZE)
&& !string.contains(QRegularExpression(u"[^2-7A-Za-z]"_s))));
}
bool isV2Hash(const QString &string)
{
// There are 1 representation for BitTorrent v2 info hash:
// 1. 64 chars hex-encoded string
// == 32 (SHA-2 256 length in bytes) * 2 (each byte maps to 2 hex characters)
const int V2_HEX_SIZE = SHA256Hash::length() * 2;
return (string.size() == V2_HEX_SIZE)
&& !string.contains(QRegularExpression(u"[^0-9A-Fa-f]"_s));
}
}
using namespace BitTorrent;
const int magnetUriId = qRegisterMetaType<MagnetUri>();
MagnetUri::MagnetUri(const QString &source)
: m_url(source)
{
if (source.isEmpty()) return;
if (isV2Hash(source))
m_url = u"magnet:?xt=urn:btmh:1220" + source; // 0x12 0x20 is the "multihash format" tag for the SHA-256 hashing scheme.
else if (isV1Hash(source))
m_url = u"magnet:?xt=urn:btih:" + source;
lt::error_code ec;
lt::parse_magnet_uri(m_url.toStdString(), m_addTorrentParams, ec);
if (ec) return;
m_valid = true;
#ifdef QBT_USES_LIBTORRENT2
m_infoHash = m_addTorrentParams.info_hashes;
#else
m_infoHash = m_addTorrentParams.info_hash;
#endif
m_name = QString::fromStdString(m_addTorrentParams.name);
m_trackers.reserve(static_cast<decltype(m_trackers)::size_type>(m_addTorrentParams.trackers.size()));
int tier = 0;
auto tierIter = m_addTorrentParams.tracker_tiers.cbegin();
for (const std::string &url : m_addTorrentParams.trackers)
{
if (tierIter != m_addTorrentParams.tracker_tiers.cend())
tier = *tierIter++;
m_trackers.append({QString::fromStdString(url), tier});
}
m_urlSeeds.reserve(static_cast<decltype(m_urlSeeds)::size_type>(m_addTorrentParams.url_seeds.size()));
for (const std::string &urlSeed : m_addTorrentParams.url_seeds)
m_urlSeeds.append(QString::fromStdString(urlSeed));
}
bool MagnetUri::isValid() const
{
return m_valid;
}
InfoHash MagnetUri::infoHash() const
{
return m_infoHash;
}
QString MagnetUri::name() const
{
return m_name;
}
QVector<TrackerEntry> MagnetUri::trackers() const
{
return m_trackers;
}
QVector<QUrl> MagnetUri::urlSeeds() const
{
return m_urlSeeds;
}
QString MagnetUri::url() const
{
return m_url;
}
lt::add_torrent_params MagnetUri::addTorrentParams() const
{
return m_addTorrentParams;
}

View File

@@ -58,8 +58,8 @@ enum DeleteOption
namespace BitTorrent
{
class InfoHash;
class MagnetUri;
class Torrent;
class TorrentDescriptor;
class TorrentID;
class TorrentInfo;
struct CacheStatus;
@@ -441,10 +441,9 @@ namespace BitTorrent
virtual bool isKnownTorrent(const InfoHash &infoHash) const = 0;
virtual bool addTorrent(const QString &source, const AddTorrentParams &params = {}) = 0;
virtual bool addTorrent(const MagnetUri &magnetUri, const AddTorrentParams &params = {}) = 0;
virtual bool addTorrent(const TorrentInfo &torrentInfo, const AddTorrentParams &params = {}) = 0;
virtual bool addTorrent(const TorrentDescriptor &torrentDescr, const AddTorrentParams &params = {}) = 0;
virtual bool deleteTorrent(const TorrentID &id, DeleteOption deleteOption = DeleteOption::DeleteTorrent) = 0;
virtual bool downloadMetadata(const MagnetUri &magnetUri) = 0;
virtual bool downloadMetadata(const TorrentDescriptor &torrentDescr) = 0;
virtual bool cancelDownloadMetadata(const TorrentID &id) = 0;
virtual void recursiveTorrentDownload(const TorrentID &id) = 0;

View File

@@ -101,10 +101,10 @@
#include "filterparserthread.h"
#include "loadtorrentparams.h"
#include "lttypecast.h"
#include "magneturi.h"
#include "nativesessionextension.h"
#include "portforwarderimpl.h"
#include "resumedatastorage.h"
#include "torrentdescriptor.h"
#include "torrentimpl.h"
#include "tracker.h"
@@ -2250,14 +2250,22 @@ void SessionImpl::handleDownloadFinished(const Net::DownloadResult &result)
{
case Net::DownloadStatus::Success:
emit downloadFromUrlFinished(result.url);
if (const nonstd::expected<TorrentInfo, QString> loadResult = TorrentInfo::load(result.data); loadResult)
if (const auto loadResult = TorrentDescriptor::load(result.data))
addTorrent(loadResult.value(), m_downloadedTorrents.take(result.url));
else
LogMsg(tr("Failed to load torrent. Reason: \"%1\"").arg(loadResult.error()), Log::WARNING);
break;
case Net::DownloadStatus::RedirectedToMagnet:
emit downloadFromUrlFinished(result.url);
addTorrent(MagnetUri(result.magnet), m_downloadedTorrents.take(result.url));
if (const auto parseResult = TorrentDescriptor::parse(result.magnetURI))
{
addTorrent(parseResult.value(), m_downloadedTorrents.take(result.url));
}
else
{
LogMsg(tr("Failed to load torrent. The request was redirected to invalid Magnet URI. Reason: \"%1\"")
.arg(parseResult.error()), Log::WARNING);
}
break;
default:
emit downloadFromUrlFailed(result.url, result.errorString);
@@ -2592,13 +2600,12 @@ bool SessionImpl::addTorrent(const QString &source, const AddTorrentParams &para
return true;
}
const MagnetUri magnetUri {source};
if (magnetUri.isValid())
return addTorrent(magnetUri, params);
if (const auto parseResult = TorrentDescriptor::parse(source))
return addTorrent(parseResult.value(), params);
const Path path {source};
TorrentFileGuard guard {path};
const nonstd::expected<TorrentInfo, QString> loadResult = TorrentInfo::loadFromFile(path);
const auto loadResult = TorrentDescriptor::loadFromFile(path);
if (!loadResult)
{
LogMsg(tr("Failed to load torrent. Source: \"%1\". Reason: \"%2\"").arg(source, loadResult.error()), Log::WARNING);
@@ -2609,23 +2616,12 @@ bool SessionImpl::addTorrent(const QString &source, const AddTorrentParams &para
return addTorrent(loadResult.value(), params);
}
bool SessionImpl::addTorrent(const MagnetUri &magnetUri, const AddTorrentParams &params)
bool SessionImpl::addTorrent(const TorrentDescriptor &torrentDescr, const AddTorrentParams &params)
{
if (!isRestored())
return false;
if (!magnetUri.isValid())
return false;
return addTorrent_impl(magnetUri, params);
}
bool SessionImpl::addTorrent(const TorrentInfo &torrentInfo, const AddTorrentParams &params)
{
if (!isRestored())
return false;
return addTorrent_impl(torrentInfo, params);
return addTorrent_impl(torrentDescr, params);
}
LoadTorrentParams SessionImpl::initLoadTorrentParams(const AddTorrentParams &addTorrentParams)
@@ -2690,12 +2686,12 @@ LoadTorrentParams SessionImpl::initLoadTorrentParams(const AddTorrentParams &add
}
// Add a torrent to the BitTorrent session
bool SessionImpl::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &source, const AddTorrentParams &addTorrentParams)
bool SessionImpl::addTorrent_impl(const TorrentDescriptor &source, const AddTorrentParams &addTorrentParams)
{
Q_ASSERT(isRestored());
const bool hasMetadata = std::holds_alternative<TorrentInfo>(source);
const auto infoHash = (hasMetadata ? std::get<TorrentInfo>(source).infoHash() : std::get<MagnetUri>(source).infoHash());
const bool hasMetadata = (source.info().has_value());
const auto infoHash = source.infoHash();
const auto id = TorrentID::fromInfoHash(infoHash);
// alternative ID can be useful to find existing torrent in case if hybrid torrent was added by v1 info hash
@@ -2712,7 +2708,7 @@ bool SessionImpl::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &so
if (hasMetadata)
{
// Trying to set metadata to existing torrent in case if it has none
torrent->setMetadata(std::get<TorrentInfo>(source));
torrent->setMetadata(*source.info());
}
if (!isMergeTrackersEnabled())
@@ -2721,29 +2717,16 @@ bool SessionImpl::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &so
return false;
}
const bool isPrivate = torrent->isPrivate() || (hasMetadata && std::get<TorrentInfo>(source).isPrivate());
const bool isPrivate = torrent->isPrivate() || (hasMetadata && source.info()->isPrivate());
if (isPrivate)
{
LogMsg(tr("Detected an attempt to add a duplicate torrent. Trackers cannot be merged because it is a private torrent. Torrent: %1").arg(torrent->name()));
return false;
}
if (hasMetadata)
{
const TorrentInfo &torrentInfo = std::get<TorrentInfo>(source);
// merge trackers and web seeds
torrent->addTrackers(torrentInfo.trackers());
torrent->addUrlSeeds(torrentInfo.urlSeeds());
}
else
{
const MagnetUri &magnetUri = std::get<MagnetUri>(source);
// merge trackers and web seeds
torrent->addTrackers(magnetUri.trackers());
torrent->addUrlSeeds(magnetUri.urlSeeds());
}
// merge trackers and web seeds
torrent->addTrackers(source.trackers());
torrent->addUrlSeeds(source.urlSeeds());
LogMsg(tr("Detected an attempt to add a duplicate torrent. Trackers are merged from new source. Torrent: %1").arg(torrent->name()));
return false;
@@ -2759,6 +2742,7 @@ bool SessionImpl::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &so
LoadTorrentParams loadTorrentParams = initLoadTorrentParams(addTorrentParams);
lt::add_torrent_params &p = loadTorrentParams.ltAddTorrentParams;
p = source.ltAddTorrentParams();
bool isFindingIncompleteFiles = false;
@@ -2767,7 +2751,7 @@ bool SessionImpl::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &so
if (hasMetadata)
{
const TorrentInfo &torrentInfo = std::get<TorrentInfo>(source);
const TorrentInfo &torrentInfo = *source.info();
Q_ASSERT(addTorrentParams.filePaths.isEmpty() || (addTorrentParams.filePaths.size() == torrentInfo.filesCount()));
@@ -2779,8 +2763,7 @@ bool SessionImpl::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &so
{
const Path originalRootFolder = Path::findRootFolder(filePaths);
const auto originalContentLayout = (originalRootFolder.isEmpty()
? TorrentContentLayout::NoSubfolder
: TorrentContentLayout::Subfolder);
? TorrentContentLayout::NoSubfolder : TorrentContentLayout::Subfolder);
if (loadTorrentParams.contentLayout != originalContentLayout)
{
if (loadTorrentParams.contentLayout == TorrentContentLayout::NoSubfolder)
@@ -2843,13 +2826,10 @@ bool SessionImpl::addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &so
p.file_priorities[LT::toUnderlyingType(nativeIndexes[i])] = LT::toNative(addTorrentParams.filePriorities[i]);
}
p.ti = torrentInfo.nativeInfo();
Q_ASSERT(p.ti);
}
else
{
const MagnetUri &magnetUri = std::get<MagnetUri>(source);
p = magnetUri.addTorrentParams();
if (loadTorrentParams.name.isEmpty() && !p.name.empty())
loadTorrentParams.name = QString::fromStdString(p.name);
}
@@ -3009,19 +2989,20 @@ void SessionImpl::invokeAsync(std::function<void ()> func)
// Add a torrent to libtorrent session in hidden mode
// and force it to download its metadata
bool SessionImpl::downloadMetadata(const MagnetUri &magnetUri)
bool SessionImpl::downloadMetadata(const TorrentDescriptor &torrentDescr)
{
if (!magnetUri.isValid())
Q_ASSERT(!torrentDescr.info().has_value());
if (Q_UNLIKELY(torrentDescr.info().has_value()))
return false;
const InfoHash infoHash = magnetUri.infoHash();
const InfoHash infoHash = torrentDescr.infoHash();
// We should not add torrent if it's already
// processed or adding to session
if (isKnownTorrent(infoHash))
return false;
lt::add_torrent_params p = magnetUri.addTorrentParams();
lt::add_torrent_params p = torrentDescr.ltAddTorrentParams();
if (isAddTrackersEnabled())
{
@@ -5281,8 +5262,7 @@ void SessionImpl::recursiveTorrentDownload(const TorrentID &id)
AddTorrentParams params;
// Passing the save path along to the sub torrent file
params.savePath = torrent->savePath();
const nonstd::expected<TorrentInfo, QString> loadResult = TorrentInfo::loadFromFile(torrentFullpath);
if (loadResult)
if (const auto loadResult = TorrentDescriptor::loadFromFile(torrentFullpath))
{
addTorrent(loadResult.value(), params);
}

View File

@@ -30,7 +30,6 @@
#pragma once
#include <utility>
#include <variant>
#include <vector>
#include <libtorrent/fwd.hpp>
@@ -76,9 +75,9 @@ namespace Net
namespace BitTorrent
{
class InfoHash;
class MagnetUri;
class ResumeDataStorage;
class Torrent;
class TorrentDescriptor;
class TorrentImpl;
class Tracker;
struct LoadTorrentParams;
@@ -416,10 +415,9 @@ namespace BitTorrent
bool isKnownTorrent(const InfoHash &infoHash) const override;
bool addTorrent(const QString &source, const AddTorrentParams &params = {}) override;
bool addTorrent(const MagnetUri &magnetUri, const AddTorrentParams &params = {}) override;
bool addTorrent(const TorrentInfo &torrentInfo, const AddTorrentParams &params = {}) override;
bool addTorrent(const TorrentDescriptor &torrentDescr, const AddTorrentParams &params = {}) override;
bool deleteTorrent(const TorrentID &id, DeleteOption deleteOption = DeleteTorrent) override;
bool downloadMetadata(const MagnetUri &magnetUri) override;
bool downloadMetadata(const TorrentDescriptor &torrentDescr) override;
bool cancelDownloadMetadata(const TorrentID &id) override;
void recursiveTorrentDownload(const TorrentID &id) override;
@@ -532,7 +530,7 @@ namespace BitTorrent
void endStartup(ResumeSessionContext *context);
LoadTorrentParams initLoadTorrentParams(const AddTorrentParams &addTorrentParams);
bool addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &source, const AddTorrentParams &addTorrentParams);
bool addTorrent_impl(const TorrentDescriptor &source, const AddTorrentParams &addTorrentParams);
void updateSeedingLimitTimer();
void exportTorrentFile(const Torrent *torrent, const Path &folderPath);

View File

@@ -0,0 +1,232 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "torrentdescriptor.h"
#include <libtorrent/load_torrent.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/version.hpp>
#include <libtorrent/write_resume_data.hpp>
#include <QByteArray>
#include <QDateTime>
#include <QRegularExpression>
#include <QString>
#include <QUrl>
#include "base/global.h"
#include "base/preferences.h"
#include "base/utils/io.h"
#include "infohash.h"
#include "trackerentry.h"
namespace
{
// BEP9 Extension for Peers to Send Metadata Files
bool isV1Hash(const QString &string)
{
// There are 2 representations for BitTorrent v1 info hash:
// 1. 40 chars hex-encoded string
// == 20 (SHA-1 length in bytes) * 2 (each byte maps to 2 hex characters)
// 2. 32 chars Base32 encoded string
// == 20 (SHA-1 length in bytes) * 1.6 (the efficiency of Base32 encoding)
const int V1_HEX_SIZE = SHA1Hash::length() * 2;
const int V1_BASE32_SIZE = SHA1Hash::length() * 1.6;
return ((((string.size() == V1_HEX_SIZE)) && !string.contains(QRegularExpression(u"[^0-9A-Fa-f]"_s)))
|| ((string.size() == V1_BASE32_SIZE) && !string.contains(QRegularExpression(u"[^2-7A-Za-z]"_s))));
}
bool isV2Hash(const QString &string)
{
// There are 1 representation for BitTorrent v2 info hash:
// 1. 64 chars hex-encoded string
// == 32 (SHA-2 256 length in bytes) * 2 (each byte maps to 2 hex characters)
const int V2_HEX_SIZE = SHA256Hash::length() * 2;
return (string.size() == V2_HEX_SIZE) && !string.contains(QRegularExpression(u"[^0-9A-Fa-f]"_s));
}
lt::load_torrent_limits loadTorrentLimits()
{
const auto *pref = Preferences::instance();
lt::load_torrent_limits limits;
limits.max_buffer_size = static_cast<int>(pref->getTorrentFileSizeLimit());
limits.max_decode_depth = pref->getBdecodeDepthLimit();
limits.max_decode_tokens = pref->getBdecodeTokenLimit();
return limits;
}
}
const int TORRENTDESCRIPTOR_TYPEID = qRegisterMetaType<BitTorrent::TorrentDescriptor>();
nonstd::expected<BitTorrent::TorrentDescriptor, QString>
BitTorrent::TorrentDescriptor::load(const QByteArray &data) noexcept
try
{
return TorrentDescriptor(lt::load_torrent_buffer(lt::span<const char>(data.data(), data.size()), loadTorrentLimits()));
}
catch (const lt::system_error &err)
{
return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
}
nonstd::expected<BitTorrent::TorrentDescriptor, QString>
BitTorrent::TorrentDescriptor::loadFromFile(const Path &path) noexcept
try
{
return TorrentDescriptor(lt::load_torrent_file(path.toString().toStdString(), loadTorrentLimits()));
}
catch (const lt::system_error &err)
{
return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
}
nonstd::expected<BitTorrent::TorrentDescriptor, QString>
BitTorrent::TorrentDescriptor::parse(const QString &str) noexcept
try
{
QString magnetURI = str;
if (isV2Hash(str))
magnetURI = u"magnet:?xt=urn:btmh:1220" + str; // 0x12 0x20 is the "multihash format" tag for the SHA-256 hashing scheme.
else if (isV1Hash(str))
magnetURI = u"magnet:?xt=urn:btih:" + str;
return TorrentDescriptor(lt::parse_magnet_uri(magnetURI.toStdString()));
}
catch (const lt::system_error &err)
{
return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
}
nonstd::expected<void, QString> BitTorrent::TorrentDescriptor::saveToFile(const Path &path) const
try
{
const lt::entry torrentEntry = lt::write_torrent_file(m_ltAddTorrentParams);
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, torrentEntry);
if (!result)
return result.get_unexpected();
return {};
}
catch (const lt::system_error &err)
{
return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
}
BitTorrent::TorrentDescriptor::TorrentDescriptor(lt::add_torrent_params ltAddTorrentParams)
: m_ltAddTorrentParams {std::move(ltAddTorrentParams)}
{
if (m_ltAddTorrentParams.ti && m_ltAddTorrentParams.ti->is_valid())
m_info.emplace(*m_ltAddTorrentParams.ti);
}
BitTorrent::InfoHash BitTorrent::TorrentDescriptor::infoHash() const
{
#ifdef QBT_USES_LIBTORRENT2
return m_ltAddTorrentParams.info_hashes;
#else
return m_ltAddTorrentParams.info_hash;
#endif
}
QString BitTorrent::TorrentDescriptor::name() const
{
return m_info ? m_info->name() : QString::fromStdString(m_ltAddTorrentParams.name);
}
QDateTime BitTorrent::TorrentDescriptor::creationDate() const
{
return ((m_ltAddTorrentParams.ti->creation_date() != 0)
? QDateTime::fromSecsSinceEpoch(m_ltAddTorrentParams.ti->creation_date()) : QDateTime());
}
QString BitTorrent::TorrentDescriptor::creator() const
{
return QString::fromStdString(m_ltAddTorrentParams.ti->creator());
}
QString BitTorrent::TorrentDescriptor::comment() const
{
return QString::fromStdString(m_ltAddTorrentParams.ti->comment());
}
const std::optional<BitTorrent::TorrentInfo> &BitTorrent::TorrentDescriptor::info() const
{
return m_info;
}
void BitTorrent::TorrentDescriptor::setTorrentInfo(TorrentInfo torrentInfo)
{
if (!torrentInfo.isValid())
{
m_info.reset();
m_ltAddTorrentParams.ti.reset();
}
else
{
m_info = std::move(torrentInfo);
m_ltAddTorrentParams.ti = torrentInfo.nativeInfo();
#ifdef QBT_USES_LIBTORRENT2
m_ltAddTorrentParams.info_hashes = m_ltAddTorrentParams.ti->info_hashes();
#else
m_ltAddTorrentParams.info_hash = m_ltAddTorrentParams.ti->info_hash();
#endif
}
}
QVector<BitTorrent::TrackerEntry> BitTorrent::TorrentDescriptor::trackers() const
{
QVector<TrackerEntry> ret;
ret.reserve(static_cast<decltype(ret)::size_type>(m_ltAddTorrentParams.trackers.size()));
std::size_t i = 0;
for (const std::string &tracker : m_ltAddTorrentParams.trackers)
ret.append({QString::fromStdString(tracker), m_ltAddTorrentParams.tracker_tiers[i++]});
return ret;
}
QVector<QUrl> BitTorrent::TorrentDescriptor::urlSeeds() const
{
QVector<QUrl> urlSeeds;
urlSeeds.reserve(static_cast<decltype(urlSeeds)::size_type>(m_ltAddTorrentParams.url_seeds.size()));
for (const std::string &nativeURLSeed : m_ltAddTorrentParams.url_seeds)
urlSeeds.append(QUrl(QString::fromStdString(nativeURLSeed)));
return urlSeeds;
}
const libtorrent::add_torrent_params &BitTorrent::TorrentDescriptor::ltAddTorrentParams() const
{
return m_ltAddTorrentParams;
}

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -28,40 +28,57 @@
#pragma once
#include <optional>
#include <libtorrent/add_torrent_params.hpp>
#include <QString>
#include <QUrl>
#include <QVector>
#include <QtContainerFwd>
#include <QMetaType>
#include "infohash.h"
#include "trackerentry.h"
#include "base/3rdparty/expected.hpp"
#include "base/path.h"
#include "torrentdescriptor.h"
#include "torrentinfo.h"
class QByteArray;
class QDateTime;
class QString;
class QUrl;
namespace BitTorrent
{
class MagnetUri
class InfoHash;
struct TrackerEntry;
class TorrentDescriptor
{
public:
explicit MagnetUri(const QString &source = {});
TorrentDescriptor() = default;
bool isValid() const;
InfoHash infoHash() const;
QString name() const;
QDateTime creationDate() const;
QString creator() const;
QString comment() const;
QVector<TrackerEntry> trackers() const;
QVector<QUrl> urlSeeds() const;
QString url() const;
const std::optional<TorrentInfo> &info() const;
lt::add_torrent_params addTorrentParams() const;
void setTorrentInfo(TorrentInfo torrentInfo);
static nonstd::expected<TorrentDescriptor, QString> load(const QByteArray &data) noexcept;
static nonstd::expected<TorrentDescriptor, QString> loadFromFile(const Path &path) noexcept;
static nonstd::expected<TorrentDescriptor, QString> parse(const QString &str) noexcept;
nonstd::expected<void, QString> saveToFile(const Path &path) const;
const lt::add_torrent_params &ltAddTorrentParams() const;
private:
bool m_valid = false;
QString m_url;
InfoHash m_infoHash;
QString m_name;
QVector<TrackerEntry> m_trackers;
QVector<QUrl> m_urlSeeds;
lt::add_torrent_params m_addTorrentParams;
explicit TorrentDescriptor(lt::add_torrent_params ltAddTorrentParams);
lt::add_torrent_params m_ltAddTorrentParams;
std::optional<TorrentInfo> m_info;
};
}
Q_DECLARE_METATYPE(BitTorrent::MagnetUri)
Q_DECLARE_METATYPE(BitTorrent::TorrentDescriptor)

View File

@@ -51,7 +51,7 @@
using namespace BitTorrent;
const int torrentInfoId = qRegisterMetaType<TorrentInfo>();
const int TORRENTINFO_TYPEID = qRegisterMetaType<TorrentInfo>();
TorrentInfo::TorrentInfo(const lt::torrent_info &nativeInfo)
: m_nativeInfo {std::make_shared<const lt::torrent_info>(nativeInfo)}
@@ -401,6 +401,34 @@ TorrentInfo::PieceRange TorrentInfo::filePieces(const int fileIndex) const
return makeInterval(beginIdx, endIdx);
}
bool TorrentInfo::matchesInfoHash(const InfoHash &otherInfoHash) const
{
if (!isValid())
return false;
const InfoHash thisInfoHash = infoHash();
if (thisInfoHash.v1().isValid() && otherInfoHash.v1().isValid()
&& (thisInfoHash.v1() != otherInfoHash.v1()))
{
return false;
}
if (thisInfoHash.v2().isValid() && otherInfoHash.v2().isValid()
&& (thisInfoHash.v2() != otherInfoHash.v2()))
{
return false;
}
if (!thisInfoHash.v1().isValid() && otherInfoHash.v1().isValid())
return false;
if (!thisInfoHash.v2().isValid() && otherInfoHash.v2().isValid())
return false;
return true;
}
int TorrentInfo::fileIndex(const Path &filePath) const
{
// the check whether the object is valid is not needed here

View File

@@ -93,6 +93,8 @@ namespace BitTorrent
PieceRange filePieces(const Path &filePath) const;
PieceRange filePieces(int fileIndex) const;
bool matchesInfoHash(const InfoHash &otherInfoHash) const;
std::shared_ptr<lt::torrent_info> nativeInfo() const;
QVector<lt::file_index_t> nativeIndexes() const;

View File

@@ -199,11 +199,9 @@ void Net::DownloadHandlerImpl::handleRedirection(const QUrl &newUrl)
// Redirect to magnet workaround
if (newUrlString.startsWith(u"magnet:", Qt::CaseInsensitive))
{
qDebug("Magnet redirect detected.");
m_result.status = Net::DownloadStatus::RedirectedToMagnet;
m_result.magnet = newUrlString;
m_result.magnetURI = newUrlString;
m_result.errorString = tr("Redirected to magnet URI");
finish();
return;
}

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015, 2018 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@@ -103,7 +103,7 @@ namespace Net
QString errorString;
QByteArray data;
Path filePath;
QString magnet;
QString magnetURI;
};
class DownloadHandler : public QObject

View File

@@ -41,8 +41,8 @@
#include <QVariant>
#include <QVector>
#include "../bittorrent/magneturi.h"
#include "../bittorrent/session.h"
#include "../bittorrent/torrentdescriptor.h"
#include "../asyncfilestorage.h"
#include "../global.h"
#include "../logger.h"
@@ -474,7 +474,7 @@ void AutoDownloader::processJob(const QSharedPointer<ProcessingJob> &job)
const auto torrentURL = job->articleData.value(Article::KeyTorrentURL).toString();
BitTorrent::Session::instance()->addTorrent(torrentURL, rule.addTorrentParams());
if (BitTorrent::MagnetUri(torrentURL).isValid())
if (BitTorrent::TorrentDescriptor::parse(torrentURL))
{
if (Feed *feed = Session::instance()->feedByURL(job->feedURL))
{

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2021-2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christian Kandeler, Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@@ -44,11 +44,9 @@
#include <QVariant>
#include "base/algorithm.h"
#include "base/bittorrent/magneturi.h"
#include "base/bittorrent/torrentcontentlayout.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrent.h"
#include "base/bittorrent/torrentinfo.h"
#include "base/exceptions.h"
#include "base/global.h"
#include "base/logger.h"
@@ -99,8 +97,7 @@ public slots:
void removeWatchedFolder(const Path &path);
signals:
void magnetFound(const BitTorrent::MagnetUri &magnetURI, const BitTorrent::AddTorrentParams &addTorrentParams);
void torrentFound(const BitTorrent::TorrentInfo &torrentInfo, const BitTorrent::AddTorrentParams &addTorrentParams);
void torrentFound(const BitTorrent::TorrentDescriptor &torrentDescr, const BitTorrent::AddTorrentParams &addTorrentParams);
private:
void onTimeout();
@@ -159,7 +156,6 @@ void TorrentFilesWatcher::initWorker()
m_asyncWorker = new TorrentFilesWatcher::Worker;
connect(m_asyncWorker, &TorrentFilesWatcher::Worker::magnetFound, this, &TorrentFilesWatcher::onMagnetFound);
connect(m_asyncWorker, &TorrentFilesWatcher::Worker::torrentFound, this, &TorrentFilesWatcher::onTorrentFound);
m_asyncWorker->moveToThread(m_ioThread.get());
@@ -332,16 +328,10 @@ void TorrentFilesWatcher::removeWatchedFolder(const Path &path)
}
}
void TorrentFilesWatcher::onMagnetFound(const BitTorrent::MagnetUri &magnetURI
, const BitTorrent::AddTorrentParams &addTorrentParams)
void TorrentFilesWatcher::onTorrentFound(const BitTorrent::TorrentDescriptor &torrentDescr
, const BitTorrent::AddTorrentParams &addTorrentParams)
{
BitTorrent::Session::instance()->addTorrent(magnetURI, addTorrentParams);
}
void TorrentFilesWatcher::onTorrentFound(const BitTorrent::TorrentInfo &torrentInfo
, const BitTorrent::AddTorrentParams &addTorrentParams)
{
BitTorrent::Session::instance()->addTorrent(torrentInfo, addTorrentParams);
BitTorrent::Session::instance()->addTorrent(torrentDescr, addTorrentParams);
}
TorrentFilesWatcher::Worker::Worker()
@@ -438,7 +428,10 @@ void TorrentFilesWatcher::Worker::processFolder(const Path &path, const Path &wa
while (!file.atEnd())
{
const auto line = QString::fromLatin1(file.readLine()).trimmed();
emit magnetFound(BitTorrent::MagnetUri(line), addTorrentParams);
if (const auto parseResult = BitTorrent::TorrentDescriptor::parse(line))
emit torrentFound(parseResult.value(), addTorrentParams);
else
LogMsg(tr("Invalid Magnet URI. URI: %1. Reason: %2").arg(line, parseResult.error()), Log::WARNING);
}
file.close();
@@ -446,7 +439,7 @@ void TorrentFilesWatcher::Worker::processFolder(const Path &path, const Path &wa
}
else
{
LogMsg(tr("Magnet file too big. File: %1").arg(file.errorString()));
LogMsg(tr("Magnet file too big. File: %1").arg(file.errorString()), Log::WARNING);
}
}
else
@@ -456,10 +449,9 @@ void TorrentFilesWatcher::Worker::processFolder(const Path &path, const Path &wa
}
else
{
const nonstd::expected<BitTorrent::TorrentInfo, QString> result = BitTorrent::TorrentInfo::loadFromFile(filePath);
if (result)
if (const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(filePath))
{
emit torrentFound(result.value(), addTorrentParams);
emit torrentFound(loadResult.value(), addTorrentParams);
Utils::Fs::removeFile(filePath);
}
else
@@ -496,8 +488,7 @@ void TorrentFilesWatcher::Worker::processFailedTorrents()
if (!torrentPath.exists())
return true;
const nonstd::expected<BitTorrent::TorrentInfo, QString> result = BitTorrent::TorrentInfo::loadFromFile(torrentPath);
if (result)
if (const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(torrentPath))
{
BitTorrent::AddTorrentParams addTorrentParams = options.addTorrentParams;
if (torrentPath != watchedFolderPath)
@@ -515,7 +506,7 @@ void TorrentFilesWatcher::Worker::processFailedTorrents()
}
}
emit torrentFound(result.value(), addTorrentParams);
emit torrentFound(loadResult.value(), addTorrentParams);
Utils::Fs::removeFile(torrentPath);
return true;

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2021-2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christian Kandeler, Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@@ -32,16 +32,12 @@
#include <QHash>
#include "base/bittorrent/addtorrentparams.h"
#include "base/bittorrent/torrentdescriptor.h"
#include "base/path.h"
#include "base/utils/thread.h"
class QThread;
namespace BitTorrent
{
class MagnetUri;
}
/*
* Watches the configured directories for new .torrent files in order
* to add torrents to BitTorrent session. Supports Network File System
@@ -72,8 +68,7 @@ signals:
void watchedFolderRemoved(const Path &path);
private slots:
void onMagnetFound(const BitTorrent::MagnetUri &magnetURI, const BitTorrent::AddTorrentParams &addTorrentParams);
void onTorrentFound(const BitTorrent::TorrentInfo &torrentInfo, const BitTorrent::AddTorrentParams &addTorrentParams);
void onTorrentFound(const BitTorrent::TorrentDescriptor &torrentDescr, const BitTorrent::AddTorrentParams &addTorrentParams);
private:
explicit TorrentFilesWatcher(QObject *parent = nullptr);