- Place both the files (app.js and package.json) in the same folder and run
npm install
in your terminal. node app.js
in your terminal- Then visit http://localhost:8000/
Created
February 28, 2012 19:12
-
-
Save madhums/1934446 to your computer and use it in GitHub Desktop.
image resize and upload to rackspace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure you replace USERNAME, API_KEY and YOUR_CONTAINER with your real rackspace credentials | |
var formidable = require('formidable'), | |
http = require('http'), | |
im = require('imagemagick'), | |
fs = require('fs'), | |
cloudfiles = require('cloudfiles'), | |
util = require('util'), | |
config = { | |
rs_cloudfiles: { | |
auth: { | |
username: "USERNAME", | |
apiKey: "API_KEY", | |
host: "lon.auth.api.rackspacecloud.com" | |
} | |
}, | |
rs_container: "YOUR_CONTAINER" | |
} | |
http.createServer(function(req, res) { | |
if (req.url == '/upload' && req.method.toLowerCase() == 'post') { | |
// parse a file upload | |
var form = new formidable.IncomingForm(); | |
form.parse(req, function(err, fields, files) { | |
//res.writeHead(200, {'content-type': 'text/plain'}); | |
//res.write('received upload:nn'); | |
//res.end(util.inspect({fields: fields, files: files})); | |
im.resize({ | |
srcData: fs.readFileSync(files.images.path, 'binary'), | |
width: 256 | |
}, function(err, stdout, stderr){ | |
if (err) throw err | |
//res.writeHead(200, {"Content-Type": "image/jpeg"}); | |
//res.write(stdout, "binary"); | |
//res.end(); | |
var client = cloudfiles.createClient(config.rs_cloudfiles); | |
var cdnUri = '' | |
client.setAuth(function() { | |
client.getContainer(config.rs_container, true, addFileToContainer); | |
}); | |
function addFileToContainer(err, container) { | |
var options = { | |
remote: files.images.name | |
, headers: { | |
'content-type': files.images.type | |
} | |
, stream: stdout } | |
, cdnUri = container.cdnUri | |
client.addFile(config.rs_container, options, onUploadToContainer); | |
} | |
function onUploadToContainer(err, uploaded) { | |
if (err) throw err; | |
if (uploaded) res.write(cdnUri+files.images.name) | |
} | |
}); | |
}); | |
return; | |
} | |
// show a file upload form | |
res.writeHead(200, {'content-type': 'text/html'}); | |
res.end( | |
'<form action="/upload" enctype="multipart/form-data" method="post">'+ | |
'<input type="text" name="title"><br>'+ | |
'<input type="file" name="images" multiple="multiple"><br>'+ | |
'<input type="submit" value="Upload">'+ | |
'</form>' | |
); | |
}).listen(8000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test123" | |
, "description": "image resize and upload to rackspace" | |
, "version": "0.0.1" | |
, "private": true | |
, "author": "Madhusudhan M S (http://github.com/madhums)" | |
, "engines": { | |
"node": ">= 0.6.5" | |
} | |
, "dependencies": { | |
"cloudfiles" : "0.3.3" | |
, "imagemagick" : "0.1.2" | |
, "formidable" : "1.0.9" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment