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

fix fs.read bug since node.js is upgrade #251

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
79 changes: 43 additions & 36 deletions lib/multipartform.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,42 +101,49 @@ 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;

var position = 0;

(function reader () {
fs.read(fd, 1024 * 4, position, "binary", function (er, chunk) {
if (er) callback(err);
stream.write(chunk);
position += 1024 * 4;
if (chunk) reader();
else {
stream.write("\r\n")
callback();
fs.close(fd);
}
});
})(); // reader()
});
} else if (this.value instanceof Data) {
stream.write(this.value.data);
stream.write("\r\n");
callback();
} else {
stream.write(this.value + "\r\n");
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+', function (err, fd) {
var stats = fs.fstatSync(fd);
var bufferSize = stats.size
if (err) throw err;
var chunkSize = 512;
var position = 0;
var buf = new Buffer(bufferSize);
(function reader () {

if ((position + chunkSize) > bufferSize) {
chunkSize = (bufferSize - position);
}

fs.read(fd, buf, position, chunkSize, position, function (er, chunk, buf) {
if (er) callback(err);
stream.write(buf.slice(position, chunkSize + position));
position += chunkSize;
if (position < bufferSize) reader();
else {
stream.write("\r\n")
callback();
fs.close(fd);
}
});
})(); // reader()
});
} else if (this.value instanceof Data) {
stream.write(this.value.data);
stream.write("\r\n");
callback();
} else {
stream.write(this.value + "\r\n");
callback();
}
}
}

//Renamed to MultiPartRequest from Request
Expand Down