Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support to parse buffer submitted. #153

Open
wants to merge 4 commits into from

1 participant

Math-
Math-

Useful if you need to listen "data" events (and register data) before calling "formidable". #130 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 19, 2012
  1. Math-

    Add support to parse buffer submitted. Useful if you need to listen "…

    Math- authored
    …data" events (and register data) before calling "formidable". felixge#130 (comment)
  2. Math-

    Update "parse" method.

    Math- authored
Commits on Jun 20, 2012
  1. Math-
Commits on Jun 24, 2012
  1. Math-

    Update "_uploadPath". Previously, "crypto.randomBytes" needed to be c…

    Math- authored
    …alled synchronously which is not adequate.
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 17 deletions.
  1. +42 −17 lib/incoming_form.js
59 lib/incoming_form.js
View
@@ -22,6 +22,7 @@ function IncomingForm(opts) {
this.keepExtensions = opts.keepExtensions || false;
this.uploadDir = opts.uploadDir || IncomingForm.UPLOAD_DIR;
this.encoding = opts.encoding || 'utf-8';
+ this.tmpName = opts.tmpName || false;
this.headers = null;
this.type = null;
@@ -49,7 +50,16 @@ IncomingForm.UPLOAD_DIR = (function() {
}
})();
-IncomingForm.prototype.parse = function(req, cb) {
+IncomingForm.prototype.parse = function(req, buf, cb) {
+
+ var isBufferSubmitted=false;
+ if(Buffer.isBuffer(buf)){
+ isBufferSubmitted=true;
+ }
+ else if(typeof buf == 'function'){
+ cb=buf;
+ }
+
this.pause = function() {
try {
req.pause();
@@ -81,27 +91,37 @@ IncomingForm.prototype.parse = function(req, cb) {
this.writeHeaders(req.headers);
- var self = this;
+ var self = this,
+ onReqEnd = function() {
+ if (self.error) {
+ return;
+ }
+
+ var err = self._parser.end();
+ if (err) {
+ self._error(err);
+ }
+ };
+
req
.on('error', function(err) {
self._error(err);
})
.on('aborted', function() {
self.emit('aborted');
- })
- .on('data', function(buffer) {
- self.write(buffer);
- })
- .on('end', function() {
- if (self.error) {
- return;
- }
-
- var err = self._parser.end();
- if (err) {
- self._error(err);
- }
});
+
+ if(!isBufferSubmitted){
+ req
+ .on('data', function(buffer) {
+ self.write(buffer);
+ })
+ .on('end', onReqEnd);
+ }
+ else{
+ self.write(buf);
+ onReqEnd();
+ }
if (cb) {
var fields = {}, files = {};
@@ -355,9 +375,14 @@ IncomingForm.prototype._initUrlencoded = function() {
};
IncomingForm.prototype._uploadPath = function(filename) {
+
var name = '';
- for (var i = 0; i < 32; i++) {
- name += Math.floor(Math.random() * 16).toString(16);
+ if (typeof this.tmpName == 'string' ) name += this.tmpName;//Use concatenation operation to ensure to have a string data
+ else if (typeof this.tmpName == 'function' ) name += this.tmpName();//Use concatenation operation to ensure to have a string data
+ else {
+ for (var i = 0; i < 32; i++) {
+ name += Math.floor(Math.random() * 16).toString(16);
+ }
}
if (this.keepExtensions) {
Something went wrong with that request. Please try again.