Improve content file/folder names handling

Move files/folders renaming functions to core classes.
Query file/folder for renaming by its current path.
Add ability to rename content folders from WebAPI/WebUI.
This commit is contained in:
Vladimir Golovnev (Glassez) 2020-12-17 11:57:06 +03:00
commit 4453e7fcdd
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
16 changed files with 309 additions and 299 deletions

View file

@ -1251,44 +1251,44 @@ void TorrentsController::tagsAction()
void TorrentsController::renameFileAction()
{
requireParams({"hash", "id", "name"});
requireParams({"hash", "oldPath", "newPath"});
const QString hash = params()["hash"];
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent)
throw APIError(APIErrorType::NotFound);
QString newName = params()["name"].trimmed();
if (newName.isEmpty())
throw APIError(APIErrorType::BadParams, tr("Name cannot be empty"));
if (!Utils::Fs::isValidFileSystemName(newName))
throw APIError(APIErrorType::Conflict, tr("Name is not valid"));
if (newName.endsWith(QB_EXT))
newName.chop(QB_EXT.size());
const QString oldPath = params()["oldPath"];
const QString newPath = params()["newPath"];
bool ok = false;
const int fileIndex = params()["id"].toInt(&ok);
if (!ok || (fileIndex < 0) || (fileIndex >= torrent->filesCount()))
throw APIError(APIErrorType::Conflict, tr("ID is not valid"));
const QString oldFileName = torrent->fileName(fileIndex);
const QString oldFilePath = torrent->filePath(fileIndex);
const bool useFilenameExt = BitTorrent::Session::instance()->isAppendExtensionEnabled()
&& (torrent->filesProgress()[fileIndex] != 1);
const QString newFileName = (newName + (useFilenameExt ? QB_EXT : QString()));
const QString newFilePath = (oldFilePath.leftRef(oldFilePath.size() - oldFileName.size()) + newFileName);
if (oldFileName == newFileName)
return;
// check if new name is already used
for (int i = 0; i < torrent->filesCount(); ++i)
try
{
if (i == fileIndex) continue;
if (Utils::Fs::sameFileNames(torrent->filePath(i), newFilePath))
throw APIError(APIErrorType::Conflict, tr("Name is already in use"));
torrent->renameFile(oldPath, newPath);
}
catch (const RuntimeError &error)
{
throw APIError(APIErrorType::Conflict, error.message());
}
}
void TorrentsController::renameFolderAction()
{
requireParams({"hash", "oldPath", "newPath"});
const QString hash = params()["hash"];
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent)
throw APIError(APIErrorType::NotFound);
const QString oldPath = params()["oldPath"];
const QString newPath = params()["newPath"];
try
{
torrent->renameFolder(oldPath, newPath);
}
catch (const RuntimeError &error)
{
throw APIError(APIErrorType::Conflict, error.message());
}
torrent->renameFile(fileIndex, newFilePath);
}