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

[linshare] First usable storage

parent 89ecbaff
......@@ -32,15 +32,22 @@
function makeRequest(options) {
var ajax_param = {
type: options.type,
url: BASE_URL.expand({uuid: options.id || ""}),
url: BASE_URL.expand({uuid: options.uuid || ""}),
headers : {
"Authorization": "Basic dXNlcjFAbGluc2hhcmUub3JnOnBhc3N3b3JkMQ==",
"Accept": "application/json"
}
};
if (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 @@
*/
function LinshareStorage() {}
function createFormData(id, doc) {
function createFormData(doc) {
var data = new FormData();
data.append('file', new Blob(), doc.title);
data.append('filesize', 0);
......@@ -60,43 +67,49 @@
}
LinshareStorage.prototype.put = function (id, doc) {
return new RSVP.Queue()
.push(function () {
return makeRequest({
data: createFormData(id, doc),
type: "PUT",
uuid: id
});
return makeRequest({
data: createFormData(doc),
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) {
return new RSVP.Queue()
.push(function () {
return makeRequest({
data: createFormData(id, doc),
type: "POST"
});
return makeRequest({
data: createFormData(doc),
type: "POST"
})
.push(function (result) {
return result.uuid;
});
};
LinshareStorage.prototype.remove = function (id) {
return new RSVP.Queue()
.push(function () {
return makeRequest({
type: "REMOVE",
uuid: id
});
});
return makeRequest({
type: "REMOVE",
uuid: id
});
};
LinshareStorage.prototype.get = function (id) {
return new RSVP.Queue()
.push(function () {
return makeRequest({
type: "GET",
uuid: id
});
return makeRequest({
type: "GET",
uuid: id
})
.push(function (result) {
return JSON.parse(result.metadata);
});
};
......@@ -104,118 +117,69 @@
return name === "list";
};
LinshareStorage.prototype.buidQuery = function () {
return new RSVP.Queue()
.push(function () {
return makeRequest({
type: "GET"
});
LinshareStorage.prototype.buildQuery = function () {
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) {
id = restrictDocumentId(id);
return recursiveAllAttachments({}, this._access_token, 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": {}}];
});
};
//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.getAttachment = function (id, name) {
var context = this;
id = restrictDocumentId(id);
restrictAttachmentId(name);
var data = new FormData();
if (name !== 'data') {
throw new jIO.util.jIOError(
"Force to use only data as atachment name",
401
);
}
data.append('file', blob);
data.append('filesize', blob.size);
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
url: GET_URL,
type: "POST",
dataType: "blob",
headers: {
"Authorization": "Bearer " + context._access_token,
"Linshare-API-Arg": JSON.stringify({"path": id + "/" + name})
}
return makeRequest({
type: "PUT",
data: data,
uuid: id
});
})
.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.removeAttachment = function (id, name) {
var that = this;
id = restrictDocumentId(id);
restrictAttachmentId(name);
return new RSVP.Queue()
LinshareStorage.prototype.getAttachment = function (id) {
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax({
type: "POST",
url: REMOVE_URL,
headers: {
"Authorization": "Bearer " + that._access_token,
"Content-Type": "application/json"
},
data: JSON.stringify({"path": id + "/" + name})
return makeRequest({
type: "GET",
uuid: id
});
}).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, RSVP, Blob, UriTemplate));
......@@ -42,22 +42,23 @@
});
/////////////////////////////////////////////////////////////////
// DropboxStorage.put
// DropboxStorage.allDocs
/////////////////////////////////////////////////////////////////
module("LinshareStorage.put");
test("put document", function () {
test("put new document", function () {
stop();
expect(1);
expect(2);
var jio = jIO.createJIO({
type: "linshare"
});
jio.put("foo", {bar: 'foo'})
jio.put("foo", {"bar": "foo"})
.then(function (res) {
equal(res, "foo", "test ended");
ok(false, 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 () {
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