From dab968709639e717cccf92b2be365706824ebbc3 Mon Sep 17 00:00:00 2001 From: Sebastian Hoitz Date: Tue, 25 Sep 2012 20:28:02 +0200 Subject: [PATCH] Also allow boolean values --- lib/multipartform.js | 52 ++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/multipartform.js b/lib/multipartform.js index e917676..3358699 100644 --- a/lib/multipartform.js +++ b/lib/multipartform.js @@ -11,7 +11,7 @@ function Stream(stream) { this.string = ""; } this.stream = stream; - + } Stream.prototype = { @@ -59,24 +59,24 @@ function Part(name, value, boundary) { Part.prototype = { - - //returns the Content-Disposition header + + //returns the Content-Disposition header header: function() { var header; - + if (this.value.data) { - header = "Content-Disposition: form-data; name=\"" + this.name + + header = "Content-Disposition: form-data; name=\"" + this.name + "\"; filename=\"" + this.value.filename + "\"\r\n" + "Content-Type: " + this.value.contentType; } if (this.value instanceof File) { - header = "Content-Disposition: form-data; name=\"" + this.name + + header = "Content-Disposition: form-data; name=\"" + this.name + "\"; filename=\"" + this.value.filename + "\"\r\n" + - "Content-Length: " + this.value.fileSize + "\r\n" + - "Content-Type: " + this.value.contentType; + "Content-Length: " + this.value.fileSize + "\r\n" + + "Content-Type: " + this.value.contentType; } else { header = "Content-Disposition: form-data; name=\"" + this.name + "\""; } - + return "--" + this.boundary + "\r\n" + header + "\r\n\r\n"; }, @@ -87,12 +87,12 @@ Part.prototype = { valueSize = this.value.fileSize; } else if (this.value.data) { valueSize = this.value.data.length; - } else if (typeof this.value === 'number') { + } else if (typeof this.value === 'number' || typeof this.value === 'boolean') { valueSize = this.value.toString().length; } else { valueSize = this.value.length; } - return valueSize + this.header().length + 2; + return valueSize + this.header().length + 2; }, // Writes the Part out to a writable stream that supports the write(data) method @@ -100,23 +100,23 @@ Part.prototype = { // with the whole Part // Calls the callback when complete write: function(stream, callback) { - + var self = this; - + //first write the Content-Disposition stream.write(this.header()); - + //Now write out the body of the Part if (this.value instanceof File) { - fs.open(this.value.path, "r", 0666, function (err, fd) { - if (err) throw err; - + fs.open(this.value.path, "r", 0666, function (err, fd) { + if (err) throw err; + var position = 0; - + (function reader () { fs.read(fd, 1024 * 4, position, "binary", function (er, chunk) { if (er) callback(err); - stream.write(chunk); + stream.write(chunk); position += 1024 * 4; if (chunk) reader(); else { @@ -124,8 +124,8 @@ Part.prototype = { callback(); fs.close(fd); } - }); - })(); // reader() + }); + })(); // reader() }); } else { stream.write(this.value + "\r\n"); @@ -150,14 +150,14 @@ MultiPartRequest.prototype = { } return partNames; }, - + write: function(stream, callback) { var partCount = 0, self = this; - + // wrap the stream in our own Stream object // See the Stream function above for the benefits of this var stream = new Stream(stream); - + // Let each part write itself out to the stream (function writePart() { var partName = self.partNames[partCount]; @@ -176,12 +176,12 @@ MultiPartRequest.prototype = { if (callback) callback(stream.string || ""); } }); - })(); + })(); } } var exportMethods = { - file: function(path, filename, fileSize, encoding, contentType) { + file: function(path, filename, fileSize, encoding, contentType) { return new File(path, filename, fileSize, encoding, contentType) }, data: function(filename, contentType, data) {