-
-
Save felixge/b1556e500b5edf2123c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.Assembly.prototype._receiveUpload = function() { | |
var req = this.upload.req, | |
self = this, | |
files = {}, | |
filesToFlush = 0, | |
uploadComplete = false; | |
if (!req.headers['content-type'].match(/^multipart\/form-data/i)) { | |
return this.emit('upload.error', str.sprintf( | |
'Invalid Content-Type header "%s". Only multipart/form-data is supported.', | |
req.headers['content-type'] | |
)); | |
} | |
var stream = new multipart.Stream(req); | |
stream.addListener('part', function(part) { | |
part.addListener('body', function(chunk) { | |
self.emit('upload.progress', stream.bytesReceived, stream.bytesTotal); | |
if (!part.filename) { | |
return part.value = (part.value || '') + chunk; | |
} | |
if (!part.file) { | |
filesToFlush++; | |
var id = str.uuid(), | |
path = self.upload.dir+'/'+self.id; | |
part.file = { | |
id: id, | |
path: path, | |
handle: new file.File(path, 'w+', {encoding: 'binary'}), | |
}; | |
} | |
part.file.handle.write(chunk); | |
}); | |
part.addListener('complete', function() { | |
if (part.name == 'data' || part.name == ('data'+self.id)) { | |
try { | |
var value = JSON.parse(part.value); | |
} catch (e) { | |
self.emit('request.parseError', e); | |
} | |
self.emit('request.received', value); | |
} else if (part.file) { | |
part.file.handle.close().addCallback(function() { | |
filesToFlush--; | |
if (uploadComplete && filesToFlush == 0) { | |
self.emit('upload.received', files); | |
} | |
}); | |
files[part.name] = part; | |
} | |
}); | |
}); | |
stream.addListener('complete', function() { | |
uploadComplete = true; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment