Made sure the logs are ignored when collecting files

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-06-30 10:52:10 +03:00
parent 87efa700ab
commit 81207c6502
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
4 changed files with 26 additions and 11 deletions

View File

@ -262,9 +262,19 @@ bool FileIgnoreProxy::filterAcceptsRow(int sourceRow, const QModelIndex& sourceP
QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel()); QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel());
auto fileInfo = fsm->fileInfo(index); auto fileInfo = fsm->fileInfo(index);
return !ignoreFile(fileInfo);
}
bool FileIgnoreProxy::ignoreFile(QFileInfo fileInfo) const
{
auto fileName = fileInfo.fileName(); auto fileName = fileInfo.fileName();
auto path = relPath(fileInfo.absoluteFilePath()); auto path = relPath(fileInfo.absoluteFilePath());
return !(path.startsWith("..") || // just in case ignore files outside the gameroot return (path.startsWith("..") || // just in case ignore files outside the gameroot
std::any_of(m_ignoreFiles.cbegin(), m_ignoreFiles.cend(), [fileName](auto iFileName) { return fileName == iFileName; }) || std::any_of(m_ignoreFiles.cbegin(), m_ignoreFiles.cend(), [fileName](auto iFileName) { return fileName == iFileName; }) ||
std::any_of(m_ignoreFilePaths.cbegin(), m_ignoreFilePaths.cend(), [path](auto iPath) { return path == iPath; })); m_ignoreFilePaths.covers(path));
}
bool FileIgnoreProxy::filterFile(const QString& fileName) const
{
return blocked.covers(fileName) || ignoreFile(QFileInfo(QDir(root), fileName));
} }

View File

@ -36,6 +36,7 @@
#pragma once #pragma once
#include <QFileInfo>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "SeparatorPrefixTree.h" #include "SeparatorPrefixTree.h"
@ -66,15 +67,19 @@ class FileIgnoreProxy : public QSortFilterProxyModel {
// list of file names that need to be removed completely from model // list of file names that need to be removed completely from model
inline QStringList& ignoreFilesWithName() { return m_ignoreFiles; } inline QStringList& ignoreFilesWithName() { return m_ignoreFiles; }
// list of relative paths that need to be removed completely from model // list of relative paths that need to be removed completely from model
inline QStringList& ignoreFilesWithPath() { return m_ignoreFilePaths; } inline SeparatorPrefixTree<'/'>& ignoreFilesWithPath() { return m_ignoreFilePaths; }
bool filterFile(const QString& fileName) const;
protected: protected:
bool filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const; bool filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const;
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const; bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
bool ignoreFile(QFileInfo file) const;
private: private:
const QString root; const QString root;
SeparatorPrefixTree<'/'> blocked; SeparatorPrefixTree<'/'> blocked;
QStringList m_ignoreFiles; QStringList m_ignoreFiles;
QStringList m_ignoreFilePaths; SeparatorPrefixTree<'/'> m_ignoreFilePaths;
}; };

View File

@ -40,6 +40,7 @@
#include <QFileDialog> #include <QFileDialog>
#include <QFileSystemModel> #include <QFileSystemModel>
#include <QMessageBox> #include <QMessageBox>
#include "FileIgnoreProxy.h"
#include "ui_ExportInstanceDialog.h" #include "ui_ExportInstanceDialog.h"
#include <FileSystem.h> #include <FileSystem.h>
@ -49,6 +50,7 @@
#include <QSaveFile> #include <QSaveFile>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include <QStack> #include <QStack>
#include <functional>
#include "Application.h" #include "Application.h"
#include "SeparatorPrefixTree.h" #include "SeparatorPrefixTree.h"
@ -62,7 +64,7 @@ ExportInstanceDialog::ExportInstanceDialog(InstancePtr instance, QWidget* parent
proxyModel = new FileIgnoreProxy(root, this); proxyModel = new FileIgnoreProxy(root, this);
proxyModel->setSourceModel(model); proxyModel->setSourceModel(model);
auto prefix = QDir(instance->instanceRoot()).relativeFilePath(instance->gameRoot()); auto prefix = QDir(instance->instanceRoot()).relativeFilePath(instance->gameRoot());
proxyModel->ignoreFilesWithPath().append({ FS::PathCombine(prefix, "logs"), FS::PathCombine(prefix, "crash-reports") }); proxyModel->ignoreFilesWithPath().insert({ FS::PathCombine(prefix, "logs"), FS::PathCombine(prefix, "crash-reports") });
proxyModel->ignoreFilesWithName().append({ ".DS_Store", "thumbs.db", "Thumbs.db" }); proxyModel->ignoreFilesWithName().append({ ".DS_Store", "thumbs.db", "Thumbs.db" });
loadPackIgnore(); loadPackIgnore();
@ -137,11 +139,9 @@ bool ExportInstanceDialog::doExport()
SaveIcon(m_instance); SaveIcon(m_instance);
auto & blocked = proxyModel->blockedPaths();
using std::placeholders::_1;
auto files = QFileInfoList(); auto files = QFileInfoList();
if (!MMCZip::collectFileListRecursively(m_instance->instanceRoot(), nullptr, &files, if (!MMCZip::collectFileListRecursively(m_instance->instanceRoot(), nullptr, &files,
std::bind(&SeparatorPrefixTree<'/'>::covers, blocked, _1))) { std::bind(&FileIgnoreProxy::filterFile, proxyModel, std::placeholders::_1))) {
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance")); QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
return false; return false;
} }

View File

@ -52,7 +52,7 @@ ExportMrPackDialog::ExportMrPackDialog(InstancePtr instance, QWidget* parent)
// use the game root - everything outside cannot be exported // use the game root - everything outside cannot be exported
const QDir root(instance->gameRoot()); const QDir root(instance->gameRoot());
proxy = new FileIgnoreProxy(instance->gameRoot(), this); proxy = new FileIgnoreProxy(instance->gameRoot(), this);
proxy->ignoreFilesWithPath().append({ "logs", "crash-reports" }); proxy->ignoreFilesWithPath().insert({ "logs", "crash-reports" });
proxy->ignoreFilesWithName().append({ ".DS_Store", "thumbs.db", "Thumbs.db" }); proxy->ignoreFilesWithName().append({ ".DS_Store", "thumbs.db", "Thumbs.db" });
proxy->setSourceModel(model); proxy->setSourceModel(model);
@ -100,7 +100,7 @@ void ExportMrPackDialog::done(int result)
return; return;
ModrinthPackExportTask task(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output, ModrinthPackExportTask task(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output,
[this](const QString& path) { return proxy->blockedPaths().covers(path); }); std::bind(&FileIgnoreProxy::filterFile, proxy, std::placeholders::_1));
connect(&task, &Task::failed, connect(&task, &Task::failed,
[this](const QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); }); [this](const QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); });