diff options
| author | Chris Bessent <lost.dragonist@gmail.com> | 2021-04-29 00:42:51 -0700 |
|---|---|---|
| committer | Chris Bessent <lost.dragonist@gmail.com> | 2021-04-29 00:50:11 -0700 |
| commit | 9638a797d16eb2d9b504a4a337a20b4eee535e4c (patch) | |
| tree | 51a41f8c54d57a41819ef431242d6d2509af065d | |
| parent | 40a4bf1e3a25d98da481c6807e327f036a9a33e6 (diff) | |
Sanitize download file names
Also switch instance name sanitizer to MOBase version
| -rw-r--r-- | src/createinstancedialogpages.cpp | 7 | ||||
| -rw-r--r-- | src/downloadmanager.cpp | 5 | ||||
| -rw-r--r-- | src/instancemanager.cpp | 32 | ||||
| -rw-r--r-- | src/instancemanager.h | 10 | ||||
| -rw-r--r-- | src/instancemanagerdialog.cpp | 7 |
5 files changed, 13 insertions, 48 deletions
diff --git a/src/createinstancedialogpages.cpp b/src/createinstancedialogpages.cpp index c337ba86..09026fa4 100644 --- a/src/createinstancedialogpages.cpp +++ b/src/createinstancedialogpages.cpp @@ -8,6 +8,7 @@ #include <iplugingame.h> #include <report.h> #include <utility.h> +#include "filesystemutilities.h" namespace cid { @@ -856,7 +857,7 @@ QString NamePage::selectedInstanceName() const } const auto text = ui->instanceName->text().trimmed(); - return InstanceManager::singleton().sanitizeInstanceName(text); + return MOBase::sanitizeFileName(text); } void NamePage::onChanged() @@ -883,7 +884,7 @@ bool NamePage::checkName(QString parentDir, QString name) if (name.isEmpty()) { empty = true; } else { - if (InstanceManager::singleton().validInstanceName(name)) { + if (MOBase::validFileName(name)) { exists = QDir(parentDir).exists(name); } else { invalid = true; @@ -1108,7 +1109,7 @@ bool PathsPage::checkPath( } else { const QDir d(path); - if (m.validInstanceName(d.dirName())) { + if (MOBase::validFileName(d.dirName())) { if (m_dlg.rawCreationInfo().type == CreateInstanceDialog::Portable) { // the default data path for a portable instance is the application // directory, so it's not an error if it exists diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index 676b43de..4c8e820f 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -33,6 +33,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include "shared/util.h" #include <utility.h> #include <report.h> +#include "filesystemutilities.h" #include <QTimer> #include <QFileInfo> @@ -1456,7 +1457,7 @@ void DownloadManager::markUninstalled(QString fileName) QString DownloadManager::getDownloadFileName(const QString &baseName, bool rename) const { - QString fullPath = m_OutputDirectory + "/" + baseName; + QString fullPath = m_OutputDirectory + "/" + MOBase::sanitizeFileName(baseName); if (QFile::exists(fullPath) && rename) { int i = 1; while (QFile::exists(QString("%1/%2_%3").arg(m_OutputDirectory).arg(i).arg(baseName))) { @@ -1476,7 +1477,7 @@ QString DownloadManager::getFileNameFromNetworkReply(QNetworkReply *reply) std::cmatch result; if (std::regex_search(reply->rawHeader("Content-Disposition").constData(), result, exp)) { - return QString::fromUtf8(result.str(1).c_str()); + return MOBase::sanitizeFileName(QString::fromUtf8(result.str(1).c_str())); } } diff --git a/src/instancemanager.cpp b/src/instancemanager.cpp index a1b17577..e7474584 100644 --- a/src/instancemanager.cpp +++ b/src/instancemanager.cpp @@ -32,6 +32,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include <iplugingame.h> #include <utility.h> #include <log.h> +#include "filesystemutilities.h" #include <QCoreApplication> #include <QDir> @@ -721,7 +722,7 @@ const MOBase::IPluginGame* InstanceManager::gamePluginForDirectory( QString InstanceManager::makeUniqueName(const QString& instanceName) const { - const QString sanitized = sanitizeInstanceName(instanceName); + const QString sanitized = MOBase::sanitizeFileName(instanceName); // trying "name (N)" QString name = sanitized; @@ -742,35 +743,6 @@ bool InstanceManager::instanceExists(const QString& instanceName) const return root.exists(instanceName); } -QString InstanceManager::sanitizeInstanceName(const QString &name) const -{ - QString new_name = name; - - // Restrict the allowed characters - new_name = new_name.remove(QRegExp("[^A-Za-z0-9 _=+;!@#$%^'\\-\\.\\[\\]\\{\\}\\(\\)]")); - - // Don't end in spaces and periods - new_name = new_name.remove(QRegExp("\\.*$")); - new_name = new_name.remove(QRegExp(" *$")); - - // Recurse until stuff stops changing - if (new_name != name) { - return sanitizeInstanceName(new_name); - } - return new_name; -} - -bool InstanceManager::validInstanceName(const QString& instanceName) const -{ - if (instanceName.isEmpty()) { - return false; - } - - return (instanceName == sanitizeInstanceName(instanceName)); -} - - - std::unique_ptr<Instance> selectInstance() { auto& m = InstanceManager::singleton(); diff --git a/src/instancemanager.h b/src/instancemanager.h index f1781a70..7174ec3a 100644 --- a/src/instancemanager.h +++ b/src/instancemanager.h @@ -305,10 +305,6 @@ public: // std::vector<QString> globalInstancePaths() const; - // returns `name` modified so that it is a valid instance name - // - QString sanitizeInstanceName(const QString &name) const; - // sanitizes the given instance name and either // 1) returns it if there is no instance with this name // 2) tries to add " (N)" at the end until it works @@ -321,12 +317,6 @@ public: // bool instanceExists(const QString& instanceName) const; - // returns whether the given instance name would be a valid name; this does - // not check whether the instance already exists, it's basiscally just a check - // against what sanitizeInstanceName() returns - // - bool validInstanceName(const QString& instanceName) const; - // returns the absolute path of a global instance with the given name; this // does not check if the name is valid or if exists // diff --git a/src/instancemanagerdialog.cpp b/src/instancemanagerdialog.cpp index 1fd43128..f21ff695 100644 --- a/src/instancemanagerdialog.cpp +++ b/src/instancemanagerdialog.cpp @@ -10,6 +10,7 @@ #include <utility.h> #include <report.h> #include <iplugingame.h> +#include "filesystemutilities.h" using namespace MOBase; @@ -109,10 +110,10 @@ QString getInstanceName( if (text->text().isEmpty()) { error->setText(""); - } else if (!m.validInstanceName(text->text())) { + } else if (!MOBase::validFileName(text->text())) { error->setText(QObject::tr("The instance name must be a valid folder name.")); } else { - const auto name = m.sanitizeInstanceName(text->text()); + const auto name = MOBase::sanitizeFileName(text->text()); if ((name != oldName) && m.instanceExists(text->text())) { error->setText(QObject::tr("An instance with this name already exists.")); @@ -136,7 +137,7 @@ QString getInstanceName( return {}; } - return m.sanitizeInstanceName(text->text()); + return MOBase::sanitizeFileName(text->text()); } |
