diff --git a/src/base/bittorrent/torrentcreatorthread.cpp b/src/base/bittorrent/torrentcreatorthread.cpp index d59feefd7..bb2dfecab 100644 --- a/src/base/bittorrent/torrentcreatorthread.cpp +++ b/src/base/bittorrent/torrentcreatorthread.cpp @@ -38,8 +38,12 @@ #include #include +#include #include +#include +#include +#include "base/global.h" #include "base/utils/fs.h" #include "base/utils/misc.h" #include "base/utils/string.h" @@ -86,9 +90,46 @@ void TorrentCreatorThread::run() emit updateProgress(0); try { - libt::file_storage fs; + const QString parentPath = Utils::Fs::branchPath(m_params.inputPath) + "/"; + // Adding files to the torrent - libt::add_files(fs, Utils::Fs::toNativePath(m_params.inputPath).toStdString(), fileFilter); + libt::file_storage fs; + if (QFileInfo(m_params.inputPath).isFile()) { + libt::add_files(fs, Utils::Fs::toNativePath(m_params.inputPath).toStdString(), fileFilter); + } + else { + // need to sort the file names by natural sort order + QStringList dirs = {m_params.inputPath}; + + QDirIterator dirIter(m_params.inputPath, (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories); + while (dirIter.hasNext()) { + dirIter.next(); + dirs += dirIter.filePath(); + } + std::sort(dirs.begin(), dirs.end(), Utils::String::naturalLessThan); + + QStringList fileNames; + QHash fileSizeMap; + + for (const auto &dir : qAsConst(dirs)) { + QStringList tmpNames; // natural sort files within each dir + + QDirIterator fileIter(dir, QDir::Files); + while (fileIter.hasNext()) { + fileIter.next(); + + const QString relFilePath = fileIter.filePath().mid(parentPath.length()); + tmpNames += relFilePath; + fileSizeMap[relFilePath] = fileIter.fileInfo().size(); + } + + std::sort(tmpNames.begin(), tmpNames.end(), Utils::String::naturalLessThan); + fileNames += tmpNames; + } + + for (const auto &fileName : qAsConst(fileNames)) + fs.add_file(fileName.toStdString(), fileSizeMap[fileName]); + } if (isInterruptionRequested()) return; @@ -113,7 +154,6 @@ void TorrentCreatorThread::run() if (isInterruptionRequested()) return; // calculate the hash for all pieces - const QString parentPath = Utils::Fs::branchPath(m_params.inputPath) + "/"; libt::set_piece_hashes(newTorrent, Utils::Fs::toNativePath(parentPath).toStdString() , [this, &newTorrent](const int n) { sendProgressSignal(n, newTorrent.num_pieces()); }); // Set qBittorrent as creator and add user comment to