shared: SandboxedProcessor: Perform MIME-type detection

Detect mime types, and for text/*, run the default text processor.

The added benefit is that we can now add plaintext files without extensions,
or many other text files (e. g. source code).
Tento commit je obsažen v:
Albert S. 2022-05-30 19:51:13 +02:00
rodič 40207c3399
revize aed0ca31f7
2 změnil soubory, kde provedl 25 přidání a 5 odebrání

Zobrazit soubor

@ -22,7 +22,7 @@ static QMap<QString, Processor *> processors{
{"py", defaultTextProcessor}, {"xml", nothingProcessor}, {"html", tagStripperProcessor}, {"py", defaultTextProcessor}, {"xml", nothingProcessor}, {"html", tagStripperProcessor},
{"java", defaultTextProcessor}, {"js", defaultTextProcessor}, {"cpp", defaultTextProcessor}, {"java", defaultTextProcessor}, {"js", defaultTextProcessor}, {"cpp", defaultTextProcessor},
{"c", defaultTextProcessor}, {"sql", defaultTextProcessor}, {"odt", odtProcessor}, {"c", defaultTextProcessor}, {"sql", defaultTextProcessor}, {"odt", odtProcessor},
{"ods", odsProcessor}}; {"ods", odsProcessor}, {"svg", nothingProcessor}};
void SandboxedProcessor::enableSandbox(QString readablePath) void SandboxedProcessor::enableSandbox(QString readablePath)
{ {
@ -50,7 +50,7 @@ void SandboxedProcessor::enableSandbox(QString readablePath)
int ret = exile_enable_policy(policy); int ret = exile_enable_policy(policy);
if(ret != 0) if(ret != 0)
{ {
qDebug() << "Failed to establish sandbox: " << ret; qCritical() << "Failed to establish sandbox: " << ret;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
exile_free_policy(policy); exile_free_policy(policy);
@ -74,9 +74,27 @@ void SandboxedProcessor::printResults(const QVector<PageData> &pageData)
int SandboxedProcessor::process() int SandboxedProcessor::process()
{ {
QFileInfo fileInfo(this->filePath); QFileInfo fileInfo(this->filePath);
Processor *processor = processors.value(fileInfo.suffix(), nothingProcessor); Processor *processor = processors.value(fileInfo.suffix(), nullptr);
if(processor == nullptr)
if(processor == nothingProcessor) {
/* TODO: This is not sandboxed yet ... */
QMimeType mimeType = mimeDatabase.mimeTypeForFile(fileInfo);
if(mimeType.name().startsWith("text/"))
{
processor = defaultTextProcessor;
}
else
{
for(QString &str : mimeType.allAncestors())
{
if(str.startsWith("text/"))
{
processor = defaultTextProcessor;
}
}
}
}
if(processor == nullptr || processor == nothingProcessor)
{ {
/* Nothing to do */ /* Nothing to do */
return NOTHING_PROCESSED; return NOTHING_PROCESSED;

Zobrazit soubor

@ -1,12 +1,14 @@
#ifndef SANDBOXEDPROCESSOR_H #ifndef SANDBOXEDPROCESSOR_H
#define SANDBOXEDPROCESSOR_H #define SANDBOXEDPROCESSOR_H
#include <QString> #include <QString>
#include <QMimeDatabase>
#include "pagedata.h" #include "pagedata.h"
class SandboxedProcessor class SandboxedProcessor
{ {
private: private:
QString filePath; QString filePath;
QMimeDatabase mimeDatabase;
void enableSandbox(QString readablePath = ""); void enableSandbox(QString readablePath = "");
void printResults(const QVector<PageData> &pageData); void printResults(const QVector<PageData> &pageData);