Skip to content

Commit

Permalink
fix Qt 5.15 regression on JSON parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphaal committed Jun 6, 2020
1 parent ad57630 commit 8bbd0e0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 61 deletions.
41 changes: 33 additions & 8 deletions src/helpers/JSONSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@
#include "JSONSerializer.h"
#include "src/shared/models/RPZAtom.h"

QByteArray JSONSerializer::asBase64(const QPainterPath &path) {
QByteArray bArray;
QDataStream stream(&bArray, QIODevice::WriteOnly);
QString JSONSerializer::asBase64(QFile &file, int* fileContentSize, QString* fileHash) {
// read
file.open(QFile::ReadOnly);
auto raw = file.readAll();
if(fileContentSize) *fileContentSize = raw.count();
if(fileHash) *fileHash = _getFileHash(raw);
file.close();

return _asBase64(raw);
}

QString JSONSerializer::asBase64(const QPainterPath &path) {
QByteArray raw;
QDataStream stream(&raw, QIODevice::WriteOnly);
stream << path;
return bArray.toBase64();
return _asBase64(raw);
}

QString JSONSerializer::_asBase64(const QByteArray &raw) {
return QString::fromUtf8(raw.toBase64());
}

QPainterPath JSONSerializer::toPainterPath(const QByteArray &base64) {
Expand All @@ -39,10 +54,6 @@ QByteArray JSONSerializer::toBytes(const QByteArray &base64) {
return QByteArray::fromBase64(base64);
}

QByteArray JSONSerializer::asBase64(const QByteArray &raw) {
return raw.toBase64();
}

QVariant JSONSerializer::fromQSize(const QSize &size) {
return QVariantList { size.width(), size.height() };
}
Expand Down Expand Up @@ -88,3 +99,17 @@ QList<QPolygonF> JSONSerializer::toPolygons(const QVariantList &rawPolys) {
}
return out;
}

QString JSONSerializer::_getFileHash(const QByteArray &raw) {
auto hash = QCryptographicHash::hash(raw, QCryptographicHash::Keccak_224).toHex();
return QString::fromUtf8(hash);
}

QString JSONSerializer::getFileHash(QFile &file) {
// read
file.open(QFile::ReadOnly);
auto raw = file.readAll();
file.close();

return _getFileHash(raw);
}
11 changes: 9 additions & 2 deletions src/helpers/JSONSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
#include <QVariant>
#include <QVariantList>
#include <QSize>
#include <QFile>
#include <QPointF>

class JSONSerializer {
public:
static QByteArray asBase64(const QPainterPath &path);
static QByteArray asBase64(const QByteArray &raw);
// must cast back to UTF8 to allow correct parsing for Qt's JSON engine
static QString asBase64(const QPainterPath &path);
static QString asBase64(QFile &file, int* fileContentSize = nullptr, QString* fileHash = nullptr);

static QString getFileHash(QFile &file);

static QPainterPath toPainterPath(const QByteArray &base64);
static QByteArray toBytes(const QByteArray &base64);
Expand All @@ -42,4 +46,7 @@ class JSONSerializer {

static QVariant fromPolygons(const QList<QPolygonF> &polys);
static QList<QPolygonF> toPolygons(const QVariantList &rawPolys);
private:
static QString _asBase64(const QByteArray &raw);
static QString _getFileHash(const QByteArray &raw);
};
39 changes: 8 additions & 31 deletions src/shared/models/RPZSharedDocument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,53 +130,30 @@ class RPZSharedDocument : public QVariantHash {
// check if file exists
auto fullPath = localFileUrl.toLocalFile();
QFileInfo fi(fullPath);

if (!fi.exists()) {
qDebug() << qUtf8Printable(QStringLiteral("File Share : %1 does not exist anymore !").arg(localFileUrl.toString()));
return;
}

QByteArray base64Bytes;
RPZSharedDocument::FileHash hash;
{
// read file and extract raw bytes
QFile reader(fullPath);
reader.open(QFile::ReadOnly);
auto bytes = reader.readAll();
reader.close();

// encode
base64Bytes = JSONSerializer::asBase64(bytes);

// get hash from raw bytes
hash = RPZSharedDocument::_getFileHash(bytes);
}
// read file and extract raw bytes
QFile reader(fullPath);
QString fileHash;
int docS;
auto base64 = JSONSerializer::asBase64(reader, &docS, &fileHash);

// override file if exists
auto ext = fi.suffix();
auto name = fi.completeBaseName();

// insert in obj
this->insert(QStringLiteral(u"fileH"), hash);
this->insert(QStringLiteral(u"fileH"), fileHash);
this->insert(QStringLiteral(u"nm"), name);
this->insert(QStringLiteral(u"doc"), base64Bytes);
this->insert(QStringLiteral(u"docS"), base64Bytes.count());
this->insert(QStringLiteral(u"doc"), base64);
this->insert(QStringLiteral(u"docS"), docS);
this->insert(QStringLiteral(u"ext"), ext);

//
this->_localInstSuccess = true;
}

static RPZSharedDocument::FileHash _getFileHash(const QByteArray &fileBytes) {
// read signature...
auto hash = QString::fromUtf8(
QCryptographicHash::hash(
fileBytes,
QCryptographicHash::Keccak_224
).toHex()
);

return hash;
}
};
Q_DECLARE_METATYPE(RPZSharedDocument)
15 changes: 1 addition & 14 deletions src/shared/models/toy/RPZAsset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class RPZAsset : public QVariantHash {
auto name = fInfo.baseName();

// hash
auto hash = _getFileHash(fileReader);
auto hash = JSONSerializer::getFileHash(fileReader);

// move
auto expectedStoragePath = _getFilePathToAsset(hash, ext);
Expand All @@ -195,19 +195,6 @@ class RPZAsset : public QVariantHash {
return success;
}

static RPZAsset::Hash _getFileHash(QFile &fileReader) {
// read file
fileReader.open(QFile::ReadOnly);
auto bytes = fileReader.readAll();
fileReader.close();

// read signature...
auto hexHash = QCryptographicHash::hash(bytes, QCryptographicHash::Keccak_224).toHex();
RPZAsset::Hash hash = QString::fromUtf8(hexHash);

return hash;
}

static QString _getFilePathToAsset(const RPZAsset::Hash &id, const QString &ext) {
return QStringLiteral(u"%1/%2.%3")
.arg(AppContext::getAssetsFolderLocation())
Expand Down
7 changes: 1 addition & 6 deletions src/shared/models/toy/RPZAssetImportPackage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,10 @@ class RPZAssetImportPackage : public RPZAsset {
// check asset file existance
if (!assetFile.exists()) return;

// read
assetFile.open(QFile::ReadOnly);
auto asBase64 = assetFile.readAll().toBase64();
assetFile.close();

// add to content
this->insert(
QStringLiteral(u"_content"),
QString(asBase64)
JSONSerializer::asBase64(assetFile)
);

this->_isSuccessful = true;
Expand Down

0 comments on commit 8bbd0e0

Please sign in to comment.