Skip to content

Instantly share code, notes, and snippets.

@cbosco
Created September 29, 2012 21:11
Show Gist options
  • Save cbosco/3805198 to your computer and use it in GitHub Desktop.
Save cbosco/3805198 to your computer and use it in GitHub Desktop.
Copies a binary remote file (like a jpeg image) to a temporary local file for use with node-canvas, etc
fs = require 'fs'
http = require 'http'
temp = require 'temp' # npm install
getTemporaryFilePathFromURL = ( url, callback ) ->
temp.open 'temp-image', (err, info) ->
stream = fs.createWriteStream info.path
http.get url, ( res ) ->
res.on 'data', (chunk) ->
console.log 'got chunk of size ' + chunk.length
stream.write chunk
res.on 'end', () ->
console.log 'done'
stream.end()
fs.close info.fd, (err) ->
callback info.path
module.exports =
getTemporaryFilePathFromURL : getTemporaryFilePathFromURL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment