diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 4c2784011..643842b92 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -613,11 +613,14 @@ QString Session::tempPath() const return Utils::Fs::fromNativePath(m_tempPath); } -QString Session::torrentTempPath(const InfoHash &hash) const +QString Session::torrentTempPath(const TorrentInfo &torrentInfo) const { - return tempPath() - + static_cast(hash).left(7) + if ((torrentInfo.filesCount() > 1) && !torrentInfo.hasRootFolder()) + return tempPath() + + QString::fromStdString(torrentInfo.nativeInfo()->orig_files().name()) + "/"; + + return tempPath(); } bool Session::isValidCategoryName(const QString &name) @@ -1648,7 +1651,7 @@ bool Session::deleteTorrent(const QString &hash, bool deleteLocalFiles) // Remove it from session if (deleteLocalFiles) { - if (torrent->savePath(true) == torrentTempPath(torrent->hash())) { + if (torrent->savePath(true) == torrentTempPath(torrent->info())) { m_savePathsToRemove[torrent->hash()] = torrent->savePath(true); } else { @@ -1996,7 +1999,7 @@ bool Session::findIncompleteFiles(TorrentInfo &torrentInfo, QString &savePath) c bool found = findInDir(savePath, torrentInfo); if (!found && isTempPathEnabled()) { - savePath = torrentTempPath(torrentInfo.hash()); + savePath = torrentTempPath(torrentInfo); found = findInDir(savePath, torrentInfo); } @@ -3698,9 +3701,7 @@ void Session::handleTorrentRemovedAlert(libt::torrent_removed_alert *p) void Session::handleTorrentDeletedAlert(libt::torrent_deleted_alert *p) { - const QString path = m_savePathsToRemove.take(p->info_hash); - if (path == torrentTempPath(p->info_hash)) - Utils::Fs::smartRemoveEmptyFolderTree(path); + Utils::Fs::smartRemoveEmptyFolderTree(m_savePathsToRemove.take(p->info_hash)); } void Session::handleTorrentDeleteFailedAlert(libt::torrent_delete_failed_alert *p) diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 35221b657..01422b620 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -210,7 +210,7 @@ namespace BitTorrent void setTempPath(QString path); bool isTempPathEnabled() const; void setTempPathEnabled(bool enabled); - QString torrentTempPath(const InfoHash &hash) const; + QString torrentTempPath(const TorrentInfo &torrentInfo) const; static bool isValidCategoryName(const QString &name); // returns category itself and all top level categories diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 313d56b32..c35aef406 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1498,7 +1498,7 @@ void TorrentHandle::handleStorageMovedAlert(libtorrent::storage_moved_alert *p) } qDebug("Torrent is successfully moved from %s to %s", qPrintable(m_oldPath), qPrintable(m_newPath)); - if (QDir(m_oldPath) == QDir(m_session->torrentTempPath(hash()))) { + if (QDir(m_oldPath) == QDir(m_session->torrentTempPath(info()))) { qDebug() << "Removing torrent temp folder:" << m_oldPath; Utils::Fs::smartRemoveEmptyFolderTree(m_oldPath); } @@ -1926,9 +1926,9 @@ void TorrentHandle::adjustActualSavePath_impl() path = savePath(); } else { - // Moving all downloading torrents to temporary save path - path = m_session->torrentTempPath(hash()); - qDebug() << "Moving torrent to its temp save path:" << path; + // Moving all downloading torrents to temporary folder + path = m_session->torrentTempPath(info()); + qDebug() << "Moving torrent to its temporary folder:" << path; } moveStorage(Utils::Fs::toNativePath(path)); diff --git a/src/base/bittorrent/torrentinfo.cpp b/src/base/bittorrent/torrentinfo.cpp index 6a63eb78a..03eabe8c8 100644 --- a/src/base/bittorrent/torrentinfo.cpp +++ b/src/base/bittorrent/torrentinfo.cpp @@ -308,9 +308,29 @@ int BitTorrent::TorrentInfo::fileIndex(const QString& fileName) const return -1; } +bool TorrentInfo::hasRootFolder() const +{ + QString testRootFolder; + for (int i = 0; i < filesCount(); ++i) { + const QString filePath = this->filePath(i); + if (QDir::isAbsolutePath(filePath)) continue; + + const auto filePathElements = filePath.splitRef('/'); + // if at least one file has no root folder, no common root folder exists + if (filePathElements.count() <= 1) return false; + + if (testRootFolder.isEmpty()) + testRootFolder = filePathElements.at(0).toString(); + else if (testRootFolder != filePathElements.at(0)) + return false; + } + + return true; +} + void TorrentInfo::stripRootFolder() { - if (filesCount() <= 1) return; + if (!hasRootFolder()) return; libtorrent::file_storage files = m_nativeInfo->files(); diff --git a/src/base/bittorrent/torrentinfo.h b/src/base/bittorrent/torrentinfo.h index 87f92266a..6730d2f11 100644 --- a/src/base/bittorrent/torrentinfo.h +++ b/src/base/bittorrent/torrentinfo.h @@ -100,6 +100,8 @@ namespace BitTorrent PieceRange filePieces(int fileIndex) const; void renameFile(uint index, const QString &newPath); + + bool hasRootFolder() const; void stripRootFolder(); NativePtr nativeInfo() const;