Skip to content

Instantly share code, notes, and snippets.

@2fours
Last active December 17, 2015 21:19
Show Gist options
  • Save 2fours/5673398 to your computer and use it in GitHub Desktop.
Save 2fours/5673398 to your computer and use it in GitHub Desktop.
piping form part stream directly to pkgcloud rackspace upload hangs
request = require 'request'
fs = require("fs")
baseUri = "http://localhost:8000"
r = request.post baseUri + "/images", (err, res, body) ->
if err
console.log err
form = r.form()
form.append "image", fs.createReadStream("testImage")
http = require('http')
express = require("express")
formidable = require 'formidable'
pkgcloud = require 'pkgcloud'
stream = require 'readable-stream'
rackspaceApiKey = ""
rackspaceImageContainer = ""
rackspace = pkgcloud.storage.createClient {provider: 'rackspace', username: '', apiKey: rackspaceApiKey}
app = express()
app.configure ->
app.use app.router
app.use (err, req, res, next) ->
console.log 'error', "middlewareError #{err}"
res.send err.status or 500
server = http.createServer app
server.listen(8000)
app.post "/images", (req, res, next) ->
form = new formidable.IncomingForm()
form.onPart = (part) ->
return form.handlePart part unless part.filename?
path = "somepath"
console.log "received part: #{part.filename}, uploading to rackspace at: #{path}"
part.pipe rackspace.upload {container: rackspaceImageContainer, remote: path}, (err) ->
#todo send messageerror on socket
return console.log err if err?
console.log 'uploaded completed'
res.send 204
console.log 'stream piped'
form.on 'error', (err) ->
next new Error err
form.on 'end', ->
console.log 'form end'
form.parse req
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment