1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-02-05 03:14:44 +00:00

Handle .!qB extension behind the scenes

PR #15920.
This commit is contained in:
Vladimir Golovnev 2022-01-08 08:45:50 +03:00 committed by GitHub
parent 9f6130cbaa
commit f44341a8e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 63 additions and 105 deletions

View File

@ -32,7 +32,6 @@
#include <QHash> #include <QHash>
#include <QVector> #include <QVector>
#include "base/bittorrent/common.h"
#include "base/exceptions.h" #include "base/exceptions.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
@ -46,10 +45,6 @@ namespace
{ {
bool areSameFileNames(QString first, QString second) bool areSameFileNames(QString first, QString second)
{ {
if (first.endsWith(QB_EXT, Qt::CaseInsensitive))
first.chop(QB_EXT.size());
if (second.endsWith(QB_EXT, Qt::CaseInsensitive))
second.chop(QB_EXT.size());
return QString::compare(first, second, CASE_SENSITIVITY) == 0; return QString::compare(first, second, CASE_SENSITIVITY) == 0;
} }
} }
@ -85,15 +80,7 @@ void BitTorrent::AbstractFileStorage::renameFile(const QString &oldPath, const Q
if (renamingFileIndex < 0) if (renamingFileIndex < 0)
throw RuntimeError {tr("No such file: '%1'.").arg(oldFilePath)}; throw RuntimeError {tr("No such file: '%1'.").arg(oldFilePath)};
const auto extAdjusted = [](const QString &path, const bool needExt) -> QString renameFile(renamingFileIndex, newFilePath);
{
if (path.endsWith(QB_EXT, Qt::CaseInsensitive) == needExt)
return path;
return (needExt ? (path + QB_EXT) : (path.left(path.size() - QB_EXT.size())));
};
renameFile(renamingFileIndex, extAdjusted(newFilePath, filePath(renamingFileIndex).endsWith(QB_EXT, Qt::CaseInsensitive)));
} }
void BitTorrent::AbstractFileStorage::renameFolder(const QString &oldPath, const QString &newPath) void BitTorrent::AbstractFileStorage::renameFolder(const QString &oldPath, const QString &newPath)

View File

@ -193,8 +193,8 @@ namespace BitTorrent
virtual qreal ratioLimit() const = 0; virtual qreal ratioLimit() const = 0;
virtual int seedingTimeLimit() const = 0; virtual int seedingTimeLimit() const = 0;
virtual QString actualFilePath(int index) const = 0;
virtual QStringList filePaths() const = 0; virtual QStringList filePaths() const = 0;
virtual QStringList absoluteFilePaths() const = 0;
virtual QVector<DownloadPriority> filePriorities() const = 0; virtual QVector<DownloadPriority> filePriorities() const = 0;
virtual TorrentInfo info() const = 0; virtual TorrentInfo info() const = 0;

View File

@ -280,7 +280,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);
const QString filePath = Utils::Fs::toUniformPath(QString::fromStdString(fileStorage.file_path(nativeIndex))); const QString filePath = Utils::Fs::toUniformPath(QString::fromStdString(fileStorage.file_path(nativeIndex)));
m_filePaths.append(filePath); m_filePaths.append(filePath.endsWith(QB_EXT, Qt::CaseInsensitive) ? filePath.chopped(QB_EXT.size()) : filePath);
} }
} }
@ -798,6 +798,12 @@ QString TorrentImpl::filePath(const int index) const
return m_filePaths.at(index); return m_filePaths.at(index);
} }
QString TorrentImpl::actualFilePath(const int index) const
{
const auto nativeIndex = m_torrentInfo.nativeIndexes().at(index);
return QString::fromStdString(m_nativeHandle.torrent_file()->files().file_path(nativeIndex));
}
qlonglong TorrentImpl::fileSize(const int index) const qlonglong TorrentImpl::fileSize(const int index) const
{ {
return m_torrentInfo.fileSize(index); return m_torrentInfo.fileSize(index);
@ -808,20 +814,6 @@ QStringList TorrentImpl::filePaths() const
return m_filePaths; return m_filePaths;
} }
// Return a list of absolute paths corresponding
// to all files in a torrent
QStringList TorrentImpl::absoluteFilePaths() const
{
if (!hasMetadata()) return {};
const QDir saveDir {actualStorageLocation()};
QStringList res;
res.reserve(filesCount());
for (int i = 0; i < filesCount(); ++i)
res << Utils::Fs::expandPathAbs(saveDir.absoluteFilePath(filePath(i)));
return res;
}
QVector<DownloadPriority> TorrentImpl::filePriorities() const QVector<DownloadPriority> TorrentImpl::filePriorities() const
{ {
if (!hasMetadata()) if (!hasMetadata())
@ -1494,14 +1486,19 @@ void TorrentImpl::fileSearchFinished(const QString &savePath, const QStringList
void TorrentImpl::endReceivedMetadataHandling(const QString &savePath, const QStringList &fileNames) void TorrentImpl::endReceivedMetadataHandling(const QString &savePath, const QStringList &fileNames)
{ {
Q_ASSERT(m_filePaths.isEmpty());
lt::add_torrent_params &p = m_ltAddTorrentParams; lt::add_torrent_params &p = m_ltAddTorrentParams;
const std::shared_ptr<lt::torrent_info> metadata = std::const_pointer_cast<lt::torrent_info>(m_nativeHandle.torrent_file()); const std::shared_ptr<lt::torrent_info> metadata = std::const_pointer_cast<lt::torrent_info>(m_nativeHandle.torrent_file());
m_torrentInfo = TorrentInfo(*metadata); m_torrentInfo = TorrentInfo(*metadata);
m_filePaths = fileNames;
const auto nativeIndexes = m_torrentInfo.nativeIndexes(); const auto nativeIndexes = m_torrentInfo.nativeIndexes();
for (int i = 0; i < fileNames.size(); ++i) for (int i = 0; i < fileNames.size(); ++i)
p.renamed_files[nativeIndexes[i]] = fileNames[i].toStdString(); {
const QString filePath = fileNames.at(i);
m_filePaths.append(filePath.endsWith(QB_EXT, Qt::CaseInsensitive) ? filePath.chopped(QB_EXT.size()) : filePath);
p.renamed_files[nativeIndexes[i]] = filePath.toStdString();
}
p.save_path = Utils::Fs::toNativePath(savePath).toStdString(); p.save_path = Utils::Fs::toNativePath(savePath).toStdString();
p.ti = metadata; p.ti = metadata;
@ -1865,31 +1862,35 @@ void TorrentImpl::handleFileRenamedAlert(const lt::file_renamed_alert *p)
const QString oldFilePath = m_filePaths.at(fileIndex); const QString oldFilePath = m_filePaths.at(fileIndex);
const QString newFilePath = Utils::Fs::toUniformPath(p->new_name()); const QString newFilePath = Utils::Fs::toUniformPath(p->new_name());
m_filePaths[fileIndex] = newFilePath; // Check if ".!qB" extension was just added or removed
if ((oldFilePath != newFilePath) && (oldFilePath != newFilePath.chopped(QB_EXT.size())))
{
m_filePaths[fileIndex] = newFilePath;
QList<QStringView> oldPathParts = QStringView(oldFilePath).split('/', Qt::SkipEmptyParts); QList<QStringView> oldPathParts = QStringView(oldFilePath).split('/', Qt::SkipEmptyParts);
oldPathParts.removeLast(); // drop file name part oldPathParts.removeLast(); // drop file name part
QList<QStringView> newPathParts = QStringView(newFilePath).split('/', Qt::SkipEmptyParts); QList<QStringView> newPathParts = QStringView(newFilePath).split('/', Qt::SkipEmptyParts);
newPathParts.removeLast(); // drop file name part newPathParts.removeLast(); // drop file name part
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
const Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive; const Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive;
#else #else
const Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive; const Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive;
#endif #endif
int pathIdx = 0; int pathIdx = 0;
while ((pathIdx < oldPathParts.size()) && (pathIdx < newPathParts.size())) while ((pathIdx < oldPathParts.size()) && (pathIdx < newPathParts.size()))
{ {
if (oldPathParts[pathIdx].compare(newPathParts[pathIdx], caseSensitivity) != 0) if (oldPathParts[pathIdx].compare(newPathParts[pathIdx], caseSensitivity) != 0)
break; break;
++pathIdx; ++pathIdx;
} }
for (int i = (oldPathParts.size() - 1); i >= pathIdx; --i) for (int i = (oldPathParts.size() - 1); i >= pathIdx; --i)
{ {
QDir().rmdir(savePath() + Utils::String::join(oldPathParts, QString::fromLatin1("/"))); QDir().rmdir(savePath() + Utils::String::join(oldPathParts, QString::fromLatin1("/")));
oldPathParts.removeLast(); oldPathParts.removeLast();
}
} }
--m_renameCount; --m_renameCount;
@ -1922,13 +1923,12 @@ void TorrentImpl::handleFileCompletedAlert(const lt::file_completed_alert *p)
qDebug("A file completed download in torrent \"%s\"", qUtf8Printable(name())); qDebug("A file completed download in torrent \"%s\"", qUtf8Printable(name()));
if (m_session->isAppendExtensionEnabled()) if (m_session->isAppendExtensionEnabled())
{ {
QString name = filePath(fileIndex); const QString path = filePath(fileIndex);
if (name.endsWith(QB_EXT)) const QString actualPath = actualFilePath(fileIndex);
if (actualPath != path)
{ {
const QString oldName = name; qDebug("Renaming %s to %s", qUtf8Printable(actualPath), qUtf8Printable(path));
name.chop(QB_EXT.size()); renameFile(fileIndex, path);
qDebug("Renaming %s to %s", qUtf8Printable(oldName), qUtf8Printable(name));
renameFile(fileIndex, name);
} }
} }
} }
@ -2046,24 +2046,23 @@ void TorrentImpl::manageIncompleteFiles()
for (int i = 0; i < filesCount(); ++i) for (int i = 0; i < filesCount(); ++i)
{ {
QString name = filePath(i); const QString path = filePath(i);
const QString actualPath = actualFilePath(i);
if (isAppendExtensionEnabled && (fileSize(i) > 0) && (fp[i] < 1)) if (isAppendExtensionEnabled && (fileSize(i) > 0) && (fp[i] < 1))
{ {
if (!name.endsWith(QB_EXT, Qt::CaseInsensitive)) const QString wantedPath = path + QB_EXT;
if (actualPath != wantedPath)
{ {
const QString newName = name + QB_EXT; qDebug() << "Renaming" << actualPath << "to" << wantedPath;
qDebug() << "Renaming" << name << "to" << newName; renameFile(i, wantedPath);
renameFile(i, newName);
} }
} }
else else
{ {
if (name.endsWith(QB_EXT, Qt::CaseInsensitive)) if (actualPath != path)
{ {
const QString oldName = name; qDebug() << "Renaming" << actualPath << "to" << path;
name.chop(QB_EXT.size()); renameFile(i, path);
qDebug() << "Renaming" << oldName << "to" << name;
renameFile(i, name);
} }
} }
} }

View File

@ -127,9 +127,9 @@ namespace BitTorrent
int seedingTimeLimit() const override; int seedingTimeLimit() const override;
QString filePath(int index) const override; QString filePath(int index) const override;
QString actualFilePath(int index) const override;
qlonglong fileSize(int index) const override; qlonglong fileSize(int index) const override;
QStringList filePaths() const override; QStringList filePaths() const override;
QStringList absoluteFilePaths() const override;
QVector<DownloadPriority> filePriorities() const override; QVector<DownloadPriority> filePriorities() const override;
TorrentInfo info() const override; TorrentInfo info() const override;

View File

@ -59,7 +59,6 @@
#include <QStorageInfo> #include <QStorageInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include "base/bittorrent/common.h"
#include "base/global.h" #include "base/global.h"
QString Utils::Fs::toNativePath(const QString &path) QString Utils::Fs::toNativePath(const QString &path)
@ -82,10 +81,7 @@ QString Utils::Fs::resolvePath(const QString &relativePath, const QString &baseP
QString Utils::Fs::fileExtension(const QString &filename) QString Utils::Fs::fileExtension(const QString &filename)
{ {
const QString name = filename.endsWith(QB_EXT) return QMimeDatabase().suffixForFileName(filename);
? filename.chopped(QB_EXT.length())
: filename;
return QMimeDatabase().suffixForFileName(name);
} }
QString Utils::Fs::fileName(const QString &filePath) QString Utils::Fs::fileName(const QString &filePath)

View File

@ -28,6 +28,7 @@
#include "previewselectdialog.h" #include "previewselectdialog.h"
#include <QDir>
#include <QFile> #include <QFile>
#include <QHeaderView> #include <QHeaderView>
#include <QMessageBox> #include <QMessageBox>
@ -36,7 +37,6 @@
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QTableView> #include <QTableView>
#include "base/bittorrent/common.h"
#include "base/bittorrent/torrent.h" #include "base/bittorrent/torrent.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
@ -91,9 +91,7 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, const BitTorrent::Torr
const QVector<qreal> fp = torrent->filesProgress(); const QVector<qreal> fp = torrent->filesProgress();
for (int i = 0; i < torrent->filesCount(); ++i) for (int i = 0; i < torrent->filesCount(); ++i)
{ {
QString fileName = Utils::Fs::fileName(torrent->filePath(i)); const QString fileName = Utils::Fs::fileName(torrent->filePath(i));
if (fileName.endsWith(QB_EXT))
fileName.chop(QB_EXT.length());
if (Utils::Misc::isPreviewable(fileName)) if (Utils::Misc::isPreviewable(fileName))
{ {
int row = m_previewListModel->rowCount(); int row = m_previewListModel->rowCount();
@ -128,9 +126,9 @@ void PreviewSelectDialog::previewButtonClicked()
// Flush data // Flush data
m_torrent->flushCache(); m_torrent->flushCache();
const QStringList absolutePaths = m_torrent->absoluteFilePaths();
// Only one file should be selected // Only one file should be selected
const QString path = absolutePaths.at(selectedIndexes.at(0).data().toInt()); const int fileIndex = selectedIndexes.at(0).data().toInt();
const QString path = QDir(m_torrent->actualStorageLocation()).absoluteFilePath(m_torrent->actualFilePath(fileIndex));
// File // File
if (!QFile::exists(path)) if (!QFile::exists(path))
{ {

View File

@ -28,7 +28,6 @@
#include "torrentcontentmodelfile.h" #include "torrentcontentmodelfile.h"
#include "base/bittorrent/common.h"
#include "torrentcontentmodelfolder.h" #include "torrentcontentmodelfolder.h"
TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulonglong fileSize, TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulonglong fileSize,
@ -39,11 +38,6 @@ TorrentContentModelFile::TorrentContentModelFile(const QString &fileName, qulong
Q_ASSERT(parent); Q_ASSERT(parent);
m_name = fileName; m_name = fileName;
// Do not display incomplete extensions
if (m_name.endsWith(QB_EXT))
m_name.chop(4);
m_size = fileSize; m_size = fileSize;
} }

View File

@ -30,7 +30,6 @@
#include <QVariant> #include <QVariant>
#include "base/bittorrent/common.h"
#include "base/global.h" #include "base/global.h"
TorrentContentModelFolder::TorrentContentModelFolder(const QString &name, TorrentContentModelFolder *parent) TorrentContentModelFolder::TorrentContentModelFolder(const QString &name, TorrentContentModelFolder *parent)
@ -38,9 +37,6 @@ TorrentContentModelFolder::TorrentContentModelFolder(const QString &name, Torren
{ {
Q_ASSERT(parent); Q_ASSERT(parent);
m_name = name; m_name = name;
// Do not display incomplete extensions
if (m_name.endsWith(QB_EXT))
m_name.chop(4);
} }
TorrentContentModelFolder::TorrentContentModelFolder(const QVector<QString> &data) TorrentContentModelFolder::TorrentContentModelFolder(const QVector<QString> &data)

View File

@ -43,7 +43,6 @@
#include <QVector> #include <QVector>
#include <QWheelEvent> #include <QWheelEvent>
#include "base/bittorrent/common.h"
#include "base/bittorrent/session.h" #include "base/bittorrent/session.h"
#include "base/bittorrent/torrent.h" #include "base/bittorrent/torrent.h"
#include "base/bittorrent/trackerentry.h" #include "base/bittorrent/trackerentry.h"
@ -93,9 +92,7 @@ namespace
for (const QString &filePath : asConst(torrent->filePaths())) for (const QString &filePath : asConst(torrent->filePaths()))
{ {
QString fileName = Utils::Fs::fileName(filePath); const QString fileName = Utils::Fs::fileName(filePath);
if (fileName.endsWith(QB_EXT))
fileName.chop(QB_EXT.length());
if (Utils::Misc::isPreviewable(fileName)) if (Utils::Misc::isPreviewable(fileName))
return true; return true;
} }

View File

@ -40,7 +40,6 @@
#include <QUrl> #include <QUrl>
#include "base/bittorrent/categoryoptions.h" #include "base/bittorrent/categoryoptions.h"
#include "base/bittorrent/common.h"
#include "base/bittorrent/downloadpriority.h" #include "base/bittorrent/downloadpriority.h"
#include "base/bittorrent/infohash.h" #include "base/bittorrent/infohash.h"
#include "base/bittorrent/peeraddress.h" #include "base/bittorrent/peeraddress.h"
@ -565,14 +564,10 @@ void TorrentsController::filesAction()
{KEY_FILE_PROGRESS, fp[index]}, {KEY_FILE_PROGRESS, fp[index]},
{KEY_FILE_PRIORITY, static_cast<int>(priorities[index])}, {KEY_FILE_PRIORITY, static_cast<int>(priorities[index])},
{KEY_FILE_SIZE, torrent->fileSize(index)}, {KEY_FILE_SIZE, torrent->fileSize(index)},
{KEY_FILE_AVAILABILITY, fileAvailability[index]} {KEY_FILE_AVAILABILITY, fileAvailability[index]},
{KEY_FILE_NAME, Utils::Fs::toUniformPath(torrent->filePath(index))}
}; };
QString fileName = torrent->filePath(index);
if (fileName.endsWith(QB_EXT, Qt::CaseInsensitive))
fileName.chop(QB_EXT.size());
fileDict[KEY_FILE_NAME] = Utils::Fs::toUniformPath(fileName);
const BitTorrent::TorrentInfo::PieceRange idx = info.filePieces(index); const BitTorrent::TorrentInfo::PieceRange idx = info.filePieces(index);
fileDict[KEY_FILE_PIECE_RANGE] = QJsonArray {idx.first(), idx.last()}; fileDict[KEY_FILE_PIECE_RANGE] = QJsonArray {idx.first(), idx.last()};

View File

@ -44,20 +44,16 @@ window.qBittorrent.Filesystem = (function() {
}; };
}; };
const QB_EXT = '.!qB';
const PathSeparator = '/'; const PathSeparator = '/';
/** /**
* Returns the file extension part of a file name. * Returns the file extension part of a file name.
*/ */
const fileExtension = function(filename) { const fileExtension = function(filename) {
const name = filename.endsWith(QB_EXT) const pointIndex = filename.lastIndexOf('.');
? filename.substring(0, filename.length - QB_EXT.length)
: filename;
const pointIndex = name.lastIndexOf('.');
if (pointIndex === -1) if (pointIndex === -1)
return ''; return '';
return name.substring(pointIndex + 1); return filename.substring(pointIndex + 1);
}; };
const fileName = function(filepath) { const fileName = function(filepath) {