Permalink
Browse files

Added support for multipart/form-data content type.

  • Loading branch information...
1 parent 7b200f7 commit f16aca577f80e53c2c81d7255749f7ac19237320 @pdfcrowd committed Jul 4, 2012
Showing with 73 additions and 11 deletions.
  1. +73 −11 lib/pdfcrowd.js
View
@@ -19,16 +19,25 @@ var Pdfcrowd = function(username, apikey) {
// Converts raw HTML code
//
Pdfcrowd.prototype.convertHtml = function(html, callbacks, options) {
- callbacks = prepareCallbacks(callbacks);
- requestQueue.addRequest([this, html, '/api/pdf/convert/html/', callbacks, options]);
+ requestQueue.addRequest([this, {
+ src: html,
+ endpoint: '/api/pdf/convert/html/',
+ callbacks: prepareCallbacks(callbacks),
+ apiOptions: options
+ }]);
}
//
// Converts a web page
//
Pdfcrowd.prototype.convertURI = function(uri, callbacks, options) {
- requestQueue.addRequest([this, uri, '/api/pdf/convert/uri/', callbacks, options]);
+ requestQueue.addRequest([this, {
+ src: uri,
+ endpoint: '/api/pdf/convert/uri/',
+ callbacks: prepareCallbacks(callbacks),
+ apiOptions: options
+ }]);
}
@@ -39,7 +48,13 @@ Pdfcrowd.prototype.convertFile = function(fname, callbacks, options) {
var that = this;
fs.readFile(fname, function(err, data) {
if (err) throw err;
- requestQueue.addRequest([that, data.toString(), '/api/pdf/convert/html/', callbacks, options]);
+ requestQueue.addRequest([that, {
+ fname: fname,
+ data: data.toString('binary'),
+ endpoint: '/api/pdf/convert/html/',
+ callbacks: prepareCallbacks(callbacks),
+ apiOptions: options
+ }]);
});
}
@@ -125,29 +140,76 @@ var prepareCallbacks = function(callbacks) {
}
+var guess_mimetype = function(fname)
+{
+ if ((/.*\.html?$/i).test(fname))
+ return 'text/html';
+
+ return 'application/octet-stream';
+}
+
+var boundary = '----------ThIs_Is_tHe_bOUnDary_$';
+
+//
+// Multipart encodes POST data
+//
+var encodeMultipartPostData = function(postData, fname, data)
+{
+ var body = new Array();
+ for(var field in postData) {
+ if (postData[field]) {
+ body.push('--' + boundary);
+ body.push('Content-Disposition: form-data; name="' + field + '"');
+ body.push('');
+ body.push(postData[field].toString());
+ }
+ }
+
+ // file
+ body.push('--' + boundary);
+ body.push('Content-Disposition: form-data; name="src"; filename="' + fname + '"');
+ body.push('Content-Type: ' + guess_mimetype(fname));
+ body.push('');
+ body.push(data);
+
+ // finalize
+ body.push('--' + boundary + '--');
+ body.push('');
+ return body.join('\r\n');
+}
+
//
// Calls the API
//
-var convertInternal = function(that, src, endpoint, callbacks, options) {
+var convertInternal = function(that, opts) {
// form POST data
- var postData = clone_object(options);
- postData['src'] = src;
+ var postData = clone_object(opts.apiOptions);
postData['username'] = that.username;
postData['key'] = that.apikey;
- postData = querystring.stringify(postData);
+
+ var contentType;
+ if (opts.src) {
+ contentType = "application/x-www-form-urlencoded";
+ postData['src'] = opts.src;
+ postData = querystring.stringify(postData);
+ } else {
+ contentType = "multipart/form-data; boundary=" + boundary;
+ postData = encodeMultipartPostData(postData, opts.fname, opts.data);
+ }
// http options
httpOptions = {
host: 'pdfcrowd.com',
port: 80,
method: 'POST',
- path: endpoint,
+ path: opts.endpoint,
headers: { 'content-length': postData.length,
- 'content-type': 'application/x-www-form-urlencoded',
+ 'content-type': contentType,
'user-agent': 'node-pdfcrowd client'},
};
+ var callbacks = opts.callbacks;
var req = http.request(httpOptions, function(res) {
if (res.statusCode < 300) {
res.on('end', function() {
@@ -179,7 +241,7 @@ var convertInternal = function(that, src, endpoint, callbacks, options) {
requestQueue.requestDone();
});
- req.write(postData);
+ req.write(postData, 'binary');
req.end();
}

0 comments on commit f16aca5

Please sign in to comment.