Skip to content
Myr

Add options to res.download

Before adoption: this commit came in with the history the project adopted, so no landing records it.

closes #3327 closes #3370

authored by Aaron Clover · committed by Douglas Christopher Wilson · parent 95fb5cc · Browse files · 2 files+110 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+110 added−4 removed

  • tests:+80 added−0 removed
  • other:+30 added−4 removed

2 files+110 added−4 removed

modified:lib/response.js+30 added-4 removed
513 unmodified lines
514 * and optional callback `callback(err)`. The callback is invoked515 * when the data transfer is complete, or when an error has516 * ocurred. Be sure to check `res.headersSent` if you plan to respond.517 *518 * This method uses `res.sendfile()`.518 * Optionally providing an `options` object to use with `res.sendFile()`.519 * This function will set the `Content-Disposition` header, overriding520 * any `Content-Disposition` header passed as header options in order521 * to set the attachment and filename.522 *523 * This method uses `res.sendFile()`.524 *525 * @public526 */527523res.download = function download(path, filename, callback) {528res.download = function download (path, filename, options, callback) {529 var done = callback;530 var name = filename;531 var opts = options || null532527 // support function as second arg533 // support function as second or third arg534 if (typeof filename === 'function') {535 done = filename;536 name = null;537 opts = null538 } else if (typeof options === 'function') {539 done = options540 opts = null541 }542543 // set Content-Disposition when file is sent544 var headers = {545 'Content-Disposition': contentDisposition(name || path)546 };547548 // merge user-provided headers549 if (opts && opts.headers) {550 var keys = Object.keys(opts.headers)551 for (var i = 0; i < keys.length; i++) {552 var key = keys[i]553 if (key.toLowerCase() !== 'content-disposition') {554 headers[key] = opts.headers[key]555 }556 }557 }558559 // merge user-provided options560 opts = Object.create(opts)561 opts.headers = headers562563 // Resolve the full path for sendFile564 var fullPath = resolve(path);565566 // send file541 return this.sendFile(fullPath, { headers: headers }, done);567 return this.sendFile(fullPath, opts, done)568};569570/**571 * Set _Content-Type_ response header with `type` through `mime.lookup()`
540 unmodified lines
modified:test/res.download.js+80 added