Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use marks for streaming uploads, upload done callback

  • Loading branch information...
commit 5fb6cd767921a3a5fc7c8bcd8040d6c01f4a1f72 1 parent 08d3859
@bancek bancek authored
Showing with 52 additions and 17 deletions.
  1. +15 −17 lib/jsftp.js
  2. +37 −0 test/jsftp_test.js
View
32 lib/jsftp.js
@@ -586,28 +586,26 @@ Ftp.getPasvPort = function(text) {
});
};
- this.getPutSocket = function(path, callback) {
+ this.getPutSocket = function(path, callback, doneCallback) {
var self = this;
this.getPasvSocket(function(err, socket) {
if (err) return callback(err);
- var hadErr;
- self._enqueueCmd("stor " + path, function(err, res) {
- if (err) hadErr = err;
- });
+ var called = false;
- // This is not a great solution. A STOR command only gives back an error
- // or a Mark. Marks are not very reliable, which is why jsftp doesn't
- // take them into account. But then if STOR is successful we never
- // know because we don't receive any response.
- // Still, the socket is just open and we can start to pump, so we set
- // a timeout and after that we pass the socket to the callback along
- // with any errors that STOR gave back.
- // A way to make this more reliable would be to look at the response
- // mark (Should be 150) and only then if it is ok send the socket back.
- setTimeout(function(err, res) {
- callback(hadErr, socket);
- }, 100);
+ var cmdCallback = function(err, res) {
+ if (!called) {
+ called = true;
+ callback(err, socket);
+ } else {
+ if (!Ftp.isMark(res.code) && doneCallback) {
+ doneCallback(res)
+ }
+ }
+ };
+ cmdCallback.acceptsMarks = true;
+
+ self._enqueueCmd("stor " + path, cmdCallback);
});
};
View
37 test/jsftp_test.js
@@ -298,6 +298,43 @@ describe("jsftp test suite", function() {
});
});
+ it("test put a big file stream", function(next) {
+ var remotePath = remoteCWD + "/bigfile.test";
+
+ var data = (new Array(1*1024*1024)).join("x");
+
+ var buffer = new Buffer(data, "binary");
+
+ ftp.getPutSocket(remotePath, function(err, socket) {
+ assert.ok(!err, err);
+
+ socket.write(data, function(err) {
+ assert.ok(!err, err);
+
+ socket.end();
+ });
+ }, function(res) {
+ assert.equal(res.code, 226);
+
+ ftp.raw.dele(remotePath, function(err, data) {
+ assert.ok(!err);
+ next();
+ });
+ });
+ });
+
+ it("test put a big file stream fail", function(next) {
+ var remotePath = remoteCWD + "/nonexisting/path/to/file.txt";
+
+ ftp.getPutSocket(remotePath, function(err, socket, res) {
+ assert.ok(err, err);
+ assert.equal(err.code, 550);
+ next();
+ }, function(res) {
+ assert.ok(false);
+ });
+ });
+
it("test get fileList array", function(next) {
var file1 = "testfile.txt";
Please sign in to comment.
Something went wrong with that request. Please try again.