Last active
December 16, 2015 11:28
-
-
Save 2fours/5427194 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
// Generated by CoffeeScript 1.4.0 | |
(function() { | |
var app, express, formidable, http, pkgcloud, rackspace, rackspaceApiKey, rackspaceImageContainer, server, stream; | |
http = require('http'); | |
express = require("express"); | |
formidable = require('formidable'); | |
pkgcloud = require('pkgcloud'); | |
stream = require('readable-stream'); | |
rackspaceImageContainer = "container"; | |
rackspace = pkgcloud.storage.createClient({ | |
provider: 'rackspace', | |
username: 'username', | |
apiKey: 'key' | |
}); | |
app = express(); | |
app.configure(function() { | |
app.use(app.router); | |
return app.use(function(err, req, res, next) { | |
console.log('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 outStream, path; | |
if (part.filename == null) { | |
return form.handlePart(part); | |
} | |
outStream = new stream.PassThrough(); | |
part.on('data', function(buffer) { | |
form.pause(); | |
console.log('received part data'); | |
return outStream.write(buffer, function() { | |
return form.resume(); | |
}); | |
}); | |
part.on('end', function() { | |
form.pause(); | |
console.log('received part end'); | |
return outStream.end(function() { | |
return form.resume(); | |
}); | |
}); | |
path = "somepath"; | |
console.log("received part: " + part.filename + ", uploading to rackspace at: " + path); | |
outStream.pipe(rackspace.upload({ | |
container: rackspaceImageContainer, | |
remote: path | |
}, function(err) { | |
if (err != null) { | |
return console.log(err); | |
} | |
console.log('uploaded completed'); | |
return res.send(204); | |
})); | |
return console.log('stream piped'); | |
}; | |
form.on('error', function(err) { | |
return next(new Error(err)); | |
}); | |
form.on('end', function() { | |
return console.log('form end'); | |
}); | |
return form.parse(req); | |
}); | |
}).call(this); |
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
// Generated by CoffeeScript 1.4.0 | |
(function() { | |
var baseUri, fs, http, should; | |
should = require("should"); | |
http = require("request"); | |
fs = require("fs"); | |
baseUri = "http://localhost:8000"; | |
describe("uploadtest", function() { | |
return it('upload image', function(done) { | |
var form, r; | |
r = http.post(baseUri + "/images", function(err, res, body) { | |
if (err) { | |
return done(err); | |
} else { | |
res.statusCode.should.equal(204); | |
return done(); | |
} | |
}); | |
form = r.form(); | |
return form.append("image", fs.createReadStream("testImage")); | |
}); | |
}); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment