Skip to content

Commit

Permalink
Avoid reading data files before saving
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Apr 21, 2024
1 parent bda32ca commit fdcea22
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/item/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <QCryptographicHash>
#include <QDataStream>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QIODevice>
#include <QList>
#include <QPair>
Expand All @@ -31,6 +33,11 @@ class DataFile {
const QString &path() const { return m_path; }
void setPath(const QString &path) { m_path = path; }

qint64 size() const
{
return QFileInfo(m_path).size();
}

QByteArray readAll() const
{
QFile f(m_path);
Expand Down Expand Up @@ -223,11 +230,20 @@ void serializeData(QDataStream *stream, const QVariantMap &data, int itemDataThr
const qint32 size = data.size();
*stream << size;

QByteArray bytes;
for (auto it = data.constBegin(); it != data.constEnd(); ++it) {
const auto &mime = it.key();
bytes = data[mime].toByteArray();
if ( (itemDataThreshold >= 0 && bytes.length() > itemDataThreshold) || mime.startsWith(mimeFilePrefix) ) {
const QString &mime = it.key();
const QVariant &value = it.value();

const DataFile dataFile = value.value<DataFile>();
const int dataLength = dataFile.path().isEmpty()
? value.toByteArray().size() : dataFile.size();

if ( (itemDataThreshold >= 0 && dataLength > itemDataThreshold) || mime.startsWith(mimeFilePrefix) ) {
// Already saved
if ( !dataFile.path().isEmpty() )
continue;

const QByteArray bytes = value.toByteArray();
const QString path = dataFilePath(bytes, true);
if ( path.isEmpty() ) {
stream->setStatus(QDataStream::WriteFailed);
Expand Down Expand Up @@ -255,6 +271,7 @@ void serializeData(QDataStream *stream, const QVariantMap &data, int itemDataThr
*stream << /* compressData = */ false
<< path.toUtf8();
} else {
const QByteArray bytes = value.toByteArray();
*stream << compressMime(mime)
<< /* compressData = */ false
<< bytes;
Expand Down

0 comments on commit fdcea22

Please sign in to comment.