Permalink
Browse files

Add support for "posting" files

  • Loading branch information...
1 parent 3c45927 commit 0cf3930954ffc1b84c668764cb96b78c2351714e @xmilliard committed Sep 1, 2011
Showing with 24 additions and 8 deletions.
  1. +24 −8 lib/oauth.js
View
@@ -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, fromFile ) {
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(!fromFile) {
+ 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, fromFile);
}
else {
callback({ statusCode: response.statusCode, data: data }, data, response);
@@ -388,11 +392,18 @@ 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(fromFile) {
+ var rs = fs.createReadStream(post_body, { flags:'r' });
+ rs.pipe(request);
+ }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 +465,7 @@ 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._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 +476,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 +488,11 @@ 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) {
+ return this._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.

0 comments on commit 0cf3930

Please sign in to comment.