Browse Source

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

adaptive-webui-19844
Vladimir Golovnev 4 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) @@ -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)
{
@ -3893,7 +3890,14 @@ void Session::handleTorrentMetadataReceived(TorrentImpl *const torrent) @@ -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);
}
@ -3944,7 +3948,14 @@ void Session::handleTorrentFinished(TorrentImpl *const torrent) @@ -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();
@ -4452,7 +4463,10 @@ void Session::createTorrent(const lt::torrent_handle &nativeHandle) @@ -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())
@ -4579,7 +4593,7 @@ void Session::handleMetadataReceivedAlert(const lt::metadata_received_alert *p) @@ -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;

8
src/base/bittorrent/session.h

@ -82,12 +82,6 @@ enum DeleteOption @@ -82,12 +82,6 @@ enum DeleteOption
DeleteTorrentAndFiles
};
enum TorrentExportFolder
{
Regular,
Finished
};
namespace Net
{
struct DownloadResult;
@ -613,7 +607,7 @@ namespace BitTorrent @@ -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);

10
src/gui/addnewtorrentdialog.cpp

@ -460,7 +460,7 @@ void AddNewTorrentDialog::saveTorrentFile() @@ -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")
@ -679,6 +679,13 @@ void AddNewTorrentDialog::updateMetadata(const BitTorrent::TorrentInfo &metadata @@ -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)
@ -687,7 +694,6 @@ void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, co @@ -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()

2
src/gui/addnewtorrentdialog.ui

@ -355,7 +355,7 @@ @@ -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>

Loading…
Cancel
Save