Commit 903ed2df authored by Vincent Bechu's avatar Vincent Bechu Committed by Romain Courteaud

[linshare] Improvement to use in officejs

parent 0115a857
...@@ -21,95 +21,141 @@ ...@@ -21,95 +21,141 @@
* JIO Linshare Storage. Type = "linshare". * JIO Linshare Storage. Type = "linshare".
* Linshare "database" storage. * Linshare "database" storage.
* http://download.linshare.org/components/linshare-core/2.2.2/ * http://download.linshare.org/components/linshare-core/2.2.2/
* Can't set up id, implied can't put new document
*/ */
/*global Blob, jIO, RSVP, UriTemplate*/ /*global Blob, jIO, RSVP, UriTemplate*/
/*jslint nomen: true*/ /*jslint nomen: true*/
(function (jIO, RSVP, Blob, UriTemplate) { (function (jIO, RSVP, Blob, UriTemplate) {
"use strict"; "use strict";
var BASE_URL = UriTemplate.parse("https://demo.linshare.org/linshare/webservice/rest/user/v2/documents/{uuid}"); var default_url = "https://softinst89769.host.vifib.net/erp5/portal_skins/" +
"erp5_http_proxy/ERP5Site_getHTTPResource?url=https://demo.linshare.org/" +
"linshare/webservice/rest/user/v2/documents/{uuid}",
default_token = "dXNlcjFAbGluc2hhcmUub3JnOnBhc3N3b3JkMQ==";
function makeRequest(options) { function makeRequest(storage, options) {
var ajax_param = { var ajax_param = {
type: options.type, type: options.type,
url: BASE_URL.expand({uuid: options.uuid || ""}), url: storage._url_template.expand({uuid: options.uuid || ""}),
headers : { headers : {
"Authorization": "Basic dXNlcjFAbGluc2hhcmUub3JnOnBhc3N3b3JkMQ==", "Authorization": "Basic " + storage._credential_token,
"Accept": "application/json" "Accept": "application/json"
} }
}; };
if (options.type === 'PUT') {
ajax_param.dataType = options.dataType;
ajax_param.headers['Content-Type'] = "application/json";
}
if (options.data) { if (options.data) {
ajax_param.data = options.data; ajax_param.data = options.data;
} }
if (options.download) {
ajax_param.url += '/download';
}
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax(ajax_param); return jIO.util.ajax(ajax_param);
}) })
.push(function (event) { .push(function (event) {
if (options.type === "PUT") {
return event.target.response.uuid;
}
if (options.download) {
return event.target.response;
}
return JSON.parse(event.target.response); return JSON.parse(event.target.response);
}); });
} }
function checkAttachmentMap(storage, id, name) {
checkDocumentMap(storage, id);
if (!storage._id_map[id].attachment.hasOwnProperty(name)) {
throw new jIO.util.jIOError(
"Can't find attachment with name :" + name,
404
);
}
}
function checkDocumentMap(storage, id) {
if (!storage._id_map.hasOwnProperty(id)) {
throw new jIO.util.jIOError(
"Can't find document with id : " + id,
404
);
}
}
/** /**
* The JIO Linshare Storage extension * The JIO Linshare Storage extension
* *
* @class LinshareStorage * @class LinshareStorage
* @constructor * @constructor
*/ */
function LinshareStorage() {} function LinshareStorage(spec) {
this._url_template = UriTemplate.parse(spec.url_template || default_url);
function createFormData(doc) { this._credential_token = spec.credential_token || default_token;
var data = new FormData(); this._id_map = {};
data.append('file', new Blob(), doc.title);
data.append('filesize', 0);
data.append('metadata', jIO.util.stringify(doc));
return data;
} }
LinshareStorage.prototype.put = function (id, doc) { LinshareStorage.prototype.put = function (id, doc) {
return makeRequest({ var storage = this,
data: createFormData(doc), data = new FormData();
type: "PUT", data.append('file', new Blob());
uuid: id data.append('filename', doc.title);
}) data.append('filesize', 0);
.push(function (event) { data.append('description', 'jio/document');
return result.uuid; data.append('metadata', jIO.util.stringify({
}, function (error) { doc: doc,
// Can't set id. id: id
if (error.target.status === 415) { }));
throw new jIO.util.jIOError(
"Can't create document with id : " + id,
400
);
}
throw error;
});
};
LinshareStorage.prototype.post = function (doc) { return makeRequest(this, {
return makeRequest({ data: data,
data: createFormData(doc),
type: "POST" type: "POST"
}) })
.push(function (result) { .push(function (result) {
return result.uuid; if (storage._id_map.hasOwnProperty(id)) {
storage._id_map[id].uuid = result.uuid;
} else {
storage._id_map[id] = {'uuid': result.uuid, attachment: {}};
}
return id;
}); });
}; };
LinshareStorage.prototype.remove = function (id) { LinshareStorage.prototype.remove = function (id) {
return makeRequest({ var storage = this;
type: "REMOVE", if (storage._id_map.hasOwnProperty(id)) {
uuid: id return makeRequest(storage, {
type: "DELETE",
uuid: storage._id_map[id].uuid
})
.push(function () {
var promise_list = [],
name;
for (name in storage._id_map[id].attachment) {
if (storage._id_map[id].attachment.hasOwnProperty(name)) {
promise_list.push(storage.removeAttachment(id, name));
}
}
return RSVP.all(promise_list);
})
.push(function () {
delete storage._id_map[id];
return id;
}); });
}
}; };
LinshareStorage.prototype.get = function (id) { LinshareStorage.prototype.get = function (id) {
return makeRequest({ checkDocumentMap(this, id);
return makeRequest(this, {
type: "GET", type: "GET",
uuid: id uuid: this._id_map[id].uuid
}) })
.push(function (result) { .push(function (result) {
return JSON.parse(result.metadata); return JSON.parse(result.metaData).doc;
}); });
}; };
...@@ -118,7 +164,7 @@ ...@@ -118,7 +164,7 @@
}; };
LinshareStorage.prototype.buildQuery = function () { LinshareStorage.prototype.buildQuery = function () {
return makeRequest({ return makeRequest(this, {
type: "GET" type: "GET"
}) })
.push(function (result) { .push(function (result) {
...@@ -126,58 +172,109 @@ ...@@ -126,58 +172,109 @@
len = result.length, len = result.length,
i; i;
for (i = 0 ; i < len ; i += 1) { for (i = 0 ; i < len ; i += 1) {
rows.push({id: result[i].uuid, value: {}}); if (result[i].hasOwnProperty('type')) {
if (result[i].description === 'jio/document') {
rows.push({id: JSON.parse(result[i].metaData).id, value: {}});
}
}
} }
return rows; return rows;
}); });
}; };
// Attachments link by field "description" - Dict
LinshareStorage.prototype.allAttachments = function (id) { LinshareStorage.prototype.allAttachments = function (id) {
return makeRequest({
type: "GET",
uuid: id
})
.push(function (result) {
if (result.filesize === 0) {
return [];
}
// Limit all storage to this attachment ( for now )
return [{"data": {}}];
});
}; };
LinshareStorage.prototype.putAttachment = function (id, name, blob) { LinshareStorage.prototype.putAttachment = function (id, name, blob) {
var data = new FormData(); var storage = this,
if (name !== 'data') { data = new FormData(),
throw new jIO.util.jIOError( uuid;
"Force to use only data as atachment name", if (!storage._id_map.hasOwnProperty(id)) {
401 throw new jIO.util.JIOError(
"Can't find document with id :" + id,
404
); );
} }
data.append('file', blob); data.append('file', blob);
data.append('filename', blob.name);
data.append('filesize', blob.size); data.append('filesize', blob.size);
return new RSVP.Queue() data.append('metadata', jIO.util.stringify({
.push(function () { 'id': id,
return makeRequest({ 'name': name
type: "PUT", }));
data.append('description', 'jio/attachment');
return makeRequest(storage, {
data: data, data: data,
uuid: id type: "POST"
}); })
.push(function (result) {
storage._id_map[id].attachment[name] = result.uuid;
return result.uuid;
}); });
}; };
LinshareStorage.prototype.getAttachment = function (id) { LinshareStorage.prototype.getAttachment = function (id, name) {
return new RSVP.Queue() checkAttachmentMap(this, id, name);
.push(function () { return makeRequest(this, {
return makeRequest({
type: "GET", type: "GET",
uuid: id uuid: this._id_map[id].attachment[name],
}); download: true
})
.push(function (result) {
return new Blob([result]);
}); });
}; };
LinshareStorage.prototype.removeAttachment = function (id, name) { LinshareStorage.prototype.removeAttachment = function (id, name) {
return this.putAttachment(id, name, new Blob()); if (this._id_map.hasOwnProperty(id) &&
this._id_map[id].attachment.hasOwnProperty(name)) {
return makeRequest(this, {
type: "DELETE",
uuid: this._id_map[id].attachment[name]
})
.push(function () {
delete this._id_map[id].attachment[name];
return id;
});
}
};
LinshareStorage.prototype.repair = function () {
var storage = this;
return makeRequest(this, {
type: "GET"
})
.push(function (result) {
var rows = [],
len = result.length,
i,
metadata,
row,
id;
for (i = 0 ; i < len ; i += 1) {
row = result[i];
if (row.hasOwnProperty('description')) {
if (row.description === 'jio/document') {
id = JSON.parse(row.metaData).id;
if (storage._id_map.hasOwnProperty(id)) {
storage._id_map[id].uuid = row.uuid;
} else {
storage._id_map[id] = {'uuid': row.uuid, attachment: {}};
}
} else if (row.description === 'jio/attachment') {
metadata = JSON.parse(row.metaData);
id = metadata.id;
if (!storage._id_map.hasOwnProperty(id)) {
storage._id_map[id] = {'uuid': undefined, attachment: {}};
}
storage._id_map[id].attachment[metadata.name] = row.uuid;
}
}
}
});
}; };
jIO.addStorage('linshare', LinshareStorage); jIO.addStorage('linshare', LinshareStorage);
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// DropboxStorage.allDocs // DropboxStorage.put
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("LinshareStorage.put"); module("LinshareStorage.put");
...@@ -52,13 +52,71 @@ ...@@ -52,13 +52,71 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "linshare" type: "linshare"
}); });
jio.put("foo", {"bar": "foo"}) jio.post({"bar": "foo"})
.then(function (id) {
return jio.put(id, {"bar": "2713"});
})
.then(function (res) { .then(function (res) {
console.warn(res);
equal(res, "foo");
})
.fail(function (error) {
console.warn(error);
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// DropboxStorage.allDocs
/////////////////////////////////////////////////////////////////
module("LinshareStorage.allDocs");
test("allDocs with include docs", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "linshare"
});
jio.allDocs({include_docs: true})
.then(function (result) {
deepEqual(result, {}, 'check result');
console.warn(res);
})
.fail(function (error) {
ok(false, error); ok(false, error);
}) })
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// DropboxStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("LinshareStorage.getAttachment");
test("getAttachment retrieve content", function () {
stop();
expect(1);
var jio = jIO.createJIO({
type: "linshare"
}),
doc_id;
jio.post({})
.then(function (id) {
doc_id = id;
return jio.putAttachment(id, "data", new Blob(['tralalaal']));
})
.then(function () {
return jio.getAttachment(doc_id, "data");
})
.then(function (result) {
deepEqual(new Blob(['tralalaal']), result, "Check Blob");
})
.fail(function (error) { .fail(function (error) {
equal(error.status_code, 400, "Check Status"); ok(false, error);
equal(error.message, "Can't create document with id : foo");
}) })
.always(function () { .always(function () {
start(); start();
......
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