Commit ccec566e authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add some minimal verification after saving

parent d0c5d769
......@@ -21,6 +21,7 @@ Boston, MA 02110-1301, USA.
#include "KisImportExportFilter.h"
#include <QFile>
#include <QFileInfo>
#include <kis_debug.h>
#include <QStack>
#include "KisImportExportManager.h"
......@@ -207,6 +208,40 @@ QMap<QString, KisExportCheckBase *> KisImportExportFilter::exportChecks()
return d->capabilities;
}
QString KisImportExportFilter::verify(const QString &fileName) const
{
QFileInfo fi(fileName);
if (!fi.exists()) {
return i18n("%1 does not exit after writing. Try saving again under a different name, in another location.", fileName);
}
if (!fi.isReadable()) {
return i18n("%1 is not readable", fileName);
}
if (fi.size() < 10) {
return i18n("%1 is smaller than 10 bytes, it must be corrupt. Try saving again under a different name, in another location.", fileName);
}
QFile f(fileName);
f.open(QFile::ReadOnly);
QByteArray ba = f.read(std::min(f.size(), (qint64)1000));
bool found = false;
for(int i = 0; i < ba.size(); ++i) {
if (ba.at(i) > 0) {
found = true;
break;
}
}
if (!found) {
return i18n("%1 has only zero bytes in the first 1000 bytes, it's probably corrupt. Try saving again under a different name, in another location.", fileName);
}
return QString();
}
void KisImportExportFilter::setUpdater(QPointer<KoUpdater> updater)
{
d->updater = updater;
......
......@@ -156,6 +156,9 @@ public:
/// Override and return false for the filters that use a library that cannot handle file handles, only file names.
virtual bool supportsIO() const { return true; }
/// Verify whether the given file is correct and readable
virtual QString verify(const QString &fileName) const;
protected:
/**
* This is the constructor your filter has to call, obviously.
......
......@@ -680,6 +680,15 @@ KisImportExportErrorCode KisImportExportManager::doExportImpl(const QString &loc
#endif
}
}
// Do some minimal verification
QString verificationResult = filter->verify(location);
if (!verificationResult.isEmpty()) {
status = KisImportExportErrorCode(ImportExportCodes::ErrorWhileWriting);
m_document->setErrorMessage(verificationResult);
}
return status;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment