download
Advanced tools
Comparing version 0.1.3 to 0.1.4
'use strict'; | ||
var decompress = require('decompress'); | ||
var forEach = require('async-foreach').forEach; | ||
var fs = require('fs'); | ||
@@ -20,42 +21,48 @@ var mkdir = require('mkdirp'); | ||
* @param {Object} opts | ||
* @api public | ||
*/ | ||
module.exports = function (url, dest, opts) { | ||
opts = opts || {}; | ||
opts.url = url; | ||
url = Array.isArray(url) ? url : [url]; | ||
var req = request.get(opts) | ||
.on('response', function (res) { | ||
stream.emit('response', res); | ||
}) | ||
.on('data', function (data) { | ||
stream.emit('data', data); | ||
}) | ||
.on('error', function (err) { | ||
stream.emit('error', err); | ||
}); | ||
forEach(url, function (url) { | ||
opts = opts || {}; | ||
opts.url = url; | ||
opts.dest = path.join(dest, path.basename(url)); | ||
req.on('response', function (res) { | ||
var mime = res.headers['content-type']; | ||
var status = res.statusCode; | ||
var end; | ||
var req = request.get(opts) | ||
.on('response', function (res) { | ||
stream.emit('response', res); | ||
}) | ||
.on('data', function (data) { | ||
stream.emit('data', data); | ||
}) | ||
.on('error', function (err) { | ||
stream.emit('error', err); | ||
}); | ||
if (status < 200 || status >= 300) { | ||
return; | ||
} | ||
req.on('response', function (res) { | ||
var mime = res.headers['content-type']; | ||
var status = res.statusCode; | ||
var end; | ||
if (opts.extract && decompress.canExtract(url, mime)) { | ||
end = decompress.extract({ ext: mime, path: dest }); | ||
} else { | ||
if (!fs.existsSync(path.dirname(dest))) { | ||
mkdir.sync(path.dirname(dest)); | ||
if (status < 200 || status >= 300) { | ||
return; | ||
} | ||
end = fs.createWriteStream(dest); | ||
} | ||
if (opts.extract && decompress.canExtract(url, mime)) { | ||
end = decompress.extract({ ext: mime, path: dest }); | ||
} else { | ||
if (!fs.existsSync(dest)) { | ||
mkdir.sync(dest); | ||
} | ||
req.pipe(end); | ||
end = fs.createWriteStream(opts.dest); | ||
} | ||
end.on('close', function () { | ||
stream.emit('close'); | ||
req.pipe(end); | ||
end.on('close', function () { | ||
stream.emit('close'); | ||
}); | ||
}); | ||
@@ -62,0 +69,0 @@ }); |
{ | ||
"name": "download", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Download and extract files effortlessly", | ||
@@ -31,3 +31,4 @@ "keywords": [ | ||
"request": "~2.25.0", | ||
"through2": "~0.1.0" | ||
"through2": "~0.1.0", | ||
"async-foreach": "~0.1.3" | ||
}, | ||
@@ -34,0 +35,0 @@ "devDependencies": { |
@@ -21,3 +21,7 @@ # download [![Build Status](https://secure.travis-ci.org/kevva/download.png?branch=master)](http://travis-ci.org/kevva/download) | ||
// download and save `foo.jpg` into `bar/foo.jpg` | ||
download('foo.jpg', 'bar/foo.jpg'); | ||
download('foo.jpg', 'bar'); | ||
// download and save an array of files in `bar/` | ||
var files = ['foo.jpg', 'bar.jpg', 'cat.jpg']; | ||
download(files, 'bar'); | ||
``` | ||
@@ -29,3 +33,3 @@ | ||
Download a file to a given destination. | ||
Download a file or an array of files to a given destination. | ||
@@ -32,0 +36,0 @@ ## Options |
3822
59
43
5
+ Addedasync-foreach@~0.1.3
+ Addedasync-foreach@0.1.3(transitive)