Commit 750c352f authored by Romain Courteaud's avatar Romain Courteaud

[indexeddb] Fix getAttachment with huge blob

The array buffer length was higher than what is stored in indexedDB

Thanks to Alexandra Rogova for reporting the issue.
parent c948c24e
...@@ -438,8 +438,7 @@ ...@@ -438,8 +438,7 @@
var index = parseInt( var index = parseInt(
cursor.primaryKey.slice(key_path.length + 1), cursor.primaryKey.slice(key_path.length + 1),
10 10
), );
i;
if ((start !== 0) && (index < start_index)) { if ((start !== 0) && (index < start_index)) {
// No need to fetch blobs at the start // No need to fetch blobs at the start
...@@ -450,12 +449,6 @@ ...@@ -450,12 +449,6 @@
return; return;
} }
i = index - start_index;
// Extend array size
while (i > promise_list.length) {
promise_list.push(null);
i -= 1;
}
// Sort the blob by their index // Sort the blob by their index
promise_list.splice( promise_list.splice(
index - start_index, index - start_index,
...@@ -512,13 +505,8 @@ ...@@ -512,13 +505,8 @@
var index = parseInt( var index = parseInt(
cursor.primaryKey.slice(key_path.length + 1), cursor.primaryKey.slice(key_path.length + 1),
10 10
), );
i = index;
// Extend array size
while (i > array_buffer_list.length) {
array_buffer_list.push(null);
i -= 1;
}
// Sort the blob by their index // Sort the blob by their index
array_buffer_list.splice( array_buffer_list.splice(
index, index,
......
...@@ -19,9 +19,11 @@ ...@@ -19,9 +19,11 @@
*/ */
/*jslint nomen: true */ /*jslint nomen: true */
/*global indexedDB, Blob, sinon, IDBDatabase, /*global indexedDB, Blob, sinon, IDBDatabase,
IDBTransaction, IDBIndex, IDBObjectStore, IDBCursor, IDBKeyRange*/ IDBTransaction, IDBIndex, IDBObjectStore, IDBCursor, IDBKeyRange,
Rusha*/
(function (jIO, QUnit, indexedDB, Blob, sinon, IDBDatabase, (function (jIO, QUnit, indexedDB, Blob, sinon, IDBDatabase,
IDBTransaction, IDBIndex, IDBObjectStore, IDBCursor, IDBKeyRange) { IDBTransaction, IDBIndex, IDBObjectStore, IDBCursor, IDBKeyRange,
Rusha) {
"use strict"; "use strict";
var test = QUnit.test, var test = QUnit.test,
stop = QUnit.stop, stop = QUnit.stop,
...@@ -31,12 +33,9 @@ ...@@ -31,12 +33,9 @@
deepEqual = QUnit.deepEqual, deepEqual = QUnit.deepEqual,
equal = QUnit.equal, equal = QUnit.equal,
module = QUnit.module, module = QUnit.module,
big_string = "", big_string = "";
j;
for (j = 0; j < 3000000; j += 1) { big_string = new Array(3000000).fill('a').join('');
big_string += "a";
}
function deleteIndexedDB(storage) { function deleteIndexedDB(storage) {
return new RSVP.Promise(function resolver(resolve, reject) { return new RSVP.Promise(function resolver(resolve, reject) {
...@@ -1400,6 +1399,46 @@ ...@@ -1400,6 +1399,46 @@
}); });
}); });
test("get huge attachment", function () {
var context = this,
attachment = "attachment";
stop();
expect(4);
deleteIndexedDB(context.jio)
.then(function () {
return context.jio.put("foo", {"title": "bar"});
})
.then(function () {
return context.jio.putAttachment(
"foo",
attachment,
new Blob([new Array(11 * 2000000).fill('a').join('')],
{type: 'text/fooplain'})
);
})
.then(function () {
return context.jio.getAttachment("foo", attachment);
})
.then(function (blob) {
ok(blob instanceof Blob, "Data is Blob");
equal(blob.type, 'text/fooplain');
equal(blob.size, 22000000);
return jIO.util.readBlobAsArrayBuffer(blob);
})
.then(function (result) {
equal((new Rusha()).digestFromArrayBuffer(result.target.result),
'6f510194afd8e436d00a543f49a7df09e86c2687');
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("retrieve empty blob", function () { test("retrieve empty blob", function () {
var context = this, var context = this,
attachment = "attachment", attachment = "attachment",
...@@ -1724,4 +1763,5 @@ ...@@ -1724,4 +1763,5 @@
}); });
}(jIO, QUnit, indexedDB, Blob, sinon, IDBDatabase, }(jIO, QUnit, indexedDB, Blob, sinon, IDBDatabase,
IDBTransaction, IDBIndex, IDBObjectStore, IDBCursor, IDBKeyRange)); IDBTransaction, IDBIndex, IDBObjectStore, IDBCursor, IDBKeyRange,
Rusha));
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