From 66448fba999162d843b5defccd0d785799cbebdf Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Sat, 1 Oct 2022 13:04:10 +0200 Subject: [PATCH] feat: first commit --- README.md | 41 ++++++++++++++++++++++++++++++++++ index.d.ts | 1 + index.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 22 ++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 README.md create mode 100644 index.d.ts create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..b1e700b --- /dev/null +++ b/README.md @@ -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 diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..8357925 --- /dev/null +++ b/index.d.ts @@ -0,0 +1 @@ +declare module 'dl-file-new'; \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..96c1f83 --- /dev/null +++ b/index.js @@ -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) + + }) + +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c347dca --- /dev/null +++ b/package.json @@ -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" + } +}