Skip to content

Allows posting content from file #66

Open
wants to merge 6 commits into from
View
69 lib/oauth.js
@@ -3,6 +3,7 @@ var crypto= require('crypto'),
http= require('http'),
https= require('https'),
URL= require('url'),
+ fs = require('fs'),
querystring= require('querystring'),
OAuthUtils= require('./_utils');
@@ -290,7 +291,8 @@ exports.OAuth.prototype._prepareParameters= function( oauth_token, oauth_token_s
return orderedParameters;
}
-exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) {
+
+exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback, useFile ) {
var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, extra_params);
if( !post_content_type ) {
@@ -328,7 +330,9 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
post_body= querystring.stringify(extra_params);
}
- headers["Content-length"]= post_body ? Buffer.byteLength(post_body) : 0;
+ if(!useFile) {
+ headers["Content-length"]= post_body ? Buffer.byteLength(post_body) : 0;
+ }
headers["Content-Type"]= post_content_type;
var path;
@@ -360,7 +364,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
} else {
// Follow 301 or 302 redirects with Location HTTP header
if((response.statusCode == 301 || response.statusCode == 302) && response.headers && response.headers.location) {
- self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback);
+ self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback, useFile);
}
else {
callback({ statusCode: response.statusCode, data: data }, data, response);
@@ -369,12 +373,27 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
}
}
+
+ var targetFile;
request.on('response', function (response) {
- response.setEncoding('utf8');
+ if(method == "GET" && response.statusCode == 200 && useFile) {
+ targetFile = fs.createWriteStream(post_body);
+ }
+ else {
+ response.setEncoding('utf8');
+ }
response.on('data', function (chunk) {
- data+=chunk;
+ if(targetFile) {
+ targetFile.write(chunk);
+ }
+ else {
+ data+=chunk;
+ }
});
response.on('end', function () {
+ if(targetFile) {
+ targetFile.destroySoon();
+ }
passBackControl( response );
});
response.on('close', function () {
@@ -388,11 +407,26 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
callbackCalled= true;
callback( err )
});
-
if( (method == "POST" || method =="PUT") && post_body != null && post_body != "" ) {
- request.write(post_body);
+ if(useFile) {
+ var rs = fs.createReadStream(post_body);
+ // pipe hang on large file
+ // using data event cure the problem
+ //rs.pipe(request);
+ rs.on('data', function(chunk) {
+ request.write(chunk);
+ });
+ rs.on('end', function() {
+ request.end();
+ });
+ }else {
+ request.write(post_body);
+ request.end();
+ }
+ } else {
+ request.end();
}
- request.end();
+
}
else {
if( (method == "POST" || method =="PUT") && post_body != null && post_body != "" ) {
@@ -454,7 +488,13 @@ exports.OAuth.prototype.get= function(url, oauth_token, oauth_token_secret, call
return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, "", null, callback );
}
-exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback) {
+exports.OAuth.prototype.getToFile= function(url, oauth_token, oauth_token_secret, fileName, callback) {
+ return this._performSecureRequest( oauth_token, oauth_token_secret, "GET", url, null, fileName, null, callback, true );
+}
+
+
+
+exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_token_secret, post_body, post_content_type, callback, fromFile) {
var extra_params= null;
if( typeof post_content_type == "function" ) {
callback= post_content_type;
@@ -465,7 +505,7 @@ exports.OAuth.prototype._putOrPost= function(method, url, oauth_token, oauth_tok
extra_params= post_body;
post_body= null;
}
- return this._performSecureRequest( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback );
+ return this._performSecureRequest( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback, fromFile );
}
@@ -477,6 +517,15 @@ exports.OAuth.prototype.post= function(url, oauth_token, oauth_token_secret, pos
return this._putOrPost("POST", url, oauth_token, oauth_token_secret, post_body, post_content_type, callback);
}
+exports.OAuth.prototype.postFromFile= function(url, oauth_token, oauth_token_secret, fileName, post_content_type, callback) {
+ var self = this;
+ fs.stat(fileName, function(err, stats) {
+ self._headers["Content-length"]= stats.size;
+ return self._putOrPost("POST", url, oauth_token, oauth_token_secret, fileName, post_content_type, callback, true);
+ });
+}
+
+
/**
* Gets a request token from the OAuth provider and passes that information back
* to the calling code.
Something went wrong with that request. Please try again.