Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also allow boolean values #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions lib/multipartform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Stream(stream) {
this.string = "";
}
this.stream = stream;

}

Stream.prototype = {
Expand Down Expand Up @@ -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";
},

Expand All @@ -87,45 +87,45 @@ 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
// You can also pass in a String and a String will be returned to the callback
// 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 {
stream.write("\r\n")
callback();
fs.close(fd);
}
});
})(); // reader()
});
})(); // reader()
});
} else {
stream.write(this.value + "\r\n");
Expand All @@ -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];
Expand All @@ -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) {
Expand Down