1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-11 07:18:08 +00:00

Improve path extension handling

PR #16867.
This commit is contained in:
Vladimir Golovnev 2022-04-14 09:43:07 +03:00 committed by GitHub
parent 669b67e666
commit 7377974731
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 15 deletions

View File

@ -208,8 +208,7 @@ void CustomDiskIOThread::handleCompleteFiles(lt::storage_index_t storage, const
if (filePath.hasExtension(QB_EXT)) if (filePath.hasExtension(QB_EXT))
{ {
const Path incompleteFilePath = savePath / filePath; const Path incompleteFilePath = savePath / filePath;
Path completeFilePath = incompleteFilePath; const Path completeFilePath = incompleteFilePath.removedExtension(QB_EXT);
completeFilePath.removeExtension(QB_EXT);
if (completeFilePath.exists()) if (completeFilePath.exists())
{ {
Utils::Fs::removeFile(incompleteFilePath); Utils::Fs::removeFile(incompleteFilePath);
@ -274,8 +273,7 @@ void CustomStorage::handleCompleteFiles(const Path &savePath)
if (filePath.hasExtension(QB_EXT)) if (filePath.hasExtension(QB_EXT))
{ {
const Path incompleteFilePath = savePath / filePath; const Path incompleteFilePath = savePath / filePath;
Path completeFilePath = incompleteFilePath; const Path completeFilePath = incompleteFilePath.removedExtension(QB_EXT);
completeFilePath.removeExtension();
if (completeFilePath.exists()) if (completeFilePath.exists())
{ {
Utils::Fs::removeFile(incompleteFilePath); Utils::Fs::removeFile(incompleteFilePath);

View File

@ -294,8 +294,7 @@ TorrentImpl::TorrentImpl(Session *session, lt::session *nativeSession
const lt::file_index_t nativeIndex = m_torrentInfo.nativeIndexes().at(i); const lt::file_index_t nativeIndex = m_torrentInfo.nativeIndexes().at(i);
m_indexMap[nativeIndex] = i; m_indexMap[nativeIndex] = i;
Path filePath {fileStorage.file_path(nativeIndex)}; const auto filePath = Path(fileStorage.file_path(nativeIndex)).removedExtension(QB_EXT);
filePath.removeExtension(QB_EXT);
m_filePaths.append(filePath); m_filePaths.append(filePath);
const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]); const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]);
@ -1520,11 +1519,10 @@ void TorrentImpl::endReceivedMetadataHandling(const Path &savePath, const PathLi
{ {
const auto nativeIndex = nativeIndexes.at(i); const auto nativeIndex = nativeIndexes.at(i);
Path filePath = fileNames.at(i); const Path filePath = fileNames.at(i);
p.renamed_files[nativeIndex] = filePath.toString().toStdString(); p.renamed_files[nativeIndex] = filePath.toString().toStdString();
filePath.removeExtension(QB_EXT); m_filePaths.append(filePath.removedExtension(QB_EXT));
m_filePaths.append(filePath);
const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]); const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]);
m_filePriorities.append(priority); m_filePriorities.append(priority);

View File

@ -151,7 +151,7 @@ QString Path::extension() const
return ((dotIndex == -1) ? QString() : filename.mid(dotIndex).toString()); return ((dotIndex == -1) ? QString() : filename.mid(dotIndex).toString());
} }
bool Path::hasExtension(const QString &ext) const bool Path::hasExtension(const QStringView ext) const
{ {
Q_ASSERT(ext.startsWith(u'.') && (ext.size() >= 2)); Q_ASSERT(ext.startsWith(u'.') && (ext.size() >= 2));
@ -186,12 +186,17 @@ Path Path::removedExtension() const
return createUnchecked(m_pathStr.chopped(extension().size())); return createUnchecked(m_pathStr.chopped(extension().size()));
} }
void Path::removeExtension(const QString &ext) void Path::removeExtension(const QStringView ext)
{ {
if (hasExtension(ext)) if (hasExtension(ext))
m_pathStr.chop(ext.size()); m_pathStr.chop(ext.size());
} }
Path Path::removedExtension(const QStringView ext) const
{
return (hasExtension(ext) ? createUnchecked(m_pathStr.chopped(ext.size())) : *this);
}
QString Path::data() const QString Path::data() const
{ {
return m_pathStr; return m_pathStr;

View File

@ -58,10 +58,11 @@ public:
QString filename() const; QString filename() const;
QString extension() const; QString extension() const;
bool hasExtension(const QString &ext) const; bool hasExtension(QStringView ext) const;
void removeExtension(); void removeExtension();
Path removedExtension() const; Path removedExtension() const;
void removeExtension(const QString &ext); void removeExtension(QStringView ext);
Path removedExtension(QStringView ext) const;
bool hasAncestor(const Path &other) const; bool hasAncestor(const Path &other) const;
Path relativePathOf(const Path &childPath) const; Path relativePathOf(const Path &childPath) const;

View File

@ -392,8 +392,7 @@ void SearchPluginManager::pluginDownloadFinished(const Net::DownloadResult &resu
{ {
const Path filePath = result.filePath; const Path filePath = result.filePath;
Path pluginPath {QUrl(result.url).path()}; const auto pluginPath = Path(QUrl(result.url).path()).removedExtension();
pluginPath.removeExtension(); // Remove extension
installPlugin_impl(pluginPath.filename(), filePath); installPlugin_impl(pluginPath.filename(), filePath);
Utils::Fs::removeFile(filePath); Utils::Fs::removeFile(filePath);
} }