diff --git a/src/base/bittorrent/torrent.h b/src/base/bittorrent/torrent.h index 3c8869d6e..b4e6b9d9a 100644 --- a/src/base/bittorrent/torrent.h +++ b/src/base/bittorrent/torrent.h @@ -125,8 +125,8 @@ namespace BitTorrent virtual qlonglong wastedSize() const = 0; virtual QString currentTracker() const = 0; - // 1. savePath() - the path where all the files and subfolders of torrent are stored (as always). - // 2. rootPath() - absolute path of torrent file tree (save path + first item from 1st torrent file path). + // 1. savePath() - the path where all the files and subfolders of torrent are stored. + // 2. rootPath() - absolute path of torrent file tree (first common subfolder of torrent files); empty string if torrent has no root folder. // 3. contentPath() - absolute path of torrent content (root path for multifile torrents, absolute file path for singlefile torrents). // // These methods have 'actual' parameter (defaults to false) which allow to get actual or final path variant. @@ -166,7 +166,7 @@ namespace BitTorrent // | A | /home/user/torrents/torrentA | /home/user/torrents/torrentA | // | A*| | /home/user/torrents | // | B | /home/user/torrents/torrentB | /home/user/torrents/torrentB/subdir1/file1 | - // | C | /home/user/torrents/file1 | /home/user/torrents/file1 | + // | C | | /home/user/torrents/file1 | virtual QString savePath(bool actual = false) const = 0; virtual QString rootPath(bool actual = false) const = 0; diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index f2f65ab4d..9f4599e9e 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -397,12 +397,11 @@ QString TorrentImpl::rootPath(bool actual) const if (!hasMetadata()) return {}; - const QString firstFilePath = filePath(0); - const int slashIndex = firstFilePath.indexOf('/'); - if (slashIndex >= 0) - return QDir(savePath(actual)).absoluteFilePath(firstFilePath.left(slashIndex)); - else - return QDir(savePath(actual)).absoluteFilePath(firstFilePath); + const QString relativeRootPath = m_torrentInfo.rootFolder(); + if (relativeRootPath.isEmpty()) + return {}; + + return QDir(savePath(actual)).absoluteFilePath(relativeRootPath); } QString TorrentImpl::contentPath(const bool actual) const @@ -413,10 +412,8 @@ QString TorrentImpl::contentPath(const bool actual) const if (filesCount() == 1) return QDir(savePath(actual)).absoluteFilePath(filePath(0)); - if (m_torrentInfo.hasRootFolder()) - return rootPath(actual); - - return savePath(actual); + const QString rootPath = this->rootPath(actual); + return (rootPath.isEmpty() ? savePath(actual) : rootPath); } bool TorrentImpl::isAutoTMMEnabled() const