mirror of
https://github.com/SrIzan10/download-file-new.git
synced 2026-06-06 00:56:50 +00:00
feat: first commit
This commit is contained in:
41
README.md
Normal file
41
README.md
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# download-file-new
|
||||||
|
|
||||||
|
This creates a `d.ts` file so you can use it in your typescript project. Used by [sern CLI](https://github.com/sern-handler/cli.git).
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npm install download-file-new
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```js
|
||||||
|
var download = require('dl-file-new')
|
||||||
|
|
||||||
|
var url = "http://i.imgur.com/G9bDaPH.jpg"
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
directory: "./images/cats/",
|
||||||
|
filename: "cat.gif"
|
||||||
|
}
|
||||||
|
|
||||||
|
download(url, options, function(err){
|
||||||
|
if (err) throw err
|
||||||
|
console.log("meow")
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### download(url, [options], callback(err))
|
||||||
|
|
||||||
|
- __url__ string of the file URL to download
|
||||||
|
|
||||||
|
- __options__ object with options
|
||||||
|
|
||||||
|
- __directory__ string with path to directory where to save files (default: current working directory)
|
||||||
|
- __filename__ string for the name of the file to be saved as (default: filename in the url)
|
||||||
|
- __timeout__ integer of how long in ms to wait while downloading (default: 20000)
|
||||||
|
|
||||||
|
- __callback__ function to run after
|
||||||
1
index.d.ts
vendored
Normal file
1
index.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
declare module 'dl-file-new';
|
||||||
63
index.js
Normal file
63
index.js
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
var fs = require('fs')
|
||||||
|
var url = require('url')
|
||||||
|
var http = require('http')
|
||||||
|
var https = require('https')
|
||||||
|
var mkdirp = require('mkdirp')
|
||||||
|
|
||||||
|
module.exports = function download(file, options, callback) {
|
||||||
|
if (!file) throw("Need a file url to download")
|
||||||
|
|
||||||
|
if (!callback && typeof options === 'function') {
|
||||||
|
callback = options
|
||||||
|
}
|
||||||
|
|
||||||
|
options = typeof options === 'object' ? options : {}
|
||||||
|
options.timeout = options.timeout || 20000
|
||||||
|
options.directory = options.directory ? options.directory : '.'
|
||||||
|
|
||||||
|
var uri = file.split('/')
|
||||||
|
options.filename = options.filename || uri[uri.length - 1]
|
||||||
|
|
||||||
|
var path = options.directory + "/" + options.filename
|
||||||
|
|
||||||
|
if (url.parse(file).protocol === null) {
|
||||||
|
file = 'http://' + file
|
||||||
|
req = http
|
||||||
|
} else if (url.parse(file).protocol === 'https:') {
|
||||||
|
req = https
|
||||||
|
} else {
|
||||||
|
req = http
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = req.get(file, function(response) {
|
||||||
|
|
||||||
|
if (response.statusCode === 200) {
|
||||||
|
|
||||||
|
mkdirp(options.directory, function(err) {
|
||||||
|
if (err) throw err
|
||||||
|
var file = fs.createWriteStream(path)
|
||||||
|
response.pipe(file)
|
||||||
|
})
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if (callback) callback(response.statusCode)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
response.on("end", function(){
|
||||||
|
if (callback) callback(false, path)
|
||||||
|
})
|
||||||
|
|
||||||
|
request.setTimeout(options.timeout, function () {
|
||||||
|
request.abort()
|
||||||
|
callback("Timeout")
|
||||||
|
})
|
||||||
|
|
||||||
|
}).on('error', function(e) {
|
||||||
|
|
||||||
|
if (callback) callback(e)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
22
package.json
Normal file
22
package.json
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"name": "dl-file-new",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Generic file download utility with ts support. (download-file fork)",
|
||||||
|
"main": "./index.js",
|
||||||
|
"author": "Sr Izan",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"mkdirp": "^0.5.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {},
|
||||||
|
"keywords": [
|
||||||
|
"download",
|
||||||
|
"file",
|
||||||
|
"url",
|
||||||
|
"https",
|
||||||
|
"fork"
|
||||||
|
],
|
||||||
|
"publishConfig": {
|
||||||
|
"registry": "https://registry.npmjs.org"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user