From 26c7cdbc5f2b47fb028627634e757a7be5b67def Mon Sep 17 00:00:00 2001 From: Albert S Date: Mon, 30 May 2022 20:03:40 +0200 Subject: [PATCH] shared: Move textfile detector to common --- shared/common.cpp | 23 +++++++++++++++++++++++ shared/common.h | 3 ++- shared/sandboxedprocessor.cpp | 16 +++------------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/shared/common.cpp b/shared/common.cpp index f929ef0..095cb25 100644 --- a/shared/common.cpp +++ b/shared/common.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "looqsgeneralexception.h" #include "common.h" #include "dbmigrator.h" @@ -178,3 +179,25 @@ QStringList Common::excludedPaths() } return ::excludedPaths; } + +bool Common::isTextFile(QFileInfo fileInfo) +{ + /* TODO: This is not sandboxed yet ... */ + QMimeDatabase mimeDatabase; + QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileInfo); + if(mimeType.name().startsWith("text/")) + { + return true; + } + else + { + for(QString &str : mimeType.allAncestors()) + { + if(str.startsWith("text/")) + { + return true; + } + } + } + return false; +} diff --git a/shared/common.h b/shared/common.h index ab0457a..28b1abd 100644 --- a/shared/common.h +++ b/shared/common.h @@ -1,7 +1,7 @@ #ifndef COMMON_H #define COMMON_H #include - +#include namespace Common { void setupAppInfo(); @@ -12,5 +12,6 @@ QString findInPath(QString needle); bool initSqliteDatabase(QString path); void ensureConfigured(); QStringList excludedPaths(); +bool isTextFile(QFileInfo fileInfo); } // namespace Common #endif diff --git a/shared/sandboxedprocessor.cpp b/shared/sandboxedprocessor.cpp index 3e08754..e3168bc 100644 --- a/shared/sandboxedprocessor.cpp +++ b/shared/sandboxedprocessor.cpp @@ -9,6 +9,7 @@ #include "odtprocessor.h" #include "odsprocessor.h" #include "../submodules/exile.h/exile.h" +#include "common.h" #include "logger.h" static DefaultTextProcessor *defaultTextProcessor = new DefaultTextProcessor(); @@ -77,22 +78,11 @@ int SandboxedProcessor::process() Processor *processor = processors.value(fileInfo.suffix(), nullptr); if(processor == nullptr) { - /* TODO: This is not sandboxed yet ... */ - QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileInfo); - if(mimeType.name().startsWith("text/")) + /* TODO: Not sandboxed */ + if(Common::isTextFile(fileInfo)) { processor = defaultTextProcessor; } - else - { - for(QString &str : mimeType.allAncestors()) - { - if(str.startsWith("text/")) - { - processor = defaultTextProcessor; - } - } - } } if(processor == nullptr || processor == nothingProcessor) {