shared: Move textfile detector to common

This commit is contained in:
Albert S. 2022-05-30 20:03:40 +02:00
γονέας aed0ca31f7
υποβολή 26c7cdbc5f
3 αρχεία άλλαξαν με 28 προσθήκες και 14 διαγραφές

@ -7,6 +7,7 @@
#include <QSqlError>
#include <QTextStream>
#include <QDebug>
#include <QMimeDatabase>
#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;
}

@ -1,7 +1,7 @@
#ifndef COMMON_H
#define COMMON_H
#include <QCoreApplication>
#include <QFileInfo>
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

@ -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)
{