Commit 3fb5fcf5 authored by Romain Courteaud's avatar Romain Courteaud

Allow to return another format than Blob in getAttachment

Use a new optional parameter: jIO.getAttachment(id, name, {format: blob})

Supported formats are: blob, text, json, array_buffer, data_url
parent 7a527b42
...@@ -197,7 +197,8 @@ ...@@ -197,7 +197,8 @@
function declareMethod(klass, name, precondition_function, post_function) { function declareMethod(klass, name, precondition_function, post_function) {
klass.prototype[name] = function () { klass.prototype[name] = function () {
var argument_list = arguments, var argument_list = arguments,
context = this; context = this,
precondition_result;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -208,8 +209,9 @@ ...@@ -208,8 +209,9 @@
); );
} }
}) })
.push(function () { .push(function (result) {
var storage_method = context.__storage[name]; var storage_method = context.__storage[name];
precondition_result = result;
if (storage_method === undefined) { if (storage_method === undefined) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"Capacity '" + name + "' is not implemented on '" + "Capacity '" + name + "' is not implemented on '" +
...@@ -227,7 +229,8 @@ ...@@ -227,7 +229,8 @@
return post_function.call( return post_function.call(
context, context,
argument_list, argument_list,
result result,
precondition_result
); );
} }
return result; return result;
...@@ -306,6 +309,7 @@ ...@@ -306,6 +309,7 @@
declareMethod(JioProxyStorage, 'getAttachment', function (argument_list, declareMethod(JioProxyStorage, 'getAttachment', function (argument_list,
storage, storage,
method_name) { method_name) {
var result = "blob";
// if (param.storage_spec.type !== "indexeddb" && // if (param.storage_spec.type !== "indexeddb" &&
// param.storage_spec.type !== "dav" && // param.storage_spec.type !== "dav" &&
// (param.kwargs._start !== undefined // (param.kwargs._start !== undefined
...@@ -319,8 +323,15 @@ ...@@ -319,8 +323,15 @@
// } // }
checkId(argument_list, storage, method_name); checkId(argument_list, storage, method_name);
checkAttachmentId(argument_list, storage, method_name); checkAttachmentId(argument_list, storage, method_name);
}, function (argument_list, result) { // Drop optional parameters, which are only used in postfunction
if (!(result instanceof Blob)) { 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( throw new jIO.util.jIOError(
"'getAttachment' (" + argument_list[0] + " , " + "'getAttachment' (" + argument_list[0] + " , " +
argument_list[1] + ") on '" + this.__type + argument_list[1] + ") on '" + this.__type +
...@@ -328,6 +339,47 @@ ...@@ -328,6 +339,47 @@
501 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; return result;
}); });
......
...@@ -29,14 +29,9 @@ ...@@ -29,14 +29,9 @@
DocumentStorage.prototype.get = function (id) { DocumentStorage.prototype.get = function (id) {
return this._sub_storage.getAttachment( return this._sub_storage.getAttachment(
this._document_id, this._document_id,
getSubAttachmentIdFromParam(id) getSubAttachmentIdFromParam(id),
) {format: "json"}
.push(function (blob) { );
return jIO.util.readBlobAsText(blob);
})
.push(function (text) {
return JSON.parse(text.target.result);
});
}; };
DocumentStorage.prototype.allAttachments = function (id) { DocumentStorage.prototype.allAttachments = function (id) {
......
...@@ -28,18 +28,11 @@ ...@@ -28,18 +28,11 @@
// First get the document itself if it exists // First get the document itself if it exists
return context._sub_storage.getAttachment( return context._sub_storage.getAttachment(
DOCUMENT_KEY, DOCUMENT_KEY,
id + DOCUMENT_EXTENSION id + DOCUMENT_EXTENSION,
{format: "json"}
); );
}) })
.push(function (blob) { .push(undefined, function (error) {
return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsText(blob);
})
.push(function (text) {
return JSON.parse(text.target.result);
});
}, function (error) {
if ((error instanceof jIO.util.jIOError) && if ((error instanceof jIO.util.jIOError) &&
(error.status_code === 404)) { (error.status_code === 404)) {
......
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