nodejs-file-downloader
Advanced tools
Comparing version 4.3.0 to 4.4.0
@@ -78,2 +78,3 @@ const fs = require('fs'); | ||
* @param {function} [config.onResponse = undefined] | ||
* @param {function} [config.onBeforeSave = undefined] | ||
* @param {function} [config.onProgress = undefined] | ||
@@ -104,2 +105,3 @@ * @param {function} [config.shouldStop = undefined] | ||
onResponse: undefined, | ||
onBeforeSave: undefined, | ||
onError: undefined, | ||
@@ -195,4 +197,12 @@ onProgress: undefined | ||
// debugger | ||
const finalName = await this._getFinalFileName(response.headers); | ||
let finalName = await this._getFinalFileName(response.headers); | ||
if(this.config.onBeforeSave){ | ||
// debugger | ||
const clientOverideName = await this.config.onBeforeSave(finalName) | ||
if(clientOverideName && typeof clientOverideName === 'string'){ | ||
finalName = clientOverideName; | ||
} | ||
} | ||
const finalPath = `${this.config.directory}/${finalName}`; | ||
@@ -199,0 +209,0 @@ |
@@ -76,3 +76,3 @@ | ||
nock(`http://www.${host}.com`) | ||
.get('/contentType') | ||
.get('/contentType1') | ||
.reply(200, (uri, requestBody) => { | ||
@@ -87,3 +87,3 @@ | ||
const downloader = new Downloader({ | ||
url: `http://www.${host}.com/contentType`, | ||
url: `http://www.${host}.com/contentType1`, | ||
directory: "./downloads", | ||
@@ -106,2 +106,35 @@ cloneFiles: false, | ||
// debugger | ||
await verifyFile('./downloads/contentType1.jpeg', 23642); | ||
// console.log(verify) | ||
}) | ||
it('Should get the deduced name', async () => { | ||
let deducedName; | ||
const host = randomHost() | ||
nock(`http://www.${host}.com`) | ||
.get('/contentType') | ||
.reply(200, (uri, requestBody) => { | ||
return fs.createReadStream(Path.join(__dirname, 'fixtures/Desert.jpg')) | ||
// fs.readFile(Path.join(__dirname, 'fixtures/Desert.jpg'), cb) // Error-first callback | ||
}, { | ||
'Content-Type': 'image/jpeg', | ||
'Content-Length': '23642' | ||
}) | ||
const downloader = new Downloader({ | ||
url: `http://www.${host}.com/contentType`, | ||
directory: "./downloads", | ||
cloneFiles: false, | ||
onBeforeSave:(name)=>{ | ||
deducedName = name; | ||
// return 'yoyoyoy' | ||
} | ||
}) | ||
// debugger; | ||
await downloader.download(); | ||
expect(deducedName).toBe('contentType.jpeg') | ||
// debugger | ||
await verifyFile('./downloads/contentType.jpeg', 23642); | ||
@@ -113,2 +146,35 @@ // console.log(verify) | ||
it('Should override the deduced name', async () => { | ||
let deducedName; | ||
const host = randomHost() | ||
nock(`http://www.${host}.com`) | ||
.get('/contentType') | ||
.reply(200, (uri, requestBody) => { | ||
return fs.createReadStream(Path.join(__dirname, 'fixtures/Desert.jpg')) | ||
// fs.readFile(Path.join(__dirname, 'fixtures/Desert.jpg'), cb) // Error-first callback | ||
}, { | ||
'Content-Type': 'image/jpeg', | ||
'Content-Length': '23642' | ||
}) | ||
const downloader = new Downloader({ | ||
url: `http://www.${host}.com/contentType`, | ||
directory: "./downloads", | ||
cloneFiles: false, | ||
onBeforeSave:(name)=>{ | ||
deducedName = name; | ||
return 'override.jpg' | ||
} | ||
}) | ||
// debugger; | ||
await downloader.download(); | ||
expect(deducedName).toBe('contentType.jpeg') | ||
// debugger | ||
await verifyFile('./downloads/override.jpg', 23642); | ||
// console.log(verify) | ||
}) | ||
it('Should get NaN in onProgress', async () => { | ||
@@ -115,0 +181,0 @@ |
{ | ||
"name": "nodejs-file-downloader", | ||
"version": "4.3.0", | ||
"version": "4.4.0", | ||
"description": "A file downloader for NodeJs", | ||
@@ -5,0 +5,0 @@ "main": "Downloader.js", |
@@ -14,2 +14,3 @@ nodejs-file-downloader is a simple utility for downloading files. It hides the complexity of dealing with streams, redirects, paths and duplicate file names. Can automatically repeat failed downloads. | ||
* [Get the progress of a download](#get-the-progress-of-a-download) | ||
* [Get the deduced file name and override it](#get-the-deduced-file-name-and-override-it) | ||
* [Custom file name](#custom-file-name) | ||
@@ -85,2 +86,24 @@ * [Overwrite existing files](#overwrite-existing-files) | ||
#### Get the deduced file name and override it | ||
If you haven't supplied a "fileName" config property, the program will try its best to choose the most appropriate name for the file(from the URL, headers, etc). In case you wish to know the name that was chosen, before the file is actually saved, use the onBeforeSave hook, which is called with the deduced name. | ||
If you're "unhappy" with the name, it can be overridden by returning a new string. Returning anything else(including undefined/void), will | ||
let the program know that it can keep the name. | ||
```javascript | ||
const downloader = new Downloader({ | ||
url: 'http://212.183.159.230/200MB.zip', | ||
directory: "./downloads/2020/May", | ||
onBeforeSave:(deducedName)=>{ | ||
console.log(`The file name is: ${deducedName}`) | ||
//If you return a string here, it will be used as the name(that includes the extension!). | ||
} | ||
}) | ||
``` | ||
| ||
#### Custom file name | ||
@@ -87,0 +110,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
167802
1545
260