Commit f976d3de authored by Xiaowu Zhang's avatar Xiaowu Zhang

fix blob storage exception in chrome browser

parent 99971bc2
......@@ -28,7 +28,7 @@
*/
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global define, module, require, indexedDB, jIO, RSVP, Blob */
/*global define, module, require, indexedDB, jIO, RSVP, Blob*/
(function (dependencies, factory) {
"use strict";
......@@ -134,19 +134,28 @@
*@param {Object} metadata The data to put in
*@return a new promise
*/
function putIndexedDB(store, metadata) {
function resolver(resolve, reject) {
var request = store.put(metadata);
function putIndexedDB(store, metadata, readData) {
var request,
resolver;
try {
request = store.put(metadata);
resolver = function (resolve, reject) {
request.onerror = reject;
request.onsuccess = function () {
resolve(metadata);
};
}
};
return new RSVP.Promise(resolver);
} catch (e) {
return putIndexedDB(store, {"_id": metadata._id,
"_attachment" : metadata._attachment,
"blob": readData});
}
}
/**
* get a data from a store object
* @param {ObjectStore} store The objectstore
......@@ -579,18 +588,20 @@
*
* @param {Object} command The JIO command
* @param {Object} metadata The data
*
*/
IndexedDBStorage.prototype.putAttachment = function (command, metadata) {
var jio_storage = this,
transaction,
global_db,
BlobInfo,
digest;
digest,
readResult;
new RSVP.Queue()
.push(jIO.util.readBlobAsBinaryString(metadata._blob).
.push(jIO.util.readBlobAsText(metadata._blob).
then(function (e) {
digest = jIO.util.makeBinaryStringDigest(e.target.result);
digest = jIO.util.makeBinaryStringDigest(e.target.result); //xxx
readResult = e.target.result;
BlobInfo = {
"content_type": metadata._blob.type,
"digest": digest,
......@@ -618,7 +629,7 @@
researchResult.result._attachment = researchResult.result._attachment
|| {};
researchResult.result._attachment[metadata._attachment] =
BlobInfo;
(BlobInfo === undefined) ? "BlobInfo" : BlobInfo;
return putIndexedDB(researchResult.store, researchResult.result);
})
.push(function () {
......@@ -626,10 +637,10 @@
var store = transaction.objectStore("blob");
return putIndexedDB(store, {"_id": metadata._id,
"_attachment" : metadata._attachment,
"blob": metadata._blob});
"blob": metadata._blob}, readResult);
})
.push(function () {
return ({"digest": digest});
return ({"digest": digest}); //xxx
})
.push(undefined, function (error) {
// Check if transaction is ongoing, if so, abort it
......@@ -644,6 +655,8 @@
.push(command.success, command.error, command.notify);
};
/**
* Retriev a document attachment
*
......@@ -675,6 +688,10 @@
})
.push(function (result) {
//get data
if (typeof result.blob === "string") {
result.blob = new Blob([result.blob],
{type: "text/plain"});
}
return ({ "data": result.blob});
})
.push(undefined, function (error) {
......@@ -690,6 +707,7 @@
.push(command.success, command.error, command.notify);
};
/**
* Remove an attachment
*
......
......@@ -28,12 +28,14 @@
}
test("Scenario", 32, function () {
// indexedDB.deleteDatabase("jio:test");
var server, shared = {}, jio = jIO.createJIO(
{"type" : "indexeddb",
"database" : "test"
},
{"workspace": {}}
);
stop();
server = {restore: function () {
return;
......
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