Commit 92d34708 authored by Vincent Bechu's avatar Vincent Bechu

[linshare] First usable storage

parent 89ecbaff
...@@ -32,15 +32,22 @@ ...@@ -32,15 +32,22 @@
function makeRequest(options) { function makeRequest(options) {
var ajax_param = { var ajax_param = {
type: options.type, type: options.type,
url: BASE_URL.expand({uuid: options.id || ""}), url: BASE_URL.expand({uuid: options.uuid || ""}),
headers : { headers : {
"Authorization": "Basic dXNlcjFAbGluc2hhcmUub3JnOnBhc3N3b3JkMQ==", "Authorization": "Basic dXNlcjFAbGluc2hhcmUub3JnOnBhc3N3b3JkMQ==",
"Accept": "application/json"
} }
}; };
if (options.data) { if (options.data) {
ajax_param.data = options.data; ajax_param.data = options.data;
} }
return jIO.util.ajax(ajax_param); return new RSVP.Queue()
.push(function () {
return jIO.util.ajax(ajax_param);
})
.push(function (event) {
return JSON.parse(event.target.response);
});
} }
/** /**
...@@ -51,7 +58,7 @@ ...@@ -51,7 +58,7 @@
*/ */
function LinshareStorage() {} function LinshareStorage() {}
function createFormData(id, doc) { function createFormData(doc) {
var data = new FormData(); var data = new FormData();
data.append('file', new Blob(), doc.title); data.append('file', new Blob(), doc.title);
data.append('filesize', 0); data.append('filesize', 0);
...@@ -60,43 +67,49 @@ ...@@ -60,43 +67,49 @@
} }
LinshareStorage.prototype.put = function (id, doc) { LinshareStorage.prototype.put = function (id, doc) {
return new RSVP.Queue() return makeRequest({
.push(function () { data: createFormData(doc),
return makeRequest({ type: "PUT",
data: createFormData(id, doc), uuid: id
type: "PUT", })
uuid: id .push(function (event) {
}); return result.uuid;
}, function (error) {
// Can't set 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) { LinshareStorage.prototype.post = function (doc) {
return new RSVP.Queue() return makeRequest({
.push(function () { data: createFormData(doc),
return makeRequest({ type: "POST"
data: createFormData(id, doc), })
type: "POST" .push(function (result) {
}); return result.uuid;
}); });
}; };
LinshareStorage.prototype.remove = function (id) { LinshareStorage.prototype.remove = function (id) {
return new RSVP.Queue() return makeRequest({
.push(function () { type: "REMOVE",
return makeRequest({ uuid: id
type: "REMOVE", });
uuid: id
});
});
}; };
LinshareStorage.prototype.get = function (id) { LinshareStorage.prototype.get = function (id) {
return new RSVP.Queue() return makeRequest({
.push(function () { type: "GET",
return makeRequest({ uuid: id
type: "GET", })
uuid: id .push(function (result) {
}); return JSON.parse(result.metadata);
}); });
}; };
...@@ -104,118 +117,69 @@ ...@@ -104,118 +117,69 @@
return name === "list"; return name === "list";
}; };
LinshareStorage.prototype.buidQuery = function () { LinshareStorage.prototype.buildQuery = function () {
return new RSVP.Queue() return makeRequest({
.push(function () { type: "GET"
return makeRequest({ })
type: "GET" .push(function (result) {
}); var rows = [],
len = result.length,
i;
for (i = 0 ; i < len ; i += 1) {
rows.push({id: result[i].uuid, value: {}});
}
return rows;
}); });
}; };
LinshareStorage.prototype.allAttachments = function (id) { LinshareStorage.prototype.allAttachments = function (id) {
id = restrictDocumentId(id); return makeRequest({
return recursiveAllAttachments({}, this._access_token, id); type: "GET",
uuid: id
})
.push(function (result) {
if (result.filesize === 0) {
return [];
}
// Limit all storage to this attachment ( for now )
return [{"data": {}}];
});
}; };
//currently, putAttachment will fail with files larger than 150MB,
//due to the Linshare API. the API provides the "chunked_upload" method
//to pass this limit, but upload process becomes more complex to implement.
//
//putAttachment will also create a folder if you try to put an attachment
//to an inexisting foler.
LinshareStorage.prototype.putAttachment = function (id, name, blob) { LinshareStorage.prototype.putAttachment = function (id, name, blob) {
}; var data = new FormData();
if (name !== 'data') {
LinshareStorage.prototype.getAttachment = function (id, name) { throw new jIO.util.jIOError(
var context = this; "Force to use only data as atachment name",
401
id = restrictDocumentId(id); );
restrictAttachmentId(name); }
data.append('file', blob);
data.append('filesize', blob.size);
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ return makeRequest({
url: GET_URL, type: "PUT",
type: "POST", data: data,
dataType: "blob", uuid: id
headers: {
"Authorization": "Bearer " + context._access_token,
"Linshare-API-Arg": JSON.stringify({"path": id + "/" + name})
}
}); });
})
.push(function (evt) {
if (evt.target.response instanceof Blob) {
return evt.target.response;
}
return new Blob(
[evt.target.responseText],
{"type": evt.target.getResponseHeader('Content-Type') ||
"application/octet-stream"}
);
}, function (error) {
if (error.target !== undefined && error.target.status === 409) {
if (!(error.target.response instanceof Blob)) {
var err_content = JSON.parse(error.target.responseText);
if ((err_content.error['.tag'] === 'path') &&
(err_content.error.path['.tag'] === 'not_found')) {
throw new jIO.util.jIOError("Cannot find attachment: " +
id + "/, " + name, 404);
}
throw error;
}
return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsText(error.target.response);
})
.push(function (evt) {
var err_content2 = JSON.parse(evt.target.result);
if ((err_content2.error['.tag'] === 'path') &&
(err_content2.error.path['.tag'] === 'not_found')) {
throw new jIO.util.jIOError("Cannot find attachment: " +
id + "/, " + name, 404);
}
throw error;
});
}
throw error;
}); });
}; };
//removeAttachment removes also directories.(due to Linshare API) LinshareStorage.prototype.getAttachment = function (id) {
return new RSVP.Queue()
LinshareStorage.prototype.removeAttachment = function (id, name) {
var that = this;
id = restrictDocumentId(id);
restrictAttachmentId(name);
return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.ajax({ return makeRequest({
type: "POST", type: "GET",
url: REMOVE_URL, uuid: id
headers: {
"Authorization": "Bearer " + that._access_token,
"Content-Type": "application/json"
},
data: JSON.stringify({"path": id + "/" + name})
}); });
}).push(undefined, function (error) {
if (error.target !== undefined && error.target.status === 409) {
var err_content = JSON.parse(error.target.response ||
error.target.responseText);
if ((err_content.error['.tag'] === 'path_lookup') &&
(err_content.error.path_lookup['.tag'] === 'not_found')) {
throw new jIO.util.jIOError("Cannot find attachment: " +
id + "/, " + name, 404);
}
}
throw error;
}); });
}; };
LinshareStorage.prototype.removeAttachment = function (id, name) {
return this.putAttachment(id, name, new Blob());
};
jIO.addStorage('linshare', LinshareStorage); jIO.addStorage('linshare', LinshareStorage);
}(jIO, RSVP, Blob, UriTemplate)); }(jIO, RSVP, Blob, UriTemplate));
...@@ -42,22 +42,23 @@ ...@@ -42,22 +42,23 @@
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// DropboxStorage.put // DropboxStorage.allDocs
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("LinshareStorage.put"); module("LinshareStorage.put");
test("put document", function () { test("put new document", function () {
stop(); stop();
expect(1); expect(2);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "linshare" type: "linshare"
}); });
jio.put("foo", {bar: 'foo'}) jio.put("foo", {"bar": "foo"})
.then(function (res) { .then(function (res) {
equal(res, "foo", "test ended"); ok(false, error);
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); equal(error.status_code, 400, "Check Status");
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