Allow to save downloaded metadata as torrent file

This commit is contained in:
Vladimir Golovnev (Glassez)
2020-02-19 13:19:51 +03:00
committed by sledgehammer999
parent 5fdd069f66
commit 703330c40d
8 changed files with 110 additions and 63 deletions

View File

@@ -38,15 +38,16 @@
#include <libtorrent/address.hpp>
#include <libtorrent/alert_types.hpp>
#include <libtorrent/bencode.hpp>
#include <libtorrent/create_torrent.hpp>
#include <libtorrent/entry.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/time.hpp>
#include <libtorrent/version.hpp>
#if (LIBTORRENT_VERSION_NUM >= 10200)
#include <libtorrent/storage_defs.hpp>
#include <libtorrent/write_resume_data.hpp>
#else
#include <libtorrent/storage.hpp>
#endif
#include <QBitArray>
@@ -1533,29 +1534,6 @@ void TorrentHandle::renameFile(const int index, const QString &name)
m_nativeHandle.rename_file(LTFileIndex {index}, Utils::Fs::toNativePath(name).toStdString());
}
bool TorrentHandle::saveTorrentFile(const QString &path)
{
if (!m_torrentInfo.isValid()) return false;
#if (LIBTORRENT_VERSION_NUM < 10200)
const lt::create_torrent torrentCreator = lt::create_torrent(*(m_torrentInfo.nativeInfo()), true);
#else
const lt::create_torrent torrentCreator = lt::create_torrent(*(m_torrentInfo.nativeInfo()));
#endif
const lt::entry torrentEntry = torrentCreator.generate();
QByteArray out;
out.reserve(1024 * 1024); // most torrent file sizes are under 1 MB
lt::bencode(std::back_inserter(out), torrentEntry);
if (out.isEmpty())
return false;
QFile torrentFile(path);
if (torrentFile.open(QIODevice::WriteOnly))
return (torrentFile.write(out) == out.size());
return false;
}
void TorrentHandle::handleStateUpdate(const lt::torrent_status &nativeStatus)
{
updateStatus(nativeStatus);