ensure uploaded files close before routing
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Aaron Heckmann · committed by visionmedia · parent 8ee66dc · Browse files · 1 file·+16 added−9 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+16 added−9 removed
- other:+16 added−9 removed
Provenance
- Commit
680c07a- Tree
85696f7- Author
Aaron Heckmann <aaron.heckmann@gmail.com>- Committer
visionmedia <tj@vision-media.ca>- Parent
8ee66dc
1 file changed+16 added−9 removed
modified:lib/express/core.js+16 added-9 removed
207 unmodified lines208 require('http')209 .createServer(function(request, response){210 request.body = ''211 request.setBodyEncoding('binary')212+ var pendingFiles = 0213 function callback(e, result) {213- if (!e) return self.route(request, response)214- // TODO: refactore -- same code used in #route(); make util215- if (request.accepts('html') && set('show exceptions'))216- request.halt(500, require('express/pages/show-exceptions').render(request, e))214+ if (!e)215+ if (!pendingFiles) return self.route(request, response)216 else217+ // TODO: refactore -- same code used in #route(); make util218+ if (request.accepts('html') && set('show exceptions'))219+ request.halt(500, require('express/pages/show-exceptions').render(request, e))220+ else218- request.halt(500)219- if (set('throw exceptions'))220- throw error221- else self.route(request, response) 221+ request.halt(500)222+ if (set('throw exceptions'))223+ throw error224+ else self.route(request, response) 225 }226 if (request.headers['content-type'] &&227 request.headers['content-type'].indexOf('multipart/form-data') !== -1) {228 var stream = multipart.parse(request)229 request.params = { post: {}}230 stream231 .addListener('partBegin', function(part) { 232 if (part.filename)233+ ++pendingFiles,234 part.tempfile = '/tmp/express-' + Number(new Date) + utils.uid(),235 part.fileStream = fs.createWriteStream(part.tempfile)236 else237 part.buf = ''6 unmodified lines244 })245 .addListener('partEnd', function(part) {246 if (!part.name) return247 if (part.fileStream)244- part.fileStream.close(),248+ part.fileStream.close(function(){ 249+ --pendingFiles250+ callback(null)251+ }),252 utils.mergeParam(part.name, { filename: part.filename, tempfile: part.tempfile }, request.params.post) 253 else254 utils.mergeParam(part.name, part.buf, request.params.post) 255 })74 unmodified lines