Created
May 30, 2013 00:01
-
-
Save 2fours/5674810 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var app, | |
logger, | |
client, | |
server, | |
rackspaceImageContainer, | |
config = require('./config.json'), | |
express = require('express'), | |
formidable = require('formidable'), | |
http = require('http'), | |
log = require('./logging'), | |
pkgcloud = require('pkgcloud'); | |
rc = require('redis').createClient(); | |
rackspaceImageContainer = 'test'; // Make sure to set this to a valid container | |
logger = log.getLogger('debug'); | |
client = pkgcloud.storage.createClient({ | |
provider: 'rackspace', | |
username: config.username, | |
apiKey: config.apiKey | |
}); | |
client.on('log::*', log.logFunction); | |
app = express(); | |
app.configure(function () { | |
app.use(app.router); | |
return app.use(function (err, req, res, next) { | |
logger.error('middlewareError' ,err); | |
return res.send(err.status || 500); | |
}); | |
}); | |
server = http.createServer(app); | |
server.listen(8000); | |
app.post('/images', function (req, res, next) { | |
var form; | |
form = new formidable.IncomingForm(); | |
form.onPart = function (part) { | |
var path; | |
if (part.filename == null) { | |
return form.handlePart(part); | |
} | |
path = 'testImage.jpg'; | |
rc.incr("incr", function (err, inc) { | |
logger.verbose('received part: ' + part.filename + ', uploading to rackspace at: ' + path); | |
part.pipe(client.upload({ | |
container: rackspaceImageContainer, | |
remote: path | |
}, function (err) { | |
if (err != null) { | |
return logger.error(err); | |
} | |
logger.verbose('uploaded completed'); | |
return res.send(204); | |
})); | |
return logger.verbose('stream piped'); | |
}); | |
}; | |
form.on('error', function (err) { | |
return next(new Error(err)); | |
}); | |
form.on('end', function () { | |
return logger.verbose('form end'); | |
}); | |
return form.parse(req); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment