diff --git a/src/jio.js b/src/jio.js index 0b2076e80258143e26dc044d0cdc71aacd9add56..f04e0167f7664477db69870ab3d0e4be8acce607 100644 --- a/src/jio.js +++ b/src/jio.js @@ -197,7 +197,8 @@ function declareMethod(klass, name, precondition_function, post_function) { klass.prototype[name] = function () { var argument_list = arguments, - context = this; + context = this, + precondition_result; return new RSVP.Queue() .push(function () { @@ -208,8 +209,9 @@ ); } }) - .push(function () { + .push(function (result) { var storage_method = context.__storage[name]; + precondition_result = result; if (storage_method === undefined) { throw new jIO.util.jIOError( "Capacity '" + name + "' is not implemented on '" + @@ -227,7 +229,8 @@ return post_function.call( context, argument_list, - result + result, + precondition_result ); } return result; @@ -306,6 +309,7 @@ declareMethod(JioProxyStorage, 'getAttachment', function (argument_list, storage, method_name) { + var result = "blob"; // if (param.storage_spec.type !== "indexeddb" && // param.storage_spec.type !== "dav" && // (param.kwargs._start !== undefined @@ -319,8 +323,15 @@ // } checkId(argument_list, storage, method_name); checkAttachmentId(argument_list, storage, method_name); - }, function (argument_list, result) { - if (!(result instanceof Blob)) { + // Drop optional parameters, which are only used in postfunction + if (argument_list[2] !== undefined) { + result = argument_list[2].format || result; + delete argument_list[2].format; + } + return result; + }, function (argument_list, blob, convert) { + var result; + if (!(blob instanceof Blob)) { throw new jIO.util.jIOError( "'getAttachment' (" + argument_list[0] + " , " + argument_list[1] + ") on '" + this.__type + @@ -328,6 +339,47 @@ 501 ); } + if (convert === "blob") { + result = blob; + } else if (convert === "data_url") { + result = new RSVP.Queue() + .push(function () { + return jIO.util.readBlobAsDataURL(blob); + }) + .push(function (evt) { + return evt.target.result; + }); + } else if (convert === "array_buffer") { + result = new RSVP.Queue() + .push(function () { + return jIO.util.readBlobAsArrayBuffer(blob); + }) + .push(function (evt) { + return evt.target.result; + }); + } else if (convert === "text") { + result = new RSVP.Queue() + .push(function () { + return jIO.util.readBlobAsText(blob); + }) + .push(function (evt) { + return evt.target.result; + }); + } else if (convert === "json") { + result = new RSVP.Queue() + .push(function () { + return jIO.util.readBlobAsText(blob); + }) + .push(function (evt) { + return JSON.parse(evt.target.result); + }); + } else { + throw new jIO.util.jIOError( + this.__type + ".getAttachment format: '" + convert + + "' is not supported", + 400 + ); + } return result; }); diff --git a/src/jio.storage/documentstorage.js b/src/jio.storage/documentstorage.js index a284c78c77a06a77fcd6297c0180ad083fa6523a..0efd37616ccb75f60aeb0e15ec6acbb6c7b0e573 100644 --- a/src/jio.storage/documentstorage.js +++ b/src/jio.storage/documentstorage.js @@ -29,14 +29,9 @@ DocumentStorage.prototype.get = function (id) { return this._sub_storage.getAttachment( this._document_id, - getSubAttachmentIdFromParam(id) - ) - .push(function (blob) { - return jIO.util.readBlobAsText(blob); - }) - .push(function (text) { - return JSON.parse(text.target.result); - }); + getSubAttachmentIdFromParam(id), + {format: "json"} + ); }; DocumentStorage.prototype.allAttachments = function (id) { diff --git a/src/jio.storage/drivetojiomapping.js b/src/jio.storage/drivetojiomapping.js index 93800461a31e7c47565c382ccab9e08553f43266..ba2392e5f7687089341741ca80788c7dbf515b27 100644 --- a/src/jio.storage/drivetojiomapping.js +++ b/src/jio.storage/drivetojiomapping.js @@ -28,18 +28,11 @@ // First get the document itself if it exists return context._sub_storage.getAttachment( DOCUMENT_KEY, - id + DOCUMENT_EXTENSION + id + DOCUMENT_EXTENSION, + {format: "json"} ); }) - .push(function (blob) { - return new RSVP.Queue() - .push(function () { - return jIO.util.readBlobAsText(blob); - }) - .push(function (text) { - return JSON.parse(text.target.result); - }); - }, function (error) { + .push(undefined, function (error) { if ((error instanceof jIO.util.jIOError) && (error.status_code === 404)) {