Skip to content

Commit

Permalink
Fix leading dots being removed (#59)
Browse files Browse the repository at this point in the history
* fix leading dots being removed

* only modify auto-generated filenames + added test
  • Loading branch information
Chaphasilor authored Aug 17, 2021
1 parent 9fbb27d commit bed68d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,12 +649,12 @@ export class DownloaderHelper extends EventEmitter {
fileName = `${URL.parse(this.requestURL).hostname}.html`;
}
}

return (
(this.__opts.fileName)
? this.__getFileNameFromOpts(fileName, response)
: fileName
).split('.').filter(Boolean).join('.'); // remove any potential trailing '.' (just to be sure)
: fileName.replace(/\.*$/, '') // remove any potential trailing '.' (just to be sure)
)
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,29 @@ describe('DownloaderHelper', function () {
});
expect(result).to.be.equal(newFileName);
});

it("should keep leading dots but remove trailing dots for auto-generated file names", function () {
const newFileName = '.gitignore.';
const expectedFileName = '.gitignore';
const dl = new DownloaderHelper('https://google.com/', __dirname, {
// fileName: { name: newFileName, ext: true }
});
const result = dl.__getFileNameFromHeaders({
'content-disposition': 'Content-Disposition: attachment; filename="' + newFileName + '"',
});
expect(result).to.be.equal(expectedFileName);
});

it("should not modify the filename when providing a callback", function () {
const newFileName = '.gitignore.';
const expectedFileName = newFileName
const dl = new DownloaderHelper('https://google.com/', __dirname, {
fileName: () => '.gitignore.'
});
const result = dl.__getFileNameFromHeaders({
'content-disposition': 'Content-Disposition: attachment; filename="' + newFileName + '"',
});
expect(result).to.be.equal(expectedFileName);
});
});
});

0 comments on commit bed68d5

Please sign in to comment.