From 7cc5a3e0508cec82f5ff9746cb7be5f9dcd57629 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Tue, 1 Dec 2015 17:41:56 +0300 Subject: [PATCH] Improve torrent export feature. Closes #4205. --- src/core/bittorrent/session.cpp | 58 +++++++-------------------------- src/core/bittorrent/session.h | 1 - src/core/utils/fs.cpp | 14 ++++---- src/core/utils/fs.h | 2 +- 4 files changed, 21 insertions(+), 54 deletions(-) diff --git a/src/core/bittorrent/session.cpp b/src/core/bittorrent/session.cpp index b9171ca6e..df45acd0c 100644 --- a/src/core/bittorrent/session.cpp +++ b/src/core/bittorrent/session.cpp @@ -466,7 +466,8 @@ void Session::configure() m_torrentExportEnabled = torrentExportEnabled; if (m_torrentExportEnabled) { qDebug("Torrent export is enabled, exporting the current torrents"); - exportTorrentFiles(pref->getTorrentExportDir()); + for (auto torrent: m_torrents) + exportTorrentFile(torrent); } } @@ -1151,57 +1152,22 @@ void Session::exportTorrentFile(TorrentHandle *const torrent, TorrentExportFolde Q_ASSERT(((folder == TorrentExportFolder::Regular) && m_torrentExportEnabled) || ((folder == TorrentExportFolder::Finished) && m_finishedTorrentExportEnabled)); + QString validName = Utils::Fs::toValidFileSystemName(torrent->name()); QString torrentFilename = QString("%1.torrent").arg(torrent->hash()); + QString torrentExportFilename = QString("%1.torrent").arg(validName); QString torrentPath = QDir(m_resumeFolderPath).absoluteFilePath(torrentFilename); QDir exportPath(folder == TorrentExportFolder::Regular ? Preferences::instance()->getTorrentExportDir() : Preferences::instance()->getFinishedTorrentExportDir()); if (exportPath.exists() || exportPath.mkpath(exportPath.absolutePath())) { - QString newTorrentPath = exportPath.absoluteFilePath(torrentFilename); - if (QFile::exists(newTorrentPath) && Utils::Fs::sameFiles(torrentPath, newTorrentPath)) { - // Append hash to torrent name to make it unique - newTorrentPath = exportPath.absoluteFilePath(torrent->name() + "-" + torrentFilename); - } - QFile::copy(torrentPath, newTorrentPath); - } -} - -void Session::exportTorrentFiles(QString path) -{ - // NOTE: Maybe create files from current metadata here? - Q_ASSERT(m_torrentExportEnabled); - - QDir exportDir(path); - if (!exportDir.exists()) { - if (!exportDir.mkpath(exportDir.absolutePath())) { - Logger::instance()->addMessage(tr("Error: Could not create torrent export directory: '%1'").arg(exportDir.absolutePath()), Log::CRITICAL); - return; - } - } - - QDir resumeDataDir(m_resumeFolderPath); - foreach (TorrentHandle *const torrent, m_torrents) { - if (!torrent->isValid()) { - Logger::instance()->addMessage(tr("Torrent Export: torrent is invalid, skipping..."), Log::CRITICAL); - continue; + QString newTorrentPath = exportPath.absoluteFilePath(torrentExportFilename); + int counter = 0; + while (QFile::exists(newTorrentPath) && !Utils::Fs::sameFiles(torrentPath, newTorrentPath)) { + // Append number to torrent name to make it unique + torrentExportFilename = QString("%1 %2.torrent").arg(validName).arg(++counter); + newTorrentPath = exportPath.absoluteFilePath(torrentExportFilename); } - const QString srcPath(resumeDataDir.absoluteFilePath(QString("%1.torrent").arg(torrent->hash()))); - if (QFile::exists(srcPath)) { - QString dstPath = exportDir.absoluteFilePath(QString("%1.torrent").arg(torrent->name())); - if (QFile::exists(dstPath)) { - if (!Utils::Fs::sameFiles(srcPath, dstPath)) { - dstPath = exportDir.absoluteFilePath(QString("%1-%2.torrent").arg(torrent->name()).arg(torrent->hash())); - } - else { - qDebug("Torrent Export: Destination file exists, skipping..."); - continue; - } - } - qDebug("Export Torrent: %s -> %s", qPrintable(srcPath), qPrintable(dstPath)); - QFile::copy(srcPath, dstPath); - } - else { - Logger::instance()->addMessage(tr("Error: could not export torrent '%1', maybe it has not metadata yet.").arg(torrent->hash()), Log::CRITICAL); - } + if (!QFile::exists(newTorrentPath)) + QFile::copy(torrentPath, newTorrentPath); } } diff --git a/src/core/bittorrent/session.h b/src/core/bittorrent/session.h index 1fe5bd981..156f3a0e9 100644 --- a/src/core/bittorrent/session.h +++ b/src/core/bittorrent/session.h @@ -293,7 +293,6 @@ namespace BitTorrent void updateRatioTimer(); void exportTorrentFile(TorrentHandle *const torrent, TorrentExportFolder folder = TorrentExportFolder::Regular); - void exportTorrentFiles(QString path); void saveTorrentResumeData(TorrentHandle *const torrent); void handleAlert(libtorrent::alert *a); diff --git a/src/core/utils/fs.cpp b/src/core/utils/fs.cpp index 053c614be..30728d501 100644 --- a/src/core/utils/fs.cpp +++ b/src/core/utils/fs.cpp @@ -250,13 +250,15 @@ bool Utils::Fs::sameFiles(const QString& path1, const QString& path2) return same; } -QString Utils::Fs::toValidFileSystemName(QString filename) +QString Utils::Fs::toValidFileSystemName(const QString &filename) { - qDebug("toValidFSName: %s", qPrintable(filename)); - const QRegExp regex("[\\\\/:?\"*<>|]"); - filename.replace(regex, " "); - qDebug("toValidFSName, result: %s", qPrintable(filename)); - return filename.trimmed(); + static const QRegExp regex("[\\\\/:?\"*<>|]"); + + QString validName = filename.trimmed(); + validName.replace(regex, " "); + qDebug() << "toValidFileSystemName:" << filename << "=>" << validName; + + return validName; } bool Utils::Fs::isValidFileSystemName(const QString& filename) diff --git a/src/core/utils/fs.h b/src/core/utils/fs.h index cb7db0228..9f04a2726 100644 --- a/src/core/utils/fs.h +++ b/src/core/utils/fs.h @@ -48,7 +48,7 @@ namespace Utils QString folderName(const QString& file_path); qint64 computePathSize(const QString& path); bool sameFiles(const QString& path1, const QString& path2); - QString toValidFileSystemName(QString filename); + QString toValidFileSystemName(const QString &filename); bool isValidFileSystemName(const QString& filename); qlonglong freeDiskSpaceOnPath(QString path); QString branchPath(const QString& file_path, QString* removed = 0);