Multipart form-data / File uploading now works again
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Haven't re-implemented a timeout, wasn't sure what behaviour was wanted here...should be easy enough to do, wish I could get local tests working again :(
authored by ciaranj · committed by ciaranj · parent 8621786 · Browse files · 9 files·+67 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+67 added−69 removed
- other:+67 added−69 removed
Provenance
- Commit
9586ecf- Tree
0ce0ac0- Author
ciaranj <ciaranj@gmail.com>- Committer
ciaranj <ciaranj@gmail.com>- Parent
8621786
9 files changed+67 added−69 removed
modified:examples/upload/app.js+3 added-3 removed
23 unmodified lines24252627282928293031323333343536378 unmodified lines23 unmodified lines } })})
post('/upload', function(){ var self = thispost('/upload', function(){ var self = this; $(this.param('images')).each(function(image){ puts(image.filename + ' -> ' + image.tempfile) self.flash('info', 'Uploaded ' + image.filename) }) }) this.redirect('/upload')})
get('/public/*', function(file){8 unmodified linesmodified:lib/express/plugins/view.js-1 removed
63 unmodified lines64656667686869707132 unmodified lines63 unmodified lines
render: function(view, options) { var sys= require('sys'); sys.puts(sys.inspect(options)); var self = this, options = options || {}, path = set('views') + '/' + view,32 unmodified linesmodified:lib/express/collection.js+1 added-1 removed
496 unmodified lines497 array.push(val)498 return array499 }))500 else501- return $(process.mixin(this.arr, $(other).arr))501+ return $(utils.mixin(this.arr, $(other).arr))502 },503 504 /**505 * Clone the collection.44 unmodified linesmodified:lib/express/core.js+39 added-39 removed
206 unmodified lines207 require('http')208 .createServer(function(request, response){209 request.body = ''210 request.setBodyEncoding('binary')211+ var callback= function(error, result) {212+ if(error) { // THIS is the same code as in the 'route' code, needs refactoring213+ if (request.accepts('html') && set('show exceptions'))214+ request.halt(500, require('express/pages/show-exceptions').render(request, error))215+ else216+ request.halt(500)217+ if (set('throw exceptions'))218+ throw error219+ } 220+ else self.route(request, response); 221+ };222 if (request.headers['content-type'] &&223 request.headers['content-type'].indexOf('multipart/form-data') !== -1) {213- var stream = new multipart.Stream(request),214- promise = new events.Promise,215- pendingFiles = 0216- request.params = { post: {}}217- promise.timeout(set('upload timeout') || 5000)218- stream.addListener('part', function(part){219- if (part.filename) {220- part.pos = 0221- ++pendingFiles222- part.tempfile = '/tmp/express-' + Number(new Date) + utils.uid()223- part.addListener('body', function(chunk){224- fs.write(chunk, part.pos, 'binary').addErrback(function(){225- promise.emitError.apply(promise, arguments)226- })227- part.pos += chunk.length228- })229- .addListener('complete', function(){230- fs.close().addCallback(function(){231- if (!--pendingFiles)232- promise.emitSuccess()233- }).addErrback(function(){234- promise.emitError.apply(promise, arguments)235- })236- utils.mergeParam(part.name, part, request.params.post)237- })238- }239- else240- part.buf = '',241- part242- .addListener('body', function(chunk){ part.buf += chunk })243- .addListener('complete', function(){244- if (part.buf.length)245- utils.mergeParam(part.name, part.buf, request.params.post)246- })247- }).addListener('complete', function(){248- if (!pendingFiles) promise.emitSuccess()249- promise.addCallback(function(){ self.route(request, response) })250- })224+ var mp= multipart.parse(request);225+ request.params= { post: {}};226+ mp.addListener("partBegin", function (part) { 227+ if( part.filename ) {228+ part.tempfile= '/tmp/express-' + Number(new Date) + utils.uid(); 229+ part.fileStream= fs.createWriteStream( part.tempfile ); 230+ }231+ else part.buf= '';232+ });233+ mp.addListener("partEnd", function (part) { 234+ if( part.name ) { // Not sure why this guard is needed, but otherwise we get an undefined part.name being passed?235+ if( part.fileStream ) { 236+ part.fileStream.close(); 237+ utils.mergeParam(part.name, {filename:part.filename, tempfile:part.tempfile}, request.params.post) 238+ }239+ else {240+ utils.mergeParam(part.name, part.buf, request.params.post) 241+ }242+ }243+ });244+ 245+ mp.addListener("body", function (chunk) {246+ if( mp.part.fileStream ) mp.part.fileStream.write(chunk) ;247+ else part.buf+= chunk;248+ });249+ mp.addListener("error", callback); 250+ mp.addListener("complete", callback);251 }252 else253 request254 .addListener('data', function(chunk){ request.body += chunk })255- .addListener('end', function(){ self.route(request, response) })255+ .addListener('end', callback )256 })257 .listen(this.port, this.host, this.backlog)258 puts('Express started at http://' + this.host + ':' + this.port + '/ in ' + Express.environment + ' mode')259 },63 unmodified linesmodified:lib/express/event.js-1 removed
123345622 unmodified lines
// Express - Event - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
exports.Event = Class({ /** * Initialize with event _name_ and optional _data_.22 unmodified linesmodified:lib/express/plugin.js+2 added-2 removed
1-1+var utils = require('express/utils');2 // Express - Plugin - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4 /**5 * Push _plugin_ with _options_ to the plugin stack.30 unmodified lines36 */37 38 init: function(options) {39 if (options)40- process.mixin(this, options)40+ utils.mixin(this, options)41 },42 43 /**44 * Trigger handler for the given _event_.11 unmodified linesmodified:lib/express/plugins.js+10 added-10 removed
1234567891011123456789101112
// Express - Plugins - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
process.mixin(require('express/plugins/hooks'))process.mixin(require('express/plugins/flash'))process.mixin(require('express/plugins/cache'))process.mixin(require('express/plugins/cookie'))process.mixin(require('express/plugins/session'))process.mixin(require('express/plugins/profiler'))process.mixin(require('express/plugins/common-logger'))process.mixin(require('express/plugins/content-length'))process.mixin(require('express/plugins/method-override'))var utils = require('express/utils');utils.mixin(require('express/plugins/hooks'))utils.mixin(require('express/plugins/flash'))utils.mixin(require('express/plugins/cache'))utils.mixin(require('express/plugins/cookie'))utils.mixin(require('express/plugins/session'))utils.mixin(require('express/plugins/profiler'))utils.mixin(require('express/plugins/common-logger'))utils.mixin(require('express/plugins/content-length'))utils.mixin(require('express/plugins/method-override'))modified:lib/express/request.js+2 added-1 removed
7 unmodified lines8 var StaticFile = require('express/static').File,9 statusBodies = require('http').STATUS_CODES,10 queryString = require('querystring'),11 mime = require('express/mime'),12+ utils= require('express/utils'),13 url = require('url')14 15 // --- InvalidStatusCode16 46 unmodified lines63 * @api private64 */65 66 init: function(request, response) {66- process.mixin(true, this, request)67+ utils.mixin(true, this, request)68 response.headers = {}69 this.response = response70 this.url = url.parse(this.url)71 this.url.pathname = exports.normalizePath(this.url.pathname)204 unmodified linesmodified:lib/express/utils.js+10 added-11 removed
186 unmodified lines187 }188 }189 else {190 // Prevent never-ending loop191- if (target === d.value) {192- continue;193- }191+ if (target !== d.value) {192 195- if (deep && d.value && typeof d.value === "object") {196- target[k] = process.mixin(deep,197- // Never move original objects, clone them198- target[k] || (d.value.length != null ? [] : {})199- , d.value);193+ if (deep && d.value && typeof d.value === "object") {194+ target[k] = exports.mixin(deep,195+ // Never move original objects, clone them196+ target[k] || (d.value.length != null ? [] : {})197+ , d.value);198+ }199+ else {200+ target[k] = d.value;201+ }202 }201- else {202- target[k] = d.value;203- }203 }204 });205 }206 }3 unmodified lines