Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: res.download with lower complexity #6302

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,31 +417,38 @@ res.sendFile = function sendFile(path, options, callback) {
* to set the attachment and filename.
*
* This method uses `res.sendFile()`.
*
* Examples:
*
* // Basic usage with path
* res.download('/path/to/file.csv');
*
* // With filename and callback
* res.download('/path/to/file.csv', 'report.csv', (err) => { ... });
*
* // With options (no filename)
* res.download('/path/to/file.csv', { headers: { 'Cache-Control': 'no-cache' } });
*
* @param {String} path
* @param {String|Function|Object} [filename]
* @param {Object|Function} [options]
* @param {Function} [callback]
* @public
*/

res.download = function download (path, filename, options, callback) {
var done = callback;
var name = filename;
var opts = options || null

// support function as second or third arg
if (typeof filename === 'function') {
done = filename;
name = null;
opts = null
} else if (typeof options === 'function') {
done = options
opts = null
}
// identify the callback among the [filename, options, callback]
const done = [filename, options, callback].find(arg => typeof arg === 'function');

// support optional filename, where options may be in it's place
if (typeof filename === 'object' &&
(typeof options === 'function' || options === undefined)) {
name = null
opts = filename
}
var name = null;
var opts = {};

// determine if filename is a object or string
if (typeof filename === 'string') name = filename;
else if (typeof filename === 'object') opts = filename;

// if options is a function, set it as callback
if (typeof options === 'object') opts = Object.assign({}, opts, options);

// set Content-Disposition when file is sent
var headers = {
Expand All @@ -450,23 +457,19 @@ res.download = function download (path, filename, options, callback) {

// merge user-provided headers
if (opts && opts.headers) {
var keys = Object.keys(opts.headers)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
Object.keys(opts.headers).forEach(key => {
if (key.toLowerCase() !== 'content-disposition') {
headers[key] = opts.headers[key]
headers[key] = opts.headers[key];
}
}
});
}

// merge user-provided options
opts = Object.create(opts)
opts.headers = headers

// Resolve the full path for sendFile
var fullPath = !opts.root
? resolve(path)
: path
var fullPath = !opts.root ? resolve(path) : path

// send file
return this.sendFile(fullPath, opts, done)
Expand Down Expand Up @@ -865,7 +868,7 @@ res.vary = function(field){
*
* - `cache` boolean hinting to the engine it should cache
* - `filename` filename of the view being rendered
*
*
* @public
*/

Expand Down