Commit e6e4943b authored by Hamza's avatar Hamza Committed by Klaus Wölfel

Update the cloudooostorage to send parameters to wkhtmltopdf

With this update, jiodev will provide the possibility to specify parameters of conversion from html to pdf using cloudooo and wkhtmltopdf.

/reviewed-on !110
parent 5d72e7e1
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
* See https://www.nexedi.com/licensing for rationale and options. * See https://www.nexedi.com/licensing for rationale and options.
*/ */
/*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob, /*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array */ FileReader, ArrayBuffer, Uint8Array, navigator */
(function (window, RSVP, Blob, QueryFactory, Query, atob, (function (window, RSVP, Blob, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array) { FileReader, ArrayBuffer, Uint8Array, navigator) {
"use strict"; "use strict";
/* Safari does not define DOMError */ /* Safari does not define DOMError */
...@@ -28,6 +28,17 @@ ...@@ -28,6 +28,17 @@
window.DOMError = {}; window.DOMError = {};
} }
/* Document is not defined in ServiceWorkser */
if (window.document === undefined) {
window.document = {
createElementNS: function () {
throw new Error(
'document.createElementNS is not supported by ' + navigator.userAgent
);
}
};
}
var util = {}, var util = {},
jIO; jIO;
...@@ -555,4 +566,4 @@ ...@@ -555,4 +566,4 @@
window.jIO = jIO; window.jIO = jIO;
}(window, RSVP, Blob, QueryFactory, Query, atob, }(window, RSVP, Blob, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array)); FileReader, ArrayBuffer, Uint8Array, navigator));
...@@ -19,26 +19,50 @@ ...@@ -19,26 +19,50 @@
*/ */
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global jIO, RSVP, DOMParser, XMLSerializer*/ /*global document, jIO, RSVP, DOMParser, XMLSerializer*/
(function (jIO, RSVP, DOMParser, XMLSerializer) { (function (document, jIO, RSVP, DOMParser, XMLSerializer) {
"use strict"; "use strict";
var parser = new DOMParser(), var parser = new DOMParser(),
serializer = new XMLSerializer(); serializer = new XMLSerializer();
function makeXmlRpcRequest(file, from, to) { function makeXmlRpcRequest(file, from, to, conversion_kw) {
var xml = parser.parseFromString( var xml = parser.parseFromString(
'<?xml version="1.0" encoding="UTF-8"?><methodCall>' + '<?xml version="1.0" encoding="UTF-8"?><methodCall>' +
'<methodName>convertFile</methodName><params>' + '<methodName>convertFile</methodName><params>' +
'<param><value><string></string></value></param>' + '<param><value><string></string></value></param>' +
'<param><value><string></string></value></param>' + '<param><value><string></string></value></param>' +
'<param><value><string></string></value></param></params></methodCall>', '<param><value><string></string></value></param>' +
'<param><struct></struct></param>' +
'</params></methodCall>',
'text/xml' 'text/xml'
), ),
elt,
member,
name,
value,
key,
struct = xml.getElementsByTagName('struct'),
string_list = xml.getElementsByTagName('string'); string_list = xml.getElementsByTagName('string');
string_list[0].textContent = file; string_list[0].textContent = file;
string_list[1].textContent = from; string_list[1].textContent = from;
string_list[2].textContent = to; string_list[2].textContent = to;
if (conversion_kw) {
for (key in conversion_kw) {
if (conversion_kw.hasOwnProperty(key)) {
elt = document.createElementNS(null, conversion_kw[key][1]);
elt.textContent = conversion_kw[key][0];
value = document.createElementNS(null, "value");
value.appendChild(elt);
name = document.createElementNS(null, "name");
name.textContent = key;
member = document.createElementNS(null, "member");
member.appendChild(name);
member.appendChild(value);
struct[0].appendChild(member);
}
}
}
return serializer.serializeToString(xml); return serializer.serializeToString(xml);
} }
...@@ -47,7 +71,7 @@ ...@@ -47,7 +71,7 @@
* from a format to another * from a format to another
* return converted blob. * return converted blob.
**/ **/
function convert(url, blob, from, to) { function convert(url, blob, from, to, conversion_kw) {
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return jIO.util.readBlobAsDataURL(blob); return jIO.util.readBlobAsDataURL(blob);
...@@ -59,7 +83,8 @@ ...@@ -59,7 +83,8 @@
data: makeXmlRpcRequest( data: makeXmlRpcRequest(
result.target.result.split('base64,')[1], result.target.result.split('base64,')[1],
from, from,
to to,
conversion_kw
) )
}); });
}) })
...@@ -109,11 +134,13 @@ ...@@ -109,11 +134,13 @@
return this._sub_storage.getAttachment.apply(this._sub_storage, arguments); return this._sub_storage.getAttachment.apply(this._sub_storage, arguments);
}; };
CloudoooStorage.prototype.putAttachment = function (id, name, blob) { CloudoooStorage.prototype.putAttachment = function (id, name, blob,
conversion_kw
) {
var storage = this; var storage = this;
return storage.get(id) return storage.get(id)
.push(function (doc) { .push(function (doc) {
return convert(storage._url, blob, doc.from, doc.to); return convert(storage._url, blob, doc.from, doc.to, conversion_kw);
}) })
.push(function (converted_blob) { .push(function (converted_blob) {
return storage._sub_storage.putAttachment(id, name, converted_blob); return storage._sub_storage.putAttachment(id, name, converted_blob);
...@@ -138,4 +165,4 @@ ...@@ -138,4 +165,4 @@
jIO.addStorage('cloudooo', CloudoooStorage); jIO.addStorage('cloudooo', CloudoooStorage);
}(jIO, RSVP, DOMParser, XMLSerializer)); }(document, jIO, RSVP, DOMParser, XMLSerializer));
...@@ -392,8 +392,9 @@ ...@@ -392,8 +392,9 @@
'<methodName>convertFile</methodName><params><param><value>' + '<methodName>convertFile</methodName><params><param><value>' +
'<string>ZG9jdW1lbnRfZG9jeF9mb3JtYXQ=</string></value></param>' + '<string>ZG9jdW1lbnRfZG9jeF9mb3JtYXQ=</string></value></param>' +
'<param><value><string>docx</string></value></param>' + '<param><value><string>docx</string></value></param>' +
'<param><value><string>docy' + '<param><value><string>docy</string></value></param>' +
'</string></value></param></params></methodCall>', '<param><struct></struct></param>' +
'</params></methodCall>',
'text/xml' 'text/xml'
)); ));
...@@ -460,8 +461,9 @@ ...@@ -460,8 +461,9 @@
'<methodName>convertFile</methodName><params><param><value>' + '<methodName>convertFile</methodName><params><param><value>' +
'<string>ZG9jdW1lbnRfZG9jeF9mb3JtYXQ=</string></value></param>' + '<string>ZG9jdW1lbnRfZG9jeF9mb3JtYXQ=</string></value></param>' +
'<param><value><string>docx</string></value></param>' + '<param><value><string>docx</string></value></param>' +
'<param><value><string>docy' + '<param><value><string>docy</string></value></param>' +
'</string></value></param></params></methodCall>', '<param><struct></struct></param>' +
'</params></methodCall>',
'text/xml' 'text/xml'
)); ));
...@@ -493,4 +495,60 @@ ...@@ -493,4 +495,60 @@
}); });
}); });
test("putAttachment convert from html to pdf", function () {
stop();
expect(8);
var server = this.server,
jio = this.jio,
blob = new Blob(["document_pdf__format"], {type: "pdf"}),
blob_convert = new Blob(["document_html_format"], {type: "html"}),
result = serializer.serializeToString(parser.parseFromString(
'<?xml version="1.0" encoding="UTF-8"?><methodCall>' +
'<methodName>convertFile</methodName><params><param><value>' +
'<string>ZG9jdW1lbnRfaHRtbF9mb3JtYXQ=</string></value></param>' +
'<param><value><string>html</string></value></param>' +
'<param><value><string>pdf</string></value></param>' +
'<param><struct><member><name>encoding</name>' +
'<value><string>utf8</string></value></member></struct></param>' +
'</params></methodCall>',
'text/xml'
));
this.server.respondWith("POST", cloudooo_url, [200, {
"Content-Type": "text/xml"
}, '<?xml version="1.0"?>' +
'<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: "html", to: "pdf"};
};
return jio.putAttachment("bar", "data", blob_convert,
{"encoding": ["utf8", "string"]})
.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();
});
});
}(jIO, Blob, sinon, DOMParser, XMLSerializer)); }(jIO, Blob, sinon, DOMParser, XMLSerializer));
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