Last active
December 23, 2015 03:39
-
-
Save jakobmattsson/6575341 to your computer and use it in GitHub Desktop.
getFile
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' | |
urlLib = require 'url' | |
path = require 'path' | |
mkdirp = require 'mkdirp' | |
downloader = require 'downloader' | |
exports.get = ({ url, dir, onDownload }, callback) -> | |
onDownload ?= (callback) -> callback() | |
{pathname} = urlLib.parse(url) | |
filename = path.basename(pathname) | |
fullpath = path.resolve(__dirname, dir, filename) | |
dirname = path.dirname(fullpath) | |
mkdirp dirname, (err) -> | |
return callback(err) if err? | |
fs.exists fullpath, (exists) -> | |
if exists | |
callback(null, fullpath) | |
else | |
onDownload -> | |
downloader.on 'done', -> | |
callback(null, fullpath) | |
downloader.on 'error', (msg) -> | |
callback(msg) | |
downloader.download(url, dirname + '/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment