2019-04-16 08:50:08 +02:00
|
|
|
#include <QSqlError>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QtConcurrentMap>
|
2021-08-07 18:38:23 +02:00
|
|
|
#include <QProcess>
|
2019-04-16 08:50:08 +02:00
|
|
|
#include <functional>
|
|
|
|
#include "filesaver.h"
|
|
|
|
#include "processor.h"
|
|
|
|
#include "pdfprocessor.h"
|
|
|
|
#include "commandadd.h"
|
|
|
|
#include "defaulttextprocessor.h"
|
|
|
|
#include "tagstripperprocessor.h"
|
|
|
|
#include "nothingprocessor.h"
|
|
|
|
#include "odtprocessor.h"
|
|
|
|
#include "odsprocessor.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "logger.h"
|
|
|
|
|
|
|
|
FileSaver::FileSaver(SqliteDbService &dbService)
|
|
|
|
{
|
|
|
|
this->dbService = &dbService;
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveFileResult FileSaver::addFile(QString path)
|
|
|
|
{
|
|
|
|
QFileInfo info(path);
|
|
|
|
QString absPath = info.absoluteFilePath();
|
|
|
|
auto mtime = info.lastModified().toSecsSinceEpoch();
|
|
|
|
if(this->dbService->fileExistsInDatabase(absPath, mtime))
|
|
|
|
{
|
|
|
|
return SKIPPED;
|
|
|
|
}
|
|
|
|
return saveFile(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveFileResult FileSaver::updateFile(QString path)
|
|
|
|
{
|
|
|
|
QFileInfo info(path);
|
|
|
|
return saveFile(info);
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:46:33 +02:00
|
|
|
int FileSaver::addFiles(const QVector<QString> paths, bool keepGoing, bool verbose)
|
2019-04-16 08:50:08 +02:00
|
|
|
{
|
|
|
|
return processFiles(paths, std::bind(&FileSaver::addFile, this, std::placeholders::_1), keepGoing, verbose);
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:46:33 +02:00
|
|
|
int FileSaver::updateFiles(const QVector<QString> paths, bool keepGoing, bool verbose)
|
2019-04-16 08:50:08 +02:00
|
|
|
{
|
|
|
|
return processFiles(paths, std::bind(&FileSaver::updateFile, this, std::placeholders::_1), keepGoing, verbose);
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:46:33 +02:00
|
|
|
int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFileResult(QString path)> saverFunc,
|
|
|
|
bool keepGoing, bool verbose)
|
2019-04-16 08:50:08 +02:00
|
|
|
{
|
|
|
|
std::atomic<bool> terminate{false};
|
2019-04-30 23:44:27 +02:00
|
|
|
std::atomic<int> processedCount{0};
|
2019-04-16 08:50:08 +02:00
|
|
|
QtConcurrent::blockingMap(paths,
|
|
|
|
[&](const QString &path)
|
|
|
|
{
|
|
|
|
if(terminate.load())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(verbose)
|
|
|
|
{
|
2021-06-12 22:55:56 +02:00
|
|
|
Logger::info() << "Processing " << path << Qt::endl;
|
2019-04-16 08:50:08 +02:00
|
|
|
}
|
|
|
|
SaveFileResult result = saverFunc(path);
|
|
|
|
if(result == DBFAIL || result == PROCESSFAIL)
|
|
|
|
{
|
2021-06-12 22:55:56 +02:00
|
|
|
Logger::error() << "Failed to process " << path << Qt::endl;
|
2019-04-16 08:50:08 +02:00
|
|
|
if(!keepGoing)
|
|
|
|
{
|
|
|
|
terminate = true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 23:44:27 +02:00
|
|
|
else
|
2019-04-16 08:50:08 +02:00
|
|
|
{
|
2019-04-30 23:44:27 +02:00
|
|
|
++processedCount;
|
|
|
|
if(verbose)
|
2019-04-16 08:50:08 +02:00
|
|
|
{
|
2019-04-30 23:44:27 +02:00
|
|
|
if(result == SKIPPED)
|
|
|
|
{
|
|
|
|
Logger::info() << "Skipped" << path
|
2021-06-12 22:55:56 +02:00
|
|
|
<< "as it already exists in the database" << Qt::endl;
|
2019-04-30 23:44:27 +02:00
|
|
|
}
|
|
|
|
else if(result == OK)
|
|
|
|
{
|
2021-06-12 22:55:56 +02:00
|
|
|
Logger::info() << "Added" << path << Qt::endl;
|
2019-04-30 23:44:27 +02:00
|
|
|
}
|
2019-04-16 08:50:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-04-30 23:44:27 +02:00
|
|
|
return processedCount.load();
|
2019-04-16 08:50:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SaveFileResult FileSaver::saveFile(const QFileInfo &fileInfo)
|
|
|
|
{
|
|
|
|
QVector<PageData> pageData;
|
|
|
|
QString absPath = fileInfo.absoluteFilePath();
|
|
|
|
|
2021-08-07 18:38:23 +02:00
|
|
|
int status = -1;
|
2019-04-16 08:50:08 +02:00
|
|
|
if(fileInfo.isFile())
|
|
|
|
{
|
2021-08-07 18:38:23 +02:00
|
|
|
QProcess process;
|
|
|
|
QStringList args;
|
|
|
|
args << "process" << absPath;
|
|
|
|
process.setProcessChannelMode(QProcess::ForwardedErrorChannel);
|
|
|
|
process.start("/proc/self/exe", args);
|
|
|
|
process.waitForStarted();
|
|
|
|
process.waitForFinished();
|
|
|
|
|
|
|
|
/* TODO: This is suboptimal as it eats lots of mem
|
|
|
|
* but avoids a weird QDataStream/QProcess behaviour
|
|
|
|
* where it thinks the process has ended when it has not...
|
|
|
|
*
|
|
|
|
* Also, there seem to be issues with reads not being blocked, so
|
|
|
|
* the only reliable way appears to be waiting until the process
|
|
|
|
* finishes.
|
|
|
|
*/
|
|
|
|
QDataStream in(process.readAllStandardOutput());
|
|
|
|
while(!in.atEnd())
|
2019-04-16 08:50:08 +02:00
|
|
|
{
|
2021-08-07 18:38:23 +02:00
|
|
|
PageData pd;
|
|
|
|
in >> pd;
|
|
|
|
pageData.append(pd);
|
2019-04-16 08:50:08 +02:00
|
|
|
}
|
2021-08-07 18:38:23 +02:00
|
|
|
status = process.exitCode();
|
|
|
|
if(status != 0)
|
2019-04-16 08:50:08 +02:00
|
|
|
{
|
2021-08-07 18:38:23 +02:00
|
|
|
Logger::error() << "Error while processing" << absPath << ":"
|
|
|
|
<< "Exit code " << status << Qt::endl;
|
|
|
|
|
2019-04-16 08:50:08 +02:00
|
|
|
return PROCESSFAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Could happen if a file corrupted for example
|
2021-08-07 18:38:23 +02:00
|
|
|
if(pageData.isEmpty() && status != NOTHING_PROCESSED)
|
2019-04-16 08:50:08 +02:00
|
|
|
{
|
2021-06-12 22:55:56 +02:00
|
|
|
Logger::error() << "Could not get any content for " << absPath << Qt::endl;
|
2019-04-16 08:50:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this->dbService->saveFile(fileInfo, pageData);
|
|
|
|
}
|