Commit f976d3de authored by Xiaowu Zhang's avatar Xiaowu Zhang

fix blob storage exception in chrome browser

parent 99971bc2
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
*/ */
/*jslint indent: 2, maxlen: 80, nomen: true */ /*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) { (function (dependencies, factory) {
"use strict"; "use strict";
...@@ -134,19 +134,28 @@ ...@@ -134,19 +134,28 @@
*@param {Object} metadata The data to put in *@param {Object} metadata The data to put in
*@return a new promise *@return a new promise
*/ */
function putIndexedDB(store, metadata) { function putIndexedDB(store, metadata, readData) {
function resolver(resolve, reject) { var request,
var request = store.put(metadata); resolver;
request.onerror = reject; try {
request.onsuccess = function () { request = store.put(metadata);
resolve(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});
} }
return new RSVP.Promise(resolver);
} }
/** /**
* get a data from a store object * get a data from a store object
* @param {ObjectStore} store The objectstore * @param {ObjectStore} store The objectstore
...@@ -579,18 +588,20 @@ ...@@ -579,18 +588,20 @@
* *
* @param {Object} command The JIO command * @param {Object} command The JIO command
* @param {Object} metadata The data * @param {Object} metadata The data
*
*/ */
IndexedDBStorage.prototype.putAttachment = function (command, metadata) { IndexedDBStorage.prototype.putAttachment = function (command, metadata) {
var jio_storage = this, var jio_storage = this,
transaction, transaction,
global_db, global_db,
BlobInfo, BlobInfo,
digest; digest,
readResult;
new RSVP.Queue() new RSVP.Queue()
.push(jIO.util.readBlobAsBinaryString(metadata._blob). .push(jIO.util.readBlobAsText(metadata._blob).
then(function (e) { then(function (e) {
digest = jIO.util.makeBinaryStringDigest(e.target.result); digest = jIO.util.makeBinaryStringDigest(e.target.result); //xxx
readResult = e.target.result;
BlobInfo = { BlobInfo = {
"content_type": metadata._blob.type, "content_type": metadata._blob.type,
"digest": digest, "digest": digest,
...@@ -598,40 +609,40 @@ ...@@ -598,40 +609,40 @@
}; };
}, function () { }, function () {
command.error("conflict", "broken blob", command.error("conflict", "broken blob",
"Cannot read data to put"); "Cannot read data to put");
})) }))
.push(function () { .push(function () {
return openIndexedDB(jio_storage._database_name); return openIndexedDB(jio_storage._database_name);
}) })
.push(function (db) { .push(function (db) {
global_db = db; global_db = db;
transaction = db.transaction(["attachment", transaction = db.transaction(["attachment",
"blob"], "readwrite"); "blob"], "readwrite");
return promiseResearch(transaction, metadata._id, "attachment", "_id"); return promiseResearch(transaction, metadata._id, "attachment", "_id");
}) })
.push(function (researchResult) { .push(function (researchResult) {
if (researchResult.result === undefined) { if (researchResult.result === undefined) {
throw ({"status": 404, "reason": "Not Found", throw ({"status": 404, "reason": "Not Found",
"message": "indexeddbStorage unable to put attachment"}); "message": "indexeddbStorage unable to put attachment"});
} }
//update attachment //update attachment
researchResult.result._attachment = researchResult.result._attachment researchResult.result._attachment = researchResult.result._attachment
|| {}; || {};
researchResult.result._attachment[metadata._attachment] = researchResult.result._attachment[metadata._attachment] =
BlobInfo; (BlobInfo === undefined) ? "BlobInfo" : BlobInfo;
return putIndexedDB(researchResult.store, researchResult.result); return putIndexedDB(researchResult.store, researchResult.result);
}) })
.push(function () { .push(function () {
//put in blob //put in blob
var store = transaction.objectStore("blob"); var store = transaction.objectStore("blob");
return putIndexedDB(store, {"_id": metadata._id, return putIndexedDB(store, {"_id": metadata._id,
"_attachment" : metadata._attachment, "_attachment" : metadata._attachment,
"blob": metadata._blob}); "blob": metadata._blob}, readResult);
}) })
.push(function () { .push(function () {
return ({"digest": digest}); return ({"digest": digest}); //xxx
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
// Check if transaction is ongoing, if so, abort it // Check if transaction is ongoing, if so, abort it
if (transaction !== undefined) { if (transaction !== undefined) {
transaction.abort(); transaction.abort();
...@@ -641,9 +652,11 @@ ...@@ -641,9 +652,11 @@
} }
throw error; throw error;
}) })
.push(command.success, command.error, command.notify); .push(command.success, command.error, command.notify);
}; };
/** /**
* Retriev a document attachment * Retriev a document attachment
* *
...@@ -656,28 +669,32 @@ ...@@ -656,28 +669,32 @@
global_db, global_db,
_id_attachment = [param._id, param._attachment]; _id_attachment = [param._id, param._attachment];
new RSVP.Queue() new RSVP.Queue()
.push(function () { .push(function () {
return openIndexedDB(jio_storage._database_name); return openIndexedDB(jio_storage._database_name);
}) })
.push(function (db) { .push(function (db) {
global_db = db; global_db = db;
transaction = db.transaction(["blob"], "readwrite"); transaction = db.transaction(["blob"], "readwrite");
//check if the attachment exists //check if the attachment exists
return promiseResearch(transaction, _id_attachment, return promiseResearch(transaction, _id_attachment,
"blob", "_id_attachment"); "blob", "_id_attachment");
}) })
.push(function (researchResult) { .push(function (researchResult) {
if (researchResult.result === undefined) { if (researchResult.result === undefined) {
throw ({"status": 404, "reason": "missing attachment", throw ({"status": 404, "reason": "missing attachment",
"message": "IndexeddbStorage, unable to get attachment."}); "message": "IndexeddbStorage, unable to get attachment."});
} }
return getIndexedDB(researchResult.store, _id_attachment); return getIndexedDB(researchResult.store, _id_attachment);
}) })
.push(function (result) { .push(function (result) {
//get data //get data
if (typeof result.blob === "string") {
result.blob = new Blob([result.blob],
{type: "text/plain"});
}
return ({ "data": result.blob}); return ({ "data": result.blob});
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
// Check if transaction is ongoing, if so, abort it // Check if transaction is ongoing, if so, abort it
if (transaction !== undefined) { if (transaction !== undefined) {
transaction.abort(); transaction.abort();
...@@ -687,9 +704,10 @@ ...@@ -687,9 +704,10 @@
} }
throw error; throw error;
}) })
.push(command.success, command.error, command.notify); .push(command.success, command.error, command.notify);
}; };
/** /**
* Remove an attachment * Remove an attachment
* *
...@@ -764,4 +782,4 @@ ...@@ -764,4 +782,4 @@
}; };
jIO.addStorage("indexeddb", IndexedDBStorage); jIO.addStorage("indexeddb", IndexedDBStorage);
})); }));
\ No newline at end of file
...@@ -28,12 +28,14 @@ ...@@ -28,12 +28,14 @@
} }
test("Scenario", 32, function () { test("Scenario", 32, function () {
// indexedDB.deleteDatabase("jio:test");
var server, shared = {}, jio = jIO.createJIO( var server, shared = {}, jio = jIO.createJIO(
{"type" : "indexeddb", {"type" : "indexeddb",
"database" : "test" "database" : "test"
}, },
{"workspace": {}} {"workspace": {}}
); );
stop(); stop();
server = {restore: function () { server = {restore: function () {
return; 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