Skip to content
Myr

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

9 files+67 added−69 removed

modified:examples/upload/app.js+3 added-3 removed
modified:lib/express/plugins/view.js-1 removed
modified:lib/express/collection.js+1 added-1 removed
496 unmodified lines
497 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 lines
modified:lib/express/core.js+39 added-39 removed
206 unmodified lines
207 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 lines
modified:lib/express/event.js-1 removed
modified:lib/express/plugin.js+2 added-2 removed
11var utils = require('express/utils');2// Express - Plugin - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Push _plugin_ with _options_ to the plugin stack.
30 unmodified lines
36 */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 lines
modified:lib/express/plugins.js+10 added-10 removed
modified:lib/express/request.js+2 added-1 removed
7 unmodified lines
8var 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')1415// --- InvalidStatusCode16
46 unmodified lines
63 * @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 lines
modified:lib/express/utils.js+10 added-11 removed
186 unmodified lines
187 }188 }189 else {190 // Prevent never-ending loop191 if (target === d.value) {192 continue;193 }191 if (target !== d.value) {192195 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