Commit 8e281d79 authored by Vincent Bechu's avatar Vincent Bechu

[cloudoostorage] Add cloudoo storage

This storage add convertion on putAttachment using document as operation :
doc: {from: "", to: "", status: "convert"}
status change to converted or error

cloudo storage will be use to convert file from cloudoo server
parent 261c4159
...@@ -182,7 +182,8 @@ module.exports = function (grunt) { ...@@ -182,7 +182,8 @@ module.exports = function (grunt) {
'src/jio.storage/indexeddbstorage.js', 'src/jio.storage/indexeddbstorage.js',
'src/jio.storage/cryptstorage.js', 'src/jio.storage/cryptstorage.js',
'src/jio.storage/websqlstorage.js', 'src/jio.storage/websqlstorage.js',
'src/jio.storage/fbstorage.js' 'src/jio.storage/fbstorage.js',
'src/jio.storage/cloudooostorage.js'
], ],
dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js' dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
// dest: 'jio.js' // dest: 'jio.js'
......
/*jslint nomen: true*/
/*global jIO, RSVP, DOMParser, XMLSerializer*/
(function (jIO, RSVP, DOMParser, XMLSerializer) {
"use strict";
var parser = new DOMParser(),
serializer = new XMLSerializer();
function makeXmlRpcRequest(file, from, to) {
var xml = parser.parseFromString(
'<?xml version="1.0" encoding="UTF-8"?><methodCall>' +
'<methodName>convertFile</methodName><params>' +
'<param><value><string></string></value></param>' +
'<param><value><string></string></value></param>' +
'<param><value><string></string></value></param></params></methodCall>',
'text/xml'
),
string_list = xml.getElementsByTagName('string');
string_list[0].textContent = file;
string_list[1].textContent = from;
string_list[2].textContent = to;
return serializer.serializeToString(xml);
}
/**
* convert a blob
* from a format to another
* return converted blob.
**/
function convert(url, blob, from, to) {
return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsDataURL(blob);
})
.push(function (result) {
return jIO.util.ajax({
type: 'POST',
url: url,
data: makeXmlRpcRequest(
result.target.result.split('base64,')[1],
from,
to
)
});
})
.push(function (result) {
var data = parser.parseFromString(
result.target.responseText,
"application/xml"
), error;
if (data.getElementsByTagName('fault').length === 0) {
return jIO.util.base64toBlob(
data.getElementsByTagName('string')[0].textContent,
to
);
}
error = new jIO.util.jIOError('Conversion failed', 500);
error.detail = data.getElementsByTagName('string')[0].textContent;
throw error;
});
}
/**
* The jIO CloudoooStorage extension
*
* Convert attachment : att_id?from="format"&to="format"
*
* @class CloudoooStorage
* @constructor
*/
function CloudoooStorage(spec) {
this._url = spec.url;
this._conversion_stack = {};
}
CloudoooStorage.prototype.get = function (id) {
if (this._conversion_stack.hasOwnProperty(id)) {
return this._conversion_stack[id].doc;
}
throw new jIO.util.jIOError("Can't find document " + id, 404);
};
CloudoooStorage.prototype.put = function (id, doc) {
this._conversion_stack[id] = {doc: doc, attachment_dict: {}};
return id;
};
CloudoooStorage.prototype.remove = function (id) {
delete this._conversion_stack[id];
return id;
};
CloudoooStorage.prototype.getAttachment = function (id, name) {
var result;
if (this._conversion_stack[id].attachment_dict.hasOwnProperty(name)) {
result = this._conversion_stack[id].attachment_dict[name];
delete this._conversion_stack[id].attachment_dict[name];
return result;
}
throw jIO.util.jIOError(
"Can't find attachment " + name + " for document " + id,
404
);
};
CloudoooStorage.prototype.putAttachment = function (id, name, blob) {
var storage = this;
return new RSVP.Queue()
.push(function () {
return storage.get(id);
})
.push(function (doc) {
return convert(storage._url, blob, doc.from, doc.to);
})
.push(function (converted_blob) {
storage._conversion_stack[id].attachment_dict[name] = converted_blob;
return id;
});
};
CloudoooStorage.prototype.allAttachments = function (id) {
var result = {}, name;
for (name in this._conversion_stack[id].attachment_dict) {
if (this._conversion_stack[id].attachment_dict.hasOwnProperty(name)) {
result[name] = {};
}
}
return result;
};
CloudoooStorage.prototype.repair = function () {
return;
};
CloudoooStorage.prototype.hasCapacity = function (name) {
return name === 'list';
};
CloudoooStorage.prototype.buildQuery = function () {
var result = [], id;
for (id in this._conversion_stack) {
if (this._conversion_stack.hasOwnProperty(id)) {
result.push({id: id, value: {}});
}
}
return result;
};
jIO.addStorage('cloudooo', CloudoooStorage);
}(jIO, RSVP, DOMParser, XMLSerializer));
/*jslint nomen: true*/
/*global jIO, Blob, sinon*/
(function (jIO, Blob, sinon) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
start = QUnit.start,
ok = QUnit.ok,
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
module = QUnit.module,
cloudooo_url = 'https://www.exemple.org/';
/////////////////////////////////////////////////////////////////
// cloudoooStorage.constructor
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.constructor");
test("create substorage", function () {
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url
});
equal(jio.__type, "cloudooo");
equal(jio.__storage._url, cloudooo_url);
deepEqual(jio.__storage._conversion_stack, {});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.get
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.get");
test("get return stack document", function () {
stop();
expect(1);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url
});
jio.__storage._conversion_stack.foo = {doc: {"bar": "foo"}};
jio.get("foo")
.then(function (result) {
deepEqual(result, {
"bar": "foo"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.put
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.put");
test("put called substorage put", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url
});
jio.put("bar", {"title": "foo"})
.then(function (result) {
equal(result, "bar");
deepEqual(
jio.__storage._conversion_stack,
{bar: { doc: {"title": "foo"}, attachment_dict: {}}},
"Check document"
);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.remove
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.remove");
test("remove called substorage remove", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url
});
jio.__storage._conversion_stack.bar = {doc: {}};
jio.remove("bar")
.then(function (result) {
equal(result, "bar");
deepEqual(jio.__storage._conversion_stack, {}, "Check empty stack");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.buildQuery");
test("buildQuery list all documents", function () {
stop();
expect(1);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url
});
jio.__storage._conversion_stack = {
"bar": {doc: {}, attachment_dict: {}},
"foo": {doc: {}, attachment_dict: {}}
};
jio.allDocs()
.then(function (result) {
deepEqual(result, {
data: {
rows: [{
id: "bar",
value: {}
}, {
id: "foo",
value: {}
}],
total_rows: 2
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.allAttachments");
test("check allAttachments", function () {
stop();
expect(1);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url
});
jio.__storage._conversion_stack.bar = {
doc: {},
attachment_dict: {"foo": new Blob(["a"]), "bar": new Blob(["b"])}
};
jio.allAttachments("bar")
.then(function (result) {
deepEqual(
result,
{"foo": {}, "bar": {}},
"Check allAttachments"
);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.getAttachment");
test("getAttachment return converted attachment", function () {
stop();
expect(1);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url
}), blob = new Blob(["converted"]);
jio.__storage._conversion_stack.bar = {
doc: {},
attachment_dict: {foo: blob}
};
jio.getAttachment("bar", "foo")
.then(function (result) {
equal(result, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.putAttachment
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.putAttachment", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("putAttachment convert from docx to docy", function () {
stop();
expect(5);
var server = this.server,
jio = this.jio,
blob = new Blob(["document_docy_format"], {type: "docy"}),
blob_convert = new Blob(["document_docx_format"], {type: "docx"});
this.server.respondWith("POST", cloudooo_url, [200, {
"Content-Type": "text/xml"
}, '<?xml version="1.0" encoding="UTF-8"?>' +
'<string>ZG9jdW1lbnRhdWZvcm1hdGRvY3k=</string>']);
jio.put("bar", {from: "docx", to: "docy"})
.then(function () {
return jio.putAttachment("bar", "data", blob_convert);
})
.then(function () {
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, cloudooo_url);
equal(
server.requests[0].requestBody,
'<?xml version="1.0" encoding=\"UTF-8\"?><methodCall>' +
'<methodName>convertFile</methodName><params><param><value>' +
'<string>ZG9jdW1lbnRfZG9jeF9mb3JtYXQ=</string></value></param>' +
'<param><value><string>docx</string></value></param>' +
'<param><value><string>docy' +
'</string></value></param></params></methodCall>'
);
deepEqual(
jio.__storage._conversion_stack.bar.attachment_dict.data,
blob,
"check converted blob"
);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("putAttachment fail to convert", function () {
stop();
expect(7);
var error = [
"<?xml version='1.0'?>",
"<methodResponse>",
"<fault>",
"<value><struct>",
"<member>",
"<name>faultCode</name>",
"<value><int>1</int></value>",
"</member>",
"<member>",
"<name>faultString</name>",
"<value><string>errorFromCloudooo</string></value>",
"</member>",
"</struct></value>",
"</fault>",
"</methodResponse>"]
.join(""),
server = this.server,
jio = this.jio,
blob = new Blob(["document_docx_format"], {type: "docx"});
this.server.respondWith("POST", cloudooo_url, [200, {
"Content-Type": "text/xml"
}, error]);
jio.put("bar", {from: "docx", to: "docy"})
.then(function () {
return jio.putAttachment("bar", "data", blob);
})
.fail(function (error) {
equal(server.requests.length, 1);
equal(server.requests[0].method, "POST");
equal(server.requests[0].url, cloudooo_url);
equal(
server.requests[0].requestBody,
'<?xml version="1.0" encoding=\"UTF-8\"?><methodCall>' +
'<methodName>convertFile</methodName><params><param><value>' +
'<string>ZG9jdW1lbnRfZG9jeF9mb3JtYXQ=</string></value></param>' +
'<param><value><string>docx</string></value></param>' +
'<param><value><string>docy' +
'</string></value></param></params></methodCall>'
);
equal(error.status_code, 500);
equal(error.message, 'Conversion failed');
equal(error.detail, 'errorFromCloudooo');
})
.always(function () {
start();
});
});
}(jIO, Blob, sinon));
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
<!--script src="jio.storage/qiniustorage.tests.js"></script--> <!--script src="jio.storage/qiniustorage.tests.js"></script-->
<!--script src="jio.storage/indexstorage.tests.js"></script--> <!--script src="jio.storage/indexstorage.tests.js"></script-->
<script src="jio.storage/cryptstorage.tests.js"></script> <script src="jio.storage/cryptstorage.tests.js"></script>
<script src="jio.storage/cloudooostorage.tests.js"></script>
<script src="jio.storage/dropboxstorage.tests.js"></script> <script src="jio.storage/dropboxstorage.tests.js"></script>
<script src="jio.storage/zipstorage.tests.js"></script> <script src="jio.storage/zipstorage.tests.js"></script>
<script src="jio.storage/gdrivestorage.tests.js"></script> <script src="jio.storage/gdrivestorage.tests.js"></script>
......
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