Add trackers in exported .torrent files

Co-authored-by: Chocobo1 <Chocobo1@users.noreply.github.com>

PR #17018.
This commit is contained in:
Vladimir Golovnev 2022-05-12 08:12:19 +03:00 committed by GitHub
commit 7ad667e8d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 33 deletions

View file

@ -2351,9 +2351,9 @@ bool Session::downloadMetadata(const MagnetUri &magnetUri)
return true; return true;
} }
void Session::exportTorrentFile(const TorrentInfo &torrentInfo, const QString &folderPath, const QString &baseName) void Session::exportTorrentFile(const Torrent *torrent, const QString &folderPath)
{ {
const QString validName = Utils::Fs::toValidFileSystemName(baseName); const QString validName = Utils::Fs::toValidFileSystemName(torrent->name());
QString torrentExportFilename = QString::fromLatin1("%1.torrent").arg(validName); QString torrentExportFilename = QString::fromLatin1("%1.torrent").arg(validName);
const QDir exportDir {folderPath}; const QDir exportDir {folderPath};
if (exportDir.exists() || exportDir.mkpath(exportDir.absolutePath())) if (exportDir.exists() || exportDir.mkpath(exportDir.absolutePath()))
@ -2367,11 +2367,11 @@ void Session::exportTorrentFile(const TorrentInfo &torrentInfo, const QString &f
newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename); newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename);
} }
const nonstd::expected<void, QString> result = torrentInfo.saveToFile(newTorrentPath); const nonstd::expected<void, QString> result = torrent->exportToFile(newTorrentPath);
if (!result) if (!result)
{ {
LogMsg(tr("Couldn't export torrent metadata file '%1'. Reason: %2.") LogMsg(tr("Failed to export torrent. Torrent: \"%1\". Destination: \"%2\". Reason: \"%3\"")
.arg(newTorrentPath, result.error()), Log::WARNING); .arg(torrent->name(), newTorrentPath, result.error()), Log::WARNING);
} }
} }
} }
@ -3978,17 +3978,8 @@ void Session::handleTorrentUrlSeedsRemoved(TorrentImpl *const torrent, const QVe
void Session::handleTorrentMetadataReceived(TorrentImpl *const torrent) void Session::handleTorrentMetadataReceived(TorrentImpl *const torrent)
{ {
// Copy the torrent file to the export folder
if (!torrentExportDirectory().isEmpty()) if (!torrentExportDirectory().isEmpty())
{ exportTorrentFile(torrent, torrentExportDirectory());
#ifdef QBT_USES_LIBTORRENT2
const std::shared_ptr<lt::torrent_info> completeTorrentInfo = torrent->nativeHandle().torrent_file_with_hashes();
const TorrentInfo torrentInfo {*(completeTorrentInfo ? completeTorrentInfo : torrent->nativeHandle().torrent_file())};
#else
const TorrentInfo torrentInfo {*torrent->nativeHandle().torrent_file()};
#endif
exportTorrentFile(torrentInfo, torrentExportDirectory(), torrent->name());
}
emit torrentMetadataReceived(torrent); emit torrentMetadataReceived(torrent);
} }
@ -4035,17 +4026,8 @@ void Session::handleTorrentFinished(TorrentImpl *const torrent)
} }
} }
// Move .torrent file to another folder
if (!finishedTorrentExportDirectory().isEmpty()) if (!finishedTorrentExportDirectory().isEmpty())
{ exportTorrentFile(torrent, finishedTorrentExportDirectory());
#ifdef QBT_USES_LIBTORRENT2
const std::shared_ptr<lt::torrent_info> completeTorrentInfo = torrent->nativeHandle().torrent_file_with_hashes();
const TorrentInfo torrentInfo {*(completeTorrentInfo ? completeTorrentInfo : torrent->nativeHandle().torrent_file())};
#else
const TorrentInfo torrentInfo {*torrent->nativeHandle().torrent_file()};
#endif
exportTorrentFile(torrentInfo, finishedTorrentExportDirectory(), torrent->name());
}
if (!hasUnfinishedTorrents()) if (!hasUnfinishedTorrents())
emit allTorrentsFinished(); emit allTorrentsFinished();
@ -4777,12 +4759,8 @@ void Session::createTorrent(const lt::torrent_handle &nativeHandle)
// The following is useless for newly added magnet // The following is useless for newly added magnet
if (hasMetadata) if (hasMetadata)
{ {
// Copy the torrent file to the export folder
if (!torrentExportDirectory().isEmpty()) if (!torrentExportDirectory().isEmpty())
{ exportTorrentFile(torrent, torrentExportDirectory());
const TorrentInfo torrentInfo {*params.ltAddTorrentParams.ti};
exportTorrentFile(torrentInfo, torrentExportDirectory(), torrent->name());
}
} }
if (isAddTrackersEnabled() && !torrent->isPrivate()) if (isAddTrackersEnabled() && !torrent->isPrivate())

View file

@ -609,7 +609,7 @@ namespace BitTorrent
bool addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &source, const AddTorrentParams &addTorrentParams); bool addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &source, const AddTorrentParams &addTorrentParams);
void updateSeedingLimitTimer(); void updateSeedingLimitTimer();
void exportTorrentFile(const TorrentInfo &torrentInfo, const QString &folderPath, const QString &baseName); void exportTorrentFile(const Torrent *torrent, const QString &folderPath);
void handleAlert(const lt::alert *a); void handleAlert(const lt::alert *a);
void dispatchTorrentAlert(const lt::alert *a); void dispatchTorrentAlert(const lt::alert *a);

View file

@ -29,10 +29,11 @@
#pragma once #pragma once
#include <QtContainerFwd>
#include <QMetaType> #include <QMetaType>
#include <QString> #include <QString>
#include <QtContainerFwd>
#include "base/3rdparty/expected.hpp"
#include "base/tagset.h" #include "base/tagset.h"
#include "abstractfilestorage.h" #include "abstractfilestorage.h"
@ -294,6 +295,7 @@ namespace BitTorrent
virtual void clearPeers() = 0; virtual void clearPeers() = 0;
virtual QString createMagnetURI() const = 0; virtual QString createMagnetURI() const = 0;
virtual nonstd::expected<void, QString> exportToFile(const QString &path) const = 0;
TorrentID id() const; TorrentID id() const;
bool isResumed() const; bool isResumed() const;

View file

@ -35,6 +35,7 @@
#include <libtorrent/address.hpp> #include <libtorrent/address.hpp>
#include <libtorrent/alert_types.hpp> #include <libtorrent/alert_types.hpp>
#include <libtorrent/create_torrent.hpp>
#include <libtorrent/magnet_uri.hpp> #include <libtorrent/magnet_uri.hpp>
#include <libtorrent/session.hpp> #include <libtorrent/session.hpp>
#include <libtorrent/storage_defs.hpp> #include <libtorrent/storage_defs.hpp>
@ -55,6 +56,7 @@
#include "base/logger.h" #include "base/logger.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/io.h"
#include "base/utils/string.h" #include "base/utils/string.h"
#include "common.h" #include "common.h"
#include "downloadpriority.h" #include "downloadpriority.h"
@ -2248,6 +2250,37 @@ QString TorrentImpl::createMagnetURI() const
return QString::fromStdString(lt::make_magnet_uri(m_nativeHandle)); return QString::fromStdString(lt::make_magnet_uri(m_nativeHandle));
} }
nonstd::expected<void, QString> TorrentImpl::exportToFile(const QString &path) const
{
if (!hasMetadata())
return nonstd::make_unexpected(tr("Missing metadata"));
try
{
#ifdef QBT_USES_LIBTORRENT2
const std::shared_ptr<lt::torrent_info> completeTorrentInfo = m_nativeHandle.torrent_file_with_hashes();
const std::shared_ptr<lt::torrent_info> torrentInfo = (completeTorrentInfo ? completeTorrentInfo : info().nativeInfo());
#else
const std::shared_ptr<lt::torrent_info> torrentInfo = info().nativeInfo();
#endif
auto creator = lt::create_torrent(*torrentInfo);
for (const TrackerEntry &entry : asConst(trackers()))
creator.add_tracker(entry.url.toStdString(), entry.tier);
const lt::entry torrentEntry = creator.generate();
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, torrentEntry);
if (!result)
return result.get_unexpected();
}
catch (const lt::system_error &err)
{
return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
}
return {};
}
void TorrentImpl::prioritizeFiles(const QVector<DownloadPriority> &priorities) void TorrentImpl::prioritizeFiles(const QVector<DownloadPriority> &priorities)
{ {
if (!hasMetadata()) return; if (!hasMetadata()) return;

View file

@ -224,6 +224,7 @@ namespace BitTorrent
void clearPeers() override; void clearPeers() override;
QString createMagnetURI() const override; QString createMagnetURI() const override;
nonstd::expected<void, QString> exportToFile(const QString &path) const;
bool needSaveResumeData() const; bool needSaveResumeData() const;

View file

@ -38,7 +38,6 @@
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QUrl> #include <QUrl>
#include <QVector>
#include "base/global.h" #include "base/global.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"

View file

@ -32,6 +32,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QtContainerFwd> #include <QtContainerFwd>
#include <QVector>
#include "base/3rdparty/expected.hpp" #include "base/3rdparty/expected.hpp"
#include "base/indexrange.h" #include "base/indexrange.h"