Skip to content

Commit

Permalink
Use torrent info with hashes for creating .torrent file
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Jul 1, 2021
1 parent 261f601 commit bf6d510
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
42 changes: 28 additions & 14 deletions src/base/bittorrent/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2296,28 +2296,25 @@ bool Session::downloadMetadata(const MagnetUri &magnetUri)
return true;
}

void Session::exportTorrentFile(const Torrent *torrent, TorrentExportFolder folder)
void Session::exportTorrentFile(const TorrentInfo &torrentInfo, const QString &folderPath, const QString &baseName)
{
Q_ASSERT(((folder == TorrentExportFolder::Regular) && !torrentExportDirectory().isEmpty()) ||
((folder == TorrentExportFolder::Finished) && !finishedTorrentExportDirectory().isEmpty()));

const QString validName = Utils::Fs::toValidFileSystemName(torrent->name());
const QString validName = Utils::Fs::toValidFileSystemName(baseName);
QString torrentExportFilename = QString::fromLatin1("%1.torrent").arg(validName);
const QDir exportPath(folder == TorrentExportFolder::Regular ? torrentExportDirectory() : finishedTorrentExportDirectory());
if (exportPath.exists() || exportPath.mkpath(exportPath.absolutePath()))
const QDir exportDir {folderPath};
if (exportDir.exists() || exportDir.mkpath(exportDir.absolutePath()))
{
QString newTorrentPath = exportPath.absoluteFilePath(torrentExportFilename);
QString newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename);
int counter = 0;
while (QFile::exists(newTorrentPath))
{
// Append number to torrent name to make it unique
torrentExportFilename = QString::fromLatin1("%1 %2.torrent").arg(validName).arg(++counter);
newTorrentPath = exportPath.absoluteFilePath(torrentExportFilename);
newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename);
}

try
{
torrent->info().saveToFile(newTorrentPath);
torrentInfo.saveToFile(newTorrentPath);
}
catch (const RuntimeError &err)
{
Expand Down Expand Up @@ -3893,7 +3890,14 @@ void Session::handleTorrentMetadataReceived(TorrentImpl *const torrent)
{
// Copy the torrent file to the export folder
if (!torrentExportDirectory().isEmpty())
exportTorrentFile(torrent);
{
#if (LIBTORRENT_VERSION_NUM >= 20000)
const TorrentInfo torrentInfo {torrent->nativeHandle().torrent_file_with_hashes()};
#else
const TorrentInfo torrentInfo {torrent->nativeHandle().torrent_file()};
#endif
exportTorrentFile(torrentInfo, torrentExportDirectory(), torrent->name());
}

emit torrentMetadataReceived(torrent);
}
Expand Down Expand Up @@ -3944,7 +3948,14 @@ void Session::handleTorrentFinished(TorrentImpl *const torrent)

// Move .torrent file to another folder
if (!finishedTorrentExportDirectory().isEmpty())
exportTorrentFile(torrent, TorrentExportFolder::Finished);
{
#if (LIBTORRENT_VERSION_NUM >= 20000)
const TorrentInfo torrentInfo {torrent->nativeHandle().torrent_file_with_hashes()};
#else
const TorrentInfo torrentInfo {torrent->nativeHandle().torrent_file()};
#endif
exportTorrentFile(torrentInfo, finishedTorrentExportDirectory(), torrent->name());
}

if (!hasUnfinishedTorrents())
emit allTorrentsFinished();
Expand Down Expand Up @@ -4452,7 +4463,10 @@ void Session::createTorrent(const lt::torrent_handle &nativeHandle)
{
// Copy the torrent file to the export folder
if (!torrentExportDirectory().isEmpty())
exportTorrentFile(torrent);
{
const TorrentInfo torrentInfo {params.ltAddTorrentParams.ti};
exportTorrentFile(torrentInfo, torrentExportDirectory(), torrent->name());
}
}

if (isAddTrackersEnabled() && !torrent->isPrivate())
Expand Down Expand Up @@ -4579,7 +4593,7 @@ void Session::handleMetadataReceivedAlert(const lt::metadata_received_alert *p)

if (downloadedMetadataIter != m_downloadedMetadata.end())
{
TorrentInfo metadata {p->handle.torrent_file()};
const TorrentInfo metadata {p->handle.torrent_file()};

m_downloadedMetadata.erase(downloadedMetadataIter);
--m_extraLimit;
Expand Down
8 changes: 1 addition & 7 deletions src/base/bittorrent/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ enum DeleteOption
DeleteTorrentAndFiles
};

enum TorrentExportFolder
{
Regular,
Finished
};

namespace Net
{
struct DownloadResult;
Expand Down Expand Up @@ -613,7 +607,7 @@ namespace BitTorrent
bool addTorrent_impl(const std::variant<MagnetUri, TorrentInfo> &source, const AddTorrentParams &addTorrentParams);

void updateSeedingLimitTimer();
void exportTorrentFile(const Torrent *torrent, TorrentExportFolder folder = TorrentExportFolder::Regular);
void exportTorrentFile(const TorrentInfo &torrentInfo, const QString &folderPath, const QString &baseName);

void handleAlert(const lt::alert *a);
void dispatchTorrentAlert(const lt::alert *a);
Expand Down
10 changes: 8 additions & 2 deletions src/gui/addnewtorrentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void AddNewTorrentDialog::saveTorrentFile()
Q_ASSERT(m_hasMetadata);

const QString torrentFileExtension {C_TORRENT_FILE_EXTENSION};
const QString filter {QString{"Torrent file (*%1)"}.arg(torrentFileExtension)};
const QString filter {tr("Torrent file (*%1)").arg(torrentFileExtension)};

QString path = QFileDialog::getSaveFileName(
this, tr("Save as torrent file")
Expand Down Expand Up @@ -679,6 +679,13 @@ void AddNewTorrentDialog::updateMetadata(const BitTorrent::TorrentInfo &metadata
// Update UI
setupTreeview();
setMetadataProgressIndicator(false, tr("Metadata retrieval complete"));

m_ui->buttonSave->setVisible(true);
if (m_torrentInfo.infoHash().v2().isValid())
{
m_ui->buttonSave->setEnabled(false);
m_ui->buttonSave->setToolTip(tr("Cannot create v2 torrent until its data is fully downloaded."));
}
}

void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText)
Expand All @@ -687,7 +694,6 @@ void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, co
m_ui->lblMetaLoading->setVisible(true);
m_ui->lblMetaLoading->setText(labelText);
m_ui->progMetaLoading->setVisible(visibleIndicator);
m_ui->buttonSave->setVisible(!visibleIndicator);
}

void AddNewTorrentDialog::setupTreeview()
Expand Down
2 changes: 1 addition & 1 deletion src/gui/addnewtorrentdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@
<item row="3" column="0">
<widget class="QLabel" name="labelInfohash2">
<property name="text">
<string>Info hash v2</string>
<string>Info hash v2:</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit bf6d510

Please sign in to comment.