Don't fail because of existing files when exporting torrent files

PR #23315.
Closes #23017.
This commit is contained in:
Vladimir Golovnev
2025-09-28 21:08:00 +03:00
committed by GitHub
parent 42786b2afc
commit 84224c1bbf
2 changed files with 11 additions and 11 deletions

View File

@@ -3179,7 +3179,7 @@ void SessionImpl::exportTorrentFile(const Torrent *torrent, const Path &folderPa
while (newTorrentPath.exists()) while (newTorrentPath.exists())
{ {
// Append number to torrent name to make it unique // Append number to torrent name to make it unique
torrentExportFilename = u"%1 %2.torrent"_s.arg(validName).arg(++counter); torrentExportFilename = u"%1 (%2).torrent"_s.arg(validName).arg(++counter);
newTorrentPath = folderPath / Path(torrentExportFilename); newTorrentPath = folderPath / Path(torrentExportFilename);
} }

View File

@@ -836,21 +836,21 @@ void TransferListWidget::exportTorrent()
bool hasError = false; bool hasError = false;
for (const BitTorrent::Torrent *torrent : torrents) for (const BitTorrent::Torrent *torrent : torrents)
{ {
const QString validName = Utils::Fs::toValidFileName(torrent->name(), u"_"_s); const QString validName = Utils::Fs::toValidFileName(torrent->name());
const Path filePath = savePath / Path(validName + u".torrent"); QString torrentExportFilename = u"%1.torrent"_s.arg(validName);
if (filePath.exists()) Path newTorrentPath = savePath / Path(torrentExportFilename);
int counter = 0;
while (newTorrentPath.exists())
{ {
LogMsg(errorMsg.arg(torrent->name(), filePath.toString(), tr("A file with the same name already exists")) , Log::WARNING); // Append number to torrent name to make it unique
hasError = true; torrentExportFilename = u"%1 (%2).torrent"_s.arg(validName).arg(++counter);
continue; newTorrentPath = savePath / Path(torrentExportFilename);
} }
const nonstd::expected<void, QString> result = torrent->exportToFile(filePath); if (const nonstd::expected<void, QString> result = torrent->exportToFile(newTorrentPath); !result)
if (!result)
{ {
LogMsg(errorMsg.arg(torrent->name(), filePath.toString(), result.error()) , Log::WARNING); LogMsg(errorMsg.arg(torrent->name(), newTorrentPath.toString(), result.error()) , Log::WARNING);
hasError = true; hasError = true;
continue;
} }
} }