mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-22 00:17:23 -06:00
Add "Export .torrent" action
This commit is contained in:
@@ -34,6 +34,7 @@
|
||||
#include <QMetaType>
|
||||
#include <QString>
|
||||
|
||||
#include "base/3rdparty/expected.hpp"
|
||||
#include "base/pathfwd.h"
|
||||
#include "base/tagset.h"
|
||||
#include "abstractfilestorage.h"
|
||||
@@ -300,6 +301,7 @@ namespace BitTorrent
|
||||
virtual void clearPeers() = 0;
|
||||
|
||||
virtual QString createMagnetURI() const = 0;
|
||||
virtual nonstd::expected<void, QString> exportToFile(const Path &path) const = 0;
|
||||
|
||||
TorrentID id() const;
|
||||
bool isResumed() const;
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
|
||||
#include <libtorrent/address.hpp>
|
||||
#include <libtorrent/alert_types.hpp>
|
||||
#include <libtorrent/create_torrent.hpp>
|
||||
#include <libtorrent/magnet_uri.hpp>
|
||||
#include <libtorrent/session.hpp>
|
||||
#include <libtorrent/storage_defs.hpp>
|
||||
@@ -53,6 +54,7 @@
|
||||
#include "base/logger.h"
|
||||
#include "base/preferences.h"
|
||||
#include "base/utils/fs.h"
|
||||
#include "base/utils/io.h"
|
||||
#include "base/utils/string.h"
|
||||
#include "common.h"
|
||||
#include "downloadpriority.h"
|
||||
@@ -2232,6 +2234,37 @@ QString TorrentImpl::createMagnetURI() const
|
||||
return QString::fromStdString(lt::make_magnet_uri(m_nativeHandle));
|
||||
}
|
||||
|
||||
nonstd::expected<void, QString> TorrentImpl::exportToFile(const Path &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)
|
||||
{
|
||||
if (!hasMetadata()) return;
|
||||
|
||||
@@ -227,6 +227,7 @@ namespace BitTorrent
|
||||
void clearPeers() override;
|
||||
|
||||
QString createMagnetURI() const override;
|
||||
nonstd::expected<void, QString> exportToFile(const Path &path) const;
|
||||
|
||||
bool needSaveResumeData() const;
|
||||
|
||||
|
||||
@@ -38,7 +38,6 @@
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
#include <QVector>
|
||||
|
||||
#include "base/global.h"
|
||||
#include "base/path.h"
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QtContainerFwd>
|
||||
#include <QVector>
|
||||
|
||||
#include "base/3rdparty/expected.hpp"
|
||||
#include "base/indexrange.h"
|
||||
|
||||
Reference in New Issue
Block a user