Commit 808c376f authored by Vincent Bechu's avatar Vincent Bechu Committed by 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 bd8579dd
......@@ -182,7 +182,8 @@ module.exports = function (grunt) {
'src/jio.storage/indexeddbstorage.js',
'src/jio.storage/cryptstorage.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: '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.querySelector('string').textContent,
to
);
}
error = new jIO.util.jIOError('Conversion failed', 500);
error.detail = data.querySelector('string').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._sub_storage = jIO.createJIO(spec.sub_storage);
}
CloudoooStorage.prototype.get = function () {
return this._sub_storage.get.apply(this._sub_storage, arguments);
};
CloudoooStorage.prototype.put = function () {
return this._sub_storage.put.apply(this._sub_storage, arguments);
};
CloudoooStorage.prototype.remove = function () {
return this._sub_storage.remove.apply(this._sub_storage, arguments);
};
CloudoooStorage.prototype.getAttachment = function () {
return this._sub_storage.getAttachment.apply(this._sub_storage, arguments);
};
CloudoooStorage.prototype.putAttachment = function (id, name, blob) {
var storage = this;
return storage.get(id)
.push(function (doc) {
return convert(storage._url, blob, doc.from, doc.to);
})
.push(function (converted_blob) {
return storage._sub_storage.putAttachment(id, name, converted_blob);
});
};
CloudoooStorage.prototype.allAttachments = function () {
return this._sub_storage.allAttachments.apply(this._sub_storage, arguments);
};
CloudoooStorage.prototype.repair = function () {
return this._sub_storage.repair.apply(this._sub_storage, arguments);
};
CloudoooStorage.prototype.hasCapacity = function () {
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments);
};
CloudoooStorage.prototype.buildQuery = function () {
return this._sub_storage.buildQuery.apply(this._sub_storage, arguments);
};
jIO.addStorage('cloudooo', CloudoooStorage);
}(jIO, RSVP, DOMParser, XMLSerializer));
/*jslint nomen: true*/
/*global jIO, Blob, sinon, DOMParser, XMLSerializer*/
(function (jIO, Blob, sinon, DOMParser, XMLSerializer) {
"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,
throws = QUnit.throws,
cloudooo_url = 'https://www.exemple.org/',
parser = new DOMParser(),
serializer = new XMLSerializer();
/////////////////////////////////////////////////////////////////
// Custom test substorage definition
/////////////////////////////////////////////////////////////////
function Storage200() {
return this;
}
jIO.addStorage('cloudooostorage200', Storage200);
/////////////////////////////////////////////////////////////////
// cloudoooStorage.constructor
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.constructor");
test("create substorage", function () {
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: 'cloudooostorage200'
}
});
equal(jio.__type, "cloudooo");
equal(jio.__storage._url, cloudooo_url);
equal(jio.__storage._sub_storage.__type, "cloudooostorage200");
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.get
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.get");
test("get called substorage get", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: 'cloudooostorage200'
}
});
Storage200.prototype.get = function (param) {
equal(param, "bar", "get 200 called");
return {title: "foo"};
};
jio.get("bar")
.then(function (result) {
deepEqual(result, {
"title": "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(3);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: 'cloudooostorage200'
}
});
Storage200.prototype.put = function (id, param) {
equal(id, "bar", "put 200 called");
deepEqual(param, {"title": "foo"}, "put 200 called");
return id;
};
jio.put("bar", {"title": "foo"})
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// uuidStorage.remove
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.remove");
test("remove called substorage remove", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: 'cloudooostorage200'
}
});
Storage200.prototype.remove = function (param) {
equal(param, "bar", "remove 200 called");
return param._id;
};
jio.remove("bar")
.then(function (result) {
equal(result, "bar");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.hasCapacity");
test("hasCapacity return substorage value", function () {
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: "cloudooostorage200"
}
});
delete Storage200.prototype.hasCapacity;
throws(
function () {
jio.hasCapacity("foo");
},
function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.status_code, 501);
equal(error.message,
"Capacity 'foo' is not implemented on 'cloudooostorage200'");
return true;
}
);
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.buildQuery");
test("buildQuery return substorage buildQuery", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: 'cloudooostorage200'
}
});
Storage200.prototype.hasCapacity = function () {
return true;
};
Storage200.prototype.buildQuery = function (options) {
deepEqual(options, {
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
uuid: 'title: "two"'
}, "allDocs parameter");
return "bar";
};
jio.allDocs({
include_docs: false,
sort_on: [["title", "ascending"]],
limit: [5],
select_list: ["title", "id"],
uuid: 'title: "two"'
})
.then(function (result) {
deepEqual(result, {
data: {
rows: "bar",
total_rows: 3
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.repair
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.repair");
test("repair called substorage repair", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: 'cloudooostorage200'
}
}),
expected_options = {foo: "bar"};
Storage200.prototype.repair = function (options) {
deepEqual(options, expected_options, "repair 200 called");
return "OK";
};
jio.repair(expected_options)
.then(function (result) {
equal(result, "OK");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.allAttachments");
test("get called substorage allAttachments", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: 'cloudooostorage200'
}
});
Storage200.prototype.allAttachments = function (param) {
equal(param, "bar", "allAttachments 200 called");
return {attachmentname: {}};
};
jio.allAttachments("bar")
.then(function (result) {
deepEqual(result, {
attachmentname: {}
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// cloudoooStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("cloudoooStorage.getAttachment");
test("getAttachment called substorage getAttachment", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "cloudooo",
url: cloudooo_url,
sub_storage: {
type: 'cloudooostorage200'
}
}),
blob = new Blob([""]);
Storage200.prototype.getAttachment = function (id, name) {
equal(id, "bar", "getAttachment 200 called");
equal(name, "foo", "getAttachment 200 called");
return blob;
};
jio.getAttachment("bar", "foo")
.then(function (result) {
deepEqual(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,
sub_storage: {
type: 'cloudooostorage200'
}
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("putAttachment convert from docx to docy", function () {
stop();
expect(8);
var server = this.server,
jio = this.jio,
blob = new Blob(["document_docy_format"], {type: "docy"}),
blob_convert = new Blob(["document_docx_format"], {type: "docx"}),
result = serializer.serializeToString(parser.parseFromString(
'<?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>',
'text/xml'
));
this.server.respondWith("POST", cloudooo_url, [200, {
"Content-Type": "text/xml"
}, '<?xml version="1.0" encoding="UTF-8"?>' +
'<string>ZG9jdW1lbnRhdWZvcm1hdGRvY3k=</string>']);
Storage200.prototype.putAttachment = function (id, name, blob2) {
equal(id, "bar", "putAttachment 200 called");
equal(name, "data", "putAttachment 200 called");
deepEqual(blob2, blob, "putAttachment 200 called");
return "OK";
};
Storage200.prototype.get = function (id) {
equal(id, "bar", "get 200 called");
return {from: "docx", to: "docy"};
};
return jio.putAttachment("bar", "data", blob_convert)
.then(function () {
equal(server.requests.length, 1, "Requests Length");
equal(server.requests[0].method, "POST", "Request Method");
equal(server.requests[0].url, cloudooo_url, "Request Url");
deepEqual(
server.requests[0].requestBody,
result,
"Request Body"
);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("putAttachment fail to convert", function () {
stop();
expect(8);
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"}),
result = serializer.serializeToString(parser.parseFromString(
'<?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>',
'text/xml'
));
this.server.respondWith("POST", cloudooo_url, [200, {
"Content-Type": "text/xml"
}, error]);
Storage200.prototype.get = function (id) {
equal(id, "bar", "get 200 called");
return {from: "docx", to: "docy"};
};
return jio.putAttachment("bar", "data", blob)
.fail(function (error) {
equal(server.requests.length, 1, "Requests Length");
equal(server.requests[0].method, "POST", "Request Method");
equal(server.requests[0].url, cloudooo_url, "Request Url");
equal(
server.requests[0].requestBody,
result,
"Request Body"
);
equal(error.status_code, 500, "Error status code");
equal(error.message, 'Conversion failed', "Error message");
equal(error.detail, 'errorFromCloudooo', "Error detail");
})
.always(function () {
start();
});
});
}(jIO, Blob, sinon, DOMParser, XMLSerializer));
......@@ -50,6 +50,7 @@
<!--script src="jio.storage/qiniustorage.tests.js"></script-->
<!--script src="jio.storage/indexstorage.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/zipstorage.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