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
Provenance
- Commit
a24fd0c- Tree
bbdd340- Author
Aaron Clover <aaron.j.clover@gmail.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
95fb5cc
2 files changed+110 added−4 removed
modified:lib/response.js+30 added-4 removed
513 unmodified lines514 * 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 */527 523-res.download = function download(path, filename, callback) {528+res.download = function download (path, filename, options, callback) {529 var done = callback;530 var name = filename;531+ var opts = options || null532 527- // 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 }542 543 // set Content-Disposition when file is sent544 var headers = {545 'Content-Disposition': contentDisposition(name || path)546 };547 548+ // 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+ }558+559+ // merge user-provided options560+ opts = Object.create(opts)561+ opts.headers = headers562+563 // Resolve the full path for sendFile564 var fullPath = resolve(path);565 566+ // send file541- return this.sendFile(fullPath, { headers: headers }, done);567+ return this.sendFile(fullPath, opts, done)568 };569 570 /**571 * Set _Content-Type_ response header with `type` through `mime.lookup()`540 unmodified linesmodified:test/res.download.js+80 added
69 unmodified lines70717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615735 unmodified lines69 unmodified lines .expect(200, cb); }) })
describe('.download(path, filename, options, fn)', function () { it('should invoke the callback', function (done) { var app = express() var cb = after(2, done) var options = {}
app.use(function (req, res) { res.download('test/fixtures/user.html', 'document', options, done) })
request(app) .get('/') .expect(200) .expect('Content-Type', 'text/html; charset=UTF-8') .expect('Content-Disposition', 'attachment; filename="document"') .end(cb) })
it('should allow options to res.sendFile()', function (done) { var app = express()
app.use(function (req, res) { res.download('test/fixtures/.name', 'document', { dotfiles: 'allow', maxAge: '4h' }) })
request(app) .get('/') .expect(200) .expect('Content-Disposition', 'attachment; filename="document"') .expect('Cache-Control', 'public, max-age=14400') .expect('tobi') .end(done) })
describe('when options.headers contains Content-Disposition', function () { it('should should be ignored', function (done) { var app = express()
app.use(function (req, res) { res.download('test/fixtures/user.html', 'document', { headers: { 'Content-Type': 'text/x-custom', 'Content-Disposition': 'inline' } }) })
request(app) .get('/') .expect(200) .expect('Content-Type', 'text/x-custom') .expect('Content-Disposition', 'attachment; filename="document"') .end(done) })
it('should should be ignored case-insensitively', function (done) { var app = express()
app.use(function (req, res) { res.download('test/fixtures/user.html', 'document', { headers: { 'content-type': 'text/x-custom', 'content-disposition': 'inline' } }) })
request(app) .get('/') .expect(200) .expect('Content-Type', 'text/x-custom') .expect('Content-Disposition', 'attachment; filename="document"') .end(done) }) }) })
describe('on failure', function(){ it('should invoke the callback', function(done){ var app = express();
35 unmodified lines