Skip to content

Instantly share code, notes, and snippets.

@beaucollins
Created June 10, 2011 06:57
Show Gist options
  • Save beaucollins/1018363 to your computer and use it in GitHub Desktop.
Save beaucollins/1018363 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var http = require('http');
var url = require('url');
var file = './hyperdrive.jpg';
var blogId = 10231312;
var username = 'redacted';
var password = 'redacted';
var endpoint = 'http://example.com/';
var destination = url.parse(endpoint);
if(file){
var read = fs.createReadStream(file, { flags: 'r', encoding: null, mode: 0666, bufferSize:1024 });
var local = http.createClient((destination.port || 80), destination.hostname);
var request = local.request('POST', destination.pathname);
var front = '<?xml version="1.0" ?><methodCall><methodName>wp.uploadFile</methodName>';
front += '<params>';
front += '<param><int>' + blogId + '</int></param>';
front += '<param><string>' + username + '</string></param>';
front += '<param><string>' + password + '</string></param>';
front += '<param><base64>';
var back = '</base64></param></params></methodCall>';
request.write(front);
read.setEncoding('base64');
request.on('drain', function(){
read.resume();
});
read.on('data', function(data){
if(!write.write(data)){
read.pause();
}
});
read.on('error', function(exception){
future.result = exception;
future.returnValue = false;
});
read.on('end', function(){
request.write(back);
request.end();
});
request.on('response', function(response){
response.on('end', function(){
var result = { status: response.statusCode };
console.log(result);
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment