Created
          September 29, 2012 21:11 
        
      - 
      
- 
        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
  
        
  
    
      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
    
  
  
    
  | 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