Skip to content

Commit c2a5607

Browse files
authored
kundaiAtPropertyMe - updated bug
danwrong#251
1 parent f31e457 commit c2a5607

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

lib/multipartform.js

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,49 @@ Part.prototype = {
101101
// with the whole Part
102102
// Calls the callback when complete
103103
write: function(stream, callback) {
104-
105-
var self = this;
106-
107-
//first write the Content-Disposition
108-
stream.write(this.header());
109-
110-
//Now write out the body of the Part
111-
if (this.value instanceof File) {
112-
fs.open(this.value.path, "r", 0666, function (err, fd) {
113-
if (err) throw err;
114-
115-
var position = 0;
116-
117-
(function reader () {
118-
fs.read(fd, 1024 * 4, position, "binary", function (er, chunk) {
119-
if (er) callback(err);
120-
stream.write(chunk);
121-
position += 1024 * 4;
122-
if (chunk) reader();
123-
else {
124-
stream.write("\r\n")
125-
callback();
126-
fs.close(fd);
127-
}
128-
});
129-
})(); // reader()
130-
});
131-
} else if (this.value instanceof Data) {
132-
stream.write(this.value.data);
133-
stream.write("\r\n");
134-
callback();
135-
} else {
136-
stream.write(this.value + "\r\n");
137-
callback();
138-
}
139-
}
104+
105+
var self = this;
106+
107+
//first write the Content-Disposition
108+
stream.write(this.header());
109+
110+
//Now write out the body of the Part
111+
if (this.value instanceof File) {
112+
fs.open(this.value.path, 'r+', function (err, fd) {
113+
var stats = fs.fstatSync(fd);
114+
var bufferSize = stats.size
115+
if (err) throw err;
116+
var chunkSize = 512;
117+
var position = 0;
118+
var buf = new Buffer(bufferSize);
119+
(function reader () {
120+
121+
if ((position + chunkSize) > bufferSize) {
122+
chunkSize = (bufferSize - position);
123+
}
124+
125+
fs.read(fd, buf, position, chunkSize, position, function (er, chunk, buf) {
126+
if (er) callback(err);
127+
stream.write(buf.slice(position, chunkSize + position));
128+
position += chunkSize;
129+
if (position < bufferSize) reader();
130+
else {
131+
stream.write("\r\n")
132+
callback();
133+
fs.close(fd);
134+
}
135+
});
136+
})(); // reader()
137+
});
138+
} else if (this.value instanceof Data) {
139+
stream.write(this.value.data);
140+
stream.write("\r\n");
141+
callback();
142+
} else {
143+
stream.write(this.value + "\r\n");
144+
callback();
145+
}
146+
}
140147
}
141148

142149
//Renamed to MultiPartRequest from Request

0 commit comments

Comments
 (0)