mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-22 20:44:15 +00:00
Add support for exporting .torrent data to buffer
Related: https://github.com/qbittorrent/qBittorrent/pull/16886#discussion_r855882018 PR #16931.
This commit is contained in:
parent
7432c4dbfe
commit
9351f66c26
@ -40,6 +40,7 @@
|
||||
#include "abstractfilestorage.h"
|
||||
|
||||
class QBitArray;
|
||||
class QByteArray;
|
||||
class QDateTime;
|
||||
class QUrl;
|
||||
|
||||
@ -301,6 +302,7 @@ namespace BitTorrent
|
||||
virtual void clearPeers() = 0;
|
||||
|
||||
virtual QString createMagnetURI() const = 0;
|
||||
virtual nonstd::expected<QByteArray, QString> exportToBuffer() const = 0;
|
||||
virtual nonstd::expected<void, QString> exportToFile(const Path &path) const = 0;
|
||||
|
||||
TorrentID id() const;
|
||||
|
@ -45,6 +45,7 @@
|
||||
#include <libtorrent/info_hash.hpp>
|
||||
#endif
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QStringList>
|
||||
@ -2250,7 +2251,7 @@ QString TorrentImpl::createMagnetURI() const
|
||||
return QString::fromStdString(lt::make_magnet_uri(m_nativeHandle));
|
||||
}
|
||||
|
||||
nonstd::expected<void, QString> TorrentImpl::exportToFile(const Path &path) const
|
||||
nonstd::expected<lt::entry, QString> TorrentImpl::exportTorrent() const
|
||||
{
|
||||
if (!hasMetadata())
|
||||
return nonstd::make_unexpected(tr("Missing metadata"));
|
||||
@ -2263,21 +2264,45 @@ nonstd::expected<void, QString> TorrentImpl::exportToFile(const Path &path) cons
|
||||
#else
|
||||
const std::shared_ptr<lt::torrent_info> torrentInfo = info().nativeInfo();
|
||||
#endif
|
||||
auto creator = lt::create_torrent(*torrentInfo);
|
||||
lt::create_torrent creator {*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();
|
||||
return creator.generate();
|
||||
}
|
||||
catch (const lt::system_error &err)
|
||||
{
|
||||
return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
|
||||
}
|
||||
|
||||
return nonstd::make_unexpected(tr("Unexpected error"));
|
||||
}
|
||||
|
||||
nonstd::expected<QByteArray, QString> TorrentImpl::exportToBuffer() const
|
||||
{
|
||||
const nonstd::expected<lt::entry, QString> preparationResult = exportTorrent();
|
||||
if (!preparationResult)
|
||||
return preparationResult.get_unexpected();
|
||||
|
||||
// usually torrent size should be smaller than 1 MB,
|
||||
// however there are >100 MB v2/hybrid torrent files out in the wild
|
||||
QByteArray buffer;
|
||||
buffer.reserve(1024 * 1024);
|
||||
lt::bencode(std::back_inserter(buffer), preparationResult.value());
|
||||
return buffer;
|
||||
}
|
||||
|
||||
nonstd::expected<void, QString> TorrentImpl::exportToFile(const Path &path) const
|
||||
{
|
||||
const nonstd::expected<lt::entry, QString> preparationResult = exportTorrent();
|
||||
if (!preparationResult)
|
||||
return preparationResult.get_unexpected();
|
||||
|
||||
const nonstd::expected<void, QString> saveResult = Utils::IO::saveToFile(path, preparationResult.value());
|
||||
if (!saveResult)
|
||||
return saveResult.get_unexpected();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -229,7 +229,8 @@ namespace BitTorrent
|
||||
void clearPeers() override;
|
||||
|
||||
QString createMagnetURI() const override;
|
||||
nonstd::expected<void, QString> exportToFile(const Path &path) const;
|
||||
nonstd::expected<QByteArray, QString> exportToBuffer() const override;
|
||||
nonstd::expected<void, QString> exportToFile(const Path &path) const override;
|
||||
|
||||
bool needSaveResumeData() const;
|
||||
|
||||
@ -283,6 +284,8 @@ namespace BitTorrent
|
||||
void endReceivedMetadataHandling(const Path &savePath, const PathList &fileNames);
|
||||
void reload();
|
||||
|
||||
nonstd::expected<lt::entry, QString> exportTorrent() const;
|
||||
|
||||
Session *const m_session;
|
||||
lt::session *m_nativeSession;
|
||||
lt::torrent_handle m_nativeHandle;
|
||||
|
Loading…
x
Reference in New Issue
Block a user