Skip to content
Myr

build `res.download()` on top of `res.sendfile()`

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

like it should have always been

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 7a476fc · Browse files · 1 file+24 added−41 removed
This commit

State

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

Diff stats

1 file+24 added−41 removed

  • other:+24 added−41 removed

1 file+24 added−41 removed

modified:lib/response.js+24 added-41 removed
199 unmodified lines
200 send(this.req, this, next, options);201};202203/**204 * Transfer the file at the given `path` as an attachment.205 *206 * Optionally providing an alternate attachment `filename`,207 * and optional callback `fn(err)`. The callback is invoked208 * when the data transfer is complete, or when an error has209 * ocurred. Be sure to check `res.sendHeader` if you plan to respond.210 *211 * @param {String} path212 * @param {String|Function} filename or fn213 * @param {Function} fn214 * @api public215 */216217res.download = function(path, filename, fn){218 // support function as second arg219 if ('function' == typeof filename) {220 fn = filename;221 filename = null;222 }223224 return this.attachment(filename || path).sendfile(path, fn);225};226227/**228 * Set _Content-Type_ response header passed through `mime.lookup()`.229 *230 * Examples:231 *
31 unmodified lines
263 return this;264};265266/**243 * Transfer the file at the given `path`, with optional 244 * `filename` as an attachment and optional callback `fn(err)`,245 * and optional `fn2(err)` which is invoked when an error has246 * occurred after header has been sent.247 *248 * @param {String} path249 * @param {String|Function} filename or fn250 * @param {Function} fn251 * @param {Function} fn2252 * @api public253 */254255res.download = function(path, filename, fn, fn2){256 var self = this;257258 // support callback as second arg259 if ('function' == typeof filename) {260 fn2 = fn;261 fn = filename;262 filename = null;263 }264265 // transfer the file266 this.attachment(filename || path).sendfile(path, function(err){267 var sentHeader = self._header;268 if (err) {269 if (!sentHeader) self.removeHeader('Content-Disposition');270 if (sentHeader) {271 fn2 && fn2(err);272 } else if (fn) {273 fn(err);274 } else {275 self.req.next(err);276 }277 } else if (fn) {278 fn();279 }280 });281};282283/**267 * Set header `field` to `val`.268 *269 * @param {String} field270 * @param {String} val
176 unmodified lines