Request#download() now accepts second filename arg
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 8574edd · Browse files · 3 files·+75 added−157 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+75 added−157 removed
- tests:+64 added−95 removed
- other:+11 added−62 removed
Provenance
- Commit
a2cedbd- Tree
28bc2c9- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
8574edd
3 files changed+75 added−157 removed
modified:lib/express/plugins/static.js+6 added-57 removed
7 unmodified lines8910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364651213141539 unmodified lines5556575811359606162636465666768120121697071727374757 unmodified linesvar Request = require('express/request').Request, path = require('path'), fs = require('fs') // --- File
exports.File = new Class({ /** * Initialize with file _path_. * * @param {string} path * @api public */ constructor: function(path) { this.path = path if (path.indexOf('..') !== -1) Error.raise('InvalidPathError', "`" + path + "' is not a valid path") }, /** * Transfer static file to the given _request_. * * @param {Request} request * @api public */ sendTo: function(request) { // function sendFromDisc() { // path.exists(file, function(exists){ // if (!exists) return request.notFound() // fs.stat(file, function(err, stats){ // if (err) throw err // if (!stats.isFile()) return request.notFound() // fs.readFile(file, 'binary', function(err, content){ // if (err) throw err // request.contentType(file) // if (set('cache static files')) // request.cache.set('static:' + file, { type: file, content: content }) // request.halt(200, content, 'binary') // }) // }) // }) // } // if (set('cache static files')) // request.cache.get('static:' + file, function(cache){ // if (cache) // request.contentType(cache.type), // request.halt(200, cache.content, 'binary') // else // sendFromDisc() // }) // else // sendFromDisc() }})
// --- Static
exports.Static = Plugin.extend({ extend: {39 unmodified lines }, /** * Transfer static _file_ as an attachment. * The basename of _file_ is used as the attachment filename. * * The basename of _file_ is used as the attachment _filename_ when * not explicitly passed. * * @param {string} file * @param {string} filename * @return {Request} * @api public */
download: function(file) { return this.attachment(path.basename(file)).sendfile(file) download: function(file, filename) { return this.attachment(filename || path.basename(file)).sendfile(file) } }) } }})modified:lib/express/request.js+5 added-5 removed
307 unmodified lines308 },309 310 /**311 * Set Content-Disposition header to 'attachment',312- * with optional file _path_.312+ * with optional file _filename_.313 *314- * @param {string} path314+ * @param {string} filename315 * @return {Request}316 * @api public317 */318 319- attachment: function(path) {320- this.header('content-disposition', path321- ? 'attachment; filename="' + path + '"'319+ attachment: function(filename) {320+ this.header('content-disposition', filename321+ ? 'attachment; filename="' + filename + '"'322 : 'attachment')323 return this324 },325 65 unmodified linesmodified:spec/spec.plugins.static.js+64 added-95 removed
123345567910111213141516171819202122891024252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
describe 'Express' describe 'StaticFile' describe 'Static' before StaticFile = require('express/plugins/static').File use(require('express/plugins/static').Static, { path: 'spec/fixtures' }) end describe '#constructor' it 'should accept and assign #path' (new StaticFile('/foo/bar')).path.should.eql '/foo/bar' end it 'should throw an InvalidPathError when .. is found' // TODO: use throw_error when fixed... try { new StaticFile('/../foobar') } catch (e) { e.name.should.eql 'InvalidPathError' e.message.should.eql "`/../foobar' is not a valid path" } end end // TODO: spec again when JSpec has async support ... mocking out FileReadStream // is to much of a hassle describe 'GET /public/*' it 'should transfer static files' get('/public/user.json').body.should.include '"name":' end end describe '#sendfile()' describe 'when the file exists' it 'should transfer the file' get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').body.should.include '"name":' end it 'should automatically set the content type based on extension' get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' end end describe 'when "cache static files" is enabled' it 'should transfer the file' enable('cache static files') get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/cached', function(){ var self = this this.cache.get('static:spec/fixtures/user.json', function(val){ self.halt(200, val ? 'yes' : 'no') }) }) get('/cached').body.should.eql 'no' get('/public/user.json').body.should.include '"name":' get('/cached').body.should.eql 'yes' get('/cached').body.should.eql 'yes' end it 'should automatically set the content type based on extension' enable('cache static files') get('/public/*', function(file){ this.sendfile('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' get('/public/user.json').headers['content-type'].should.eql 'application/json' get('/public/user.json').headers['content-type'].should.eql 'application/json' end end end describe '#download()' describe 'when the file exists' it 'should set attachment filename' get('/report', function(){ this.download('report.pdf') return 'foo' }) get('/report').headers['content-disposition'].should.eql 'attachment; filename="report.pdf"' end it 'should transfer the file' get('/public/*', function(file){ this.download('spec/fixtures/' + file) }) get('/public/user.json').body.should.include '"name":' end it 'should automatically set the content type based on extension' get('/public/*', function(file){ this.download('spec/fixtures/' + file) }) get('/public/user.json').headers['content-type'].should.eql 'application/json' end end end // describe 'GET /public/*' // it 'should transfer static files' // get('/public/user.json').body.should.include '"name":' // end // end // // describe '#sendfile()' // describe 'when the file exists' // it 'should throw an InvalidPathError when .. is found' // get('/public/*', function(file){ // this.sendfile(file) // }) // try { get('/public/../foobar') } // catch (e) { // e.name.should.eql 'InvalidPathError' // e.message.should.eql "`spec/fixtures/../foobar' is not a valid path" // } // end // // it 'should transfer the file' // get('/public/*', function(file){ // this.sendfile('spec/fixtures/' + file) // }) // get('/public/user.json').body.should.include '"name":' // end // // it 'should automatically set the content type based on extension' // get('/public/*', function(file){ // this.sendfile('spec/fixtures/' + file) // }) // get('/public/user.json').headers['content-type'].should.eql 'application/json' // end // end // end // // describe '#download()' // describe 'when the file exists' // it 'should set attachment filename' // get('/report', function(){ // this.download('report.pdf') // return 'foo' // }) // get('/report').headers['content-disposition'].should.eql 'attachment; filename="report.pdf"' // end // // it 'should transfer the file' // get('/public/*', function(file){ // this.download('spec/fixtures/' + file) // }) // get('/public/user.json').body.should.include '"name":' // end // // it 'should automatically set the content type based on extension' // get('/public/*', function(file){ // this.download('spec/fixtures/' + file) // }) // get('/public/user.json').headers['content-type'].should.eql 'application/json' // end // end // end // endendNo newline at end of fileNo newline at end of file