diff --git a/examples/editModels.js b/examples/editModels.js index 31a55bf821..1a242d16d2 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -184,8 +184,9 @@ var httpMultiPart = (function () { // - name, string // - name, buffer var buffer, + string, stringBuffer, - string; + compressedBuffer; if (object.name === undefined) { @@ -217,9 +218,10 @@ var httpMultiPart = (function () { + "\r\n"; stringBuffer = string.toArrayBuffer(); - buffer = new Uint8Array(stringBuffer.byteLength + object.buffer.buffer.byteLength); + compressedBuffer = object.buffer.buffer.compress(); + buffer = new Uint8Array(stringBuffer.byteLength + compressedBuffer.byteLength); buffer.set(new Uint8Array(stringBuffer)); - buffer.set(new Uint8Array(object.buffer.buffer), stringBuffer.byteLength); + buffer.set(new Uint8Array(compressedBuffer), stringBuffer.byteLength); } else { diff --git a/libraries/script-engine/src/ArrayBufferPrototype.cpp b/libraries/script-engine/src/ArrayBufferPrototype.cpp index 53ebebc740..6f78caad2d 100644 --- a/libraries/script-engine/src/ArrayBufferPrototype.cpp +++ b/libraries/script-engine/src/ArrayBufferPrototype.cpp @@ -14,6 +14,9 @@ #include "ArrayBufferClass.h" #include "ArrayBufferPrototype.h" +static const int QCOMPRESS_HEADER_POSITION = 0; +static const int QCOMPRESS_HEADER_SIZE = 4; + Q_DECLARE_METATYPE(QByteArray*) ArrayBufferPrototype::ArrayBufferPrototype(QObject* parent) : QObject(parent) { @@ -43,6 +46,18 @@ QByteArray ArrayBufferPrototype::slice(qint32 begin) const { return ba->mid(begin, -1); } +QByteArray ArrayBufferPrototype::compress() const { + QByteArray* ba = thisArrayBuffer(); + + QByteArray buffer = qCompress(*ba); + + // Qt's qCompress() default compression level (-1) is the standard zLib compression. + // Here remove Qt's custom header that prevents the data server from uncompressing the files with zLib. + buffer.remove(QCOMPRESS_HEADER_POSITION, QCOMPRESS_HEADER_SIZE); + + return buffer; +} + QByteArray* ArrayBufferPrototype::thisArrayBuffer() const { return qscriptvalue_cast(thisObject().data()); } diff --git a/libraries/script-engine/src/ArrayBufferPrototype.h b/libraries/script-engine/src/ArrayBufferPrototype.h index 09d4596f28..2ad9843571 100644 --- a/libraries/script-engine/src/ArrayBufferPrototype.h +++ b/libraries/script-engine/src/ArrayBufferPrototype.h @@ -23,6 +23,7 @@ public: public slots: QByteArray slice(qint32 begin, qint32 end) const; QByteArray slice(qint32 begin) const; + QByteArray compress() const; private: QByteArray* thisArrayBuffer() const;