Browse Source

Use torrent info with hashes for creating .torrent file (#15138)

adaptive-webui-19844
Vladimir Golovnev 3 years ago committed by GitHub
parent
commit
5d03917877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 42
      src/base/bittorrent/session.cpp
  2. 8
      src/base/bittorrent/session.h
  3. 10
      src/gui/addnewtorrentdialog.cpp
  4. 2
      src/gui/addnewtorrentdialog.ui

42
src/base/bittorrent/session.cpp

@ -2296,28 +2296,25 @@ bool Session::downloadMetadata(const MagnetUri &magnetUri)
return true; 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()) || const QString validName = Utils::Fs::toValidFileSystemName(baseName);
((folder == TorrentExportFolder::Finished) && !finishedTorrentExportDirectory().isEmpty()));
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 exportPath(folder == TorrentExportFolder::Regular ? torrentExportDirectory() : finishedTorrentExportDirectory()); const QDir exportDir {folderPath};
if (exportPath.exists() || exportPath.mkpath(exportPath.absolutePath())) if (exportDir.exists() || exportDir.mkpath(exportDir.absolutePath()))
{ {
QString newTorrentPath = exportPath.absoluteFilePath(torrentExportFilename); QString newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename);
int counter = 0; int counter = 0;
while (QFile::exists(newTorrentPath)) while (QFile::exists(newTorrentPath))
{ {
// Append number to torrent name to make it unique // Append number to torrent name to make it unique
torrentExportFilename = QString::fromLatin1("%1 %2.torrent").arg(validName).arg(++counter); torrentExportFilename = QString::fromLatin1("%1 %2.torrent").arg(validName).arg(++counter);
newTorrentPath = exportPath.absoluteFilePath(torrentExportFilename); newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename);
} }
try try
{ {
torrent->info().saveToFile(newTorrentPath); torrentInfo.saveToFile(newTorrentPath);
} }
catch (const RuntimeError &err) catch (const RuntimeError &err)
{ {
@ -3893,7 +3890,14 @@ void Session::handleTorrentMetadataReceived(TorrentImpl *const torrent)
{ {
// Copy the torrent file to the export folder // Copy the torrent file to the export folder
if (!torrentExportDirectory().isEmpty()) 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); emit torrentMetadataReceived(torrent);
} }
@ -3944,7 +3948,14 @@ void Session::handleTorrentFinished(TorrentImpl *const torrent)
// Move .torrent file to another folder // Move .torrent file to another folder
if (!finishedTorrentExportDirectory().isEmpty()) 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()) if (!hasUnfinishedTorrents())
emit allTorrentsFinished(); emit allTorrentsFinished();
@ -4452,7 +4463,10 @@ void Session::createTorrent(const lt::torrent_handle &nativeHandle)
{ {
// Copy the torrent file to the export folder // Copy the torrent file to the export folder
if (!torrentExportDirectory().isEmpty()) if (!torrentExportDirectory().isEmpty())
exportTorrentFile(torrent); {
const TorrentInfo torrentInfo {params.ltAddTorrentParams.ti};
exportTorrentFile(torrentInfo, torrentExportDirectory(), torrent->name());
}
} }
if (isAddTrackersEnabled() && !torrent->isPrivate()) if (isAddTrackersEnabled() && !torrent->isPrivate())
@ -4579,7 +4593,7 @@ void Session::handleMetadataReceivedAlert(const lt::metadata_received_alert *p)
if (downloadedMetadataIter != m_downloadedMetadata.end()) if (downloadedMetadataIter != m_downloadedMetadata.end())
{ {
TorrentInfo metadata {p->handle.torrent_file()}; const TorrentInfo metadata {p->handle.torrent_file()};
m_downloadedMetadata.erase(downloadedMetadataIter); m_downloadedMetadata.erase(downloadedMetadataIter);
--m_extraLimit; --m_extraLimit;

8
src/base/bittorrent/session.h

@ -82,12 +82,6 @@ enum DeleteOption
DeleteTorrentAndFiles DeleteTorrentAndFiles
}; };
enum TorrentExportFolder
{
Regular,
Finished
};
namespace Net namespace Net
{ {
struct DownloadResult; struct DownloadResult;
@ -613,7 +607,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 Torrent *torrent, TorrentExportFolder folder = TorrentExportFolder::Regular); void exportTorrentFile(const TorrentInfo &torrentInfo, const QString &folderPath, const QString &baseName);
void handleAlert(const lt::alert *a); void handleAlert(const lt::alert *a);
void dispatchTorrentAlert(const lt::alert *a); void dispatchTorrentAlert(const lt::alert *a);

10
src/gui/addnewtorrentdialog.cpp

@ -460,7 +460,7 @@ void AddNewTorrentDialog::saveTorrentFile()
Q_ASSERT(m_hasMetadata); Q_ASSERT(m_hasMetadata);
const QString torrentFileExtension {C_TORRENT_FILE_EXTENSION}; 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( QString path = QFileDialog::getSaveFileName(
this, tr("Save as torrent file") this, tr("Save as torrent file")
@ -679,6 +679,13 @@ void AddNewTorrentDialog::updateMetadata(const BitTorrent::TorrentInfo &metadata
// Update UI // Update UI
setupTreeview(); setupTreeview();
setMetadataProgressIndicator(false, tr("Metadata retrieval complete")); 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) void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText)
@ -687,7 +694,6 @@ void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, co
m_ui->lblMetaLoading->setVisible(true); m_ui->lblMetaLoading->setVisible(true);
m_ui->lblMetaLoading->setText(labelText); m_ui->lblMetaLoading->setText(labelText);
m_ui->progMetaLoading->setVisible(visibleIndicator); m_ui->progMetaLoading->setVisible(visibleIndicator);
m_ui->buttonSave->setVisible(!visibleIndicator);
} }
void AddNewTorrentDialog::setupTreeview() void AddNewTorrentDialog::setupTreeview()

2
src/gui/addnewtorrentdialog.ui

@ -355,7 +355,7 @@
<item row="3" column="0"> <item row="3" column="0">
<widget class="QLabel" name="labelInfohash2"> <widget class="QLabel" name="labelInfohash2">
<property name="text"> <property name="text">
<string>Info hash v2</string> <string>Info hash v2:</string>
</property> </property>
</widget> </widget>
</item> </item>

Loading…
Cancel
Save