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

encoding problem #22

Closed
atian25 opened this issue Aug 21, 2012 · 14 comments
Closed

encoding problem #22

atian25 opened this issue Aug 21, 2012 · 14 comments

Comments

@atian25
Copy link

atian25 commented Aug 21, 2012

hi, I'm using node-ftp at win7 cmd (chinese, gbk), and got trouble at list file name.

here is a workaround only for test, could you plz add support to this issue?


var iconv = require('../iconv-lite');
var BufferHelper = require('../bufferhelper');

FTP.prototype._pasvGetLines = function(emitter, type, cb) {
  return this.send('PASV', function(e, stream) {
    if (e)
      return cb(e);
    var curData = '', lines;

    //stream.setEncoding('utf8');
    
    stream.on('data', function(data) {

      var bufferHelper = new BufferHelper();
      bufferHelper.concat(data);

      curData += data;
      if (/\r\n|\n/.test(curData)) {

        curData = iconv.decode(bufferHelper.toBuffer(),'gbk');

        if (curData[curData.length-1] === '\n') {
          lines = curData.split(/\r\n|\n/);
          curData = '';
        } else {
          var pos = curData.lastIndexOf('\r\n');
          if (pos === -1)
            pos = curData.lastIndexOf('\n');
          lines = curData.substring(0, pos).split(/\r\n|\n/);
          curData = curData.substring(pos+1);
        }
        processDirLines(lines, emitter, type);
      }
    });
    stream.on('end', function() {
      emitter.emit('end');
    });
    stream.on('error', function(e) {
      emitter.emit('error', e);
    });
    cb();
  });
};

@mscdex
Copy link
Owner

mscdex commented Aug 21, 2012

What if you instead just change the line

stream.setEncoding('utf8');

to

stream.setEncoding('binary');

?

@atian25
Copy link
Author

atian25 commented Aug 22, 2012

no, that don't work. output : âÊÔ.md when file name is 测试.md
nodejs is not support gbk now, so we need modules like iconv / iconv-lite

@ggd543
Copy link

ggd543 commented Sep 6, 2012

I have the same issue

@ramstein74
Copy link

stream.setEncoding('binary');
^
TypeError: Cannot call method 'setEncoding' of undefined
at FTP._pasvGetLines (C:\CroNode\node_modules\ftp\ftp.js:508:12)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
[Finished in 0.4s with exit code 1]

@ramstein74
Copy link

changing ftp.js encoding to utf8 the error is the same.
I was testing conn.get '1.txt'

@mscdex
Copy link
Owner

mscdex commented Sep 24, 2012

@ramstein74 With the original code from master, can you set debug: console.log in the constructor options and post the output somewhere? I'm very interested in finding out what's causing this....

@ramstein74
Copy link

i solved this problem.
I did not have auth to download a file from ftp server.
Next error

i saw with node inspector

The console error is ftp_test.js:35
Server error:425 Unable to open the data connection

in node monitor (web) tracing stops at this file
Timers.JS

line 109

debug(msecs + ' list empty');
assert(L.isEmpty(list));
list.close();
delete lists[msecs];
};here exists!!!!!!!!!!!!!
}

My app is in coffee so i converted it to javascript (code below)

(function() {
var FTPClient, conn;

FTPClient = require("ftp");

conn = new FTPClient();

conn.on("connect", function() {
return conn.auth(function(e) {
if (e) {
throw e;
}
conn.cwd('\ftproot\memcard', function() {
return conn.pwd(function(e, dir) {
if (e) {
throw e;
}
return console.log(dir);
});
});
conn.get('1.txt', function(e, st) {
if (e) {
throw e;
}
st.on('success', function() {
return console.log("file received ok");
});
return st.on('error', function() {
return console.log("erro in file");
});
});
return conn.list(function(e, entries) {
var i, len;
if (e) {
throw e; This is line 35!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ( if i remove the get file code, the listing goes ok)
}
console.log("");
i = 0;
len = entries.length;
console.log(len);
while (i < len) {
if (typeof entries[i] !== "string") {
if (entries[i].type === "l") {
entries[i].type = "LINK";
} else if (entries[i].type === "-") {
entries[i].type = "FILE";
} else {
if (entries[i].type === "d") {
entries[i].type = "DIR";
}
}
console.log(" " + entries[i].type + " " + entries[i].size + " " + entries[i].date + " " + entries[i].name);
}
++i;
}
console.log("");
return conn.end();
});
});
});

conn.connect();

}).call(this);

@mscdex
Copy link
Owner

mscdex commented Dec 16, 2012

Please try with the master branch now if you can and let me know how it goes. All file names should be 'binary' strings.

@mscdex mscdex closed this as completed Jan 14, 2013
@lizhipower
Copy link

i can get the file lists from the ftp, and with iconv.decode(list.name, 'gbk') I can get the correct Chinese, but the problem is how can I get the specified file lists of a folder in Chinese, like ‘/测试’
i can't the file lists with
ftpClient.list( ‘/测试’, (e, list1) => { if (e) throw e; console.log(list1); });
but it;s ok with
ftpClient.list( ‘/test’, (e, list1) => { if (e) throw e; console.log(list1); });

@lfcgomes
Copy link

@lizhipower any advance on this? How did you solve this? I'm facing the same issue here.

@lizhipower
Copy link

lizhipower commented Nov 30, 2016 via email

@likev
Copy link

likev commented Jun 2, 2017

@mscdex you should use Buffer with filepath, or add a encode option with method such as list, cwd etc.

FTP.prototype.cwd = function (path, cb, promote) {
    let cmddata = 'CWD ';
    if (Buffer.isBuffer(path)) cmddata = Buffer.concat([Buffer.from(cmddata), path]);
     else cmddata += path;

    this._send(cmddata, function (err, text, code) {
        if (err)
        return cb(err);
        var m = RE_WD.exec(text);
        cb(undefined, m ? m[1] : undefined);
    }, promote);
};
FTP.prototype._send = function (cmd, cb, promote) {
    clearTimeout(this._keepalive);
    if (cmd !== undefined) {
        if (promote)
        this._queue.unshift({
            cmd: cmd,
            cb: cb
        });
         else
        this._queue.push({
            cmd: cmd,
            cb: cb
        });
    }
    var queueLen = this._queue.length;
    if (!this._curReq && queueLen && this._socket && this._socket.readable) {
        this._curReq = this._queue.shift();
        if (this._curReq.cmd === 'ABOR' && this._pasvSocket)
        this._pasvSocket.aborting = true;
        this._debug && this._debug('[connection] > ' + inspect(this._curReq.cmd));
        
        let cmddata = this._curReq.cmd;
        if (Buffer.isBuffer(cmddata)) cmddata = Buffer.concat([cmddata, Buffer.from('\r\n')]);
         else cmddata += '\r\n';
        this._socket.write(cmddata);
    } else if (!this._curReq && !queueLen && this._ending)
    this._reset();
};

@iamlion
Copy link

iamlion commented May 11, 2020

i can get the file lists from the ftp, and with iconv.decode(list.name, 'gbk') I can get the correct Chinese, but the problem is how can I get the specified file lists of a folder in Chinese, like ‘/测试’
i can't the file lists with
ftpClient.list( ‘/测试’, (e, list1) => { if (e) throw e; console.log(list1); });
but it;s ok with
ftpClient.list( ‘/test’, (e, list1) => { if (e) throw e; console.log(list1); });

how to resolve it?

@iamlion
Copy link

iamlion commented May 13, 2020

i can get the file lists from the ftp, and with iconv.decode(list.name, 'gbk') I can get the correct Chinese, but the problem is how can I get the specified file lists of a folder in Chinese, like ‘/测试’
i can't the file lists with
ftpClient.list( ‘/测试’, (e, list1) => { if (e) throw e; console.log(list1); });
but it;s ok with
ftpClient.list( ‘/test’, (e, list1) => { if (e) throw e; console.log(list1); });

I resolve it , You need convert 'cmd' to 'GBK'.

var iconv = require("../../iconv-lite");

FTP.prototype._send = function(cmd, cb, promote) {
  clearTimeout(this._keepalive);
  if (cmd !== undefined) {
    if (promote)
      this._queue.unshift({ cmd: cmd, cb: cb });
    else
      this._queue.push({ cmd: cmd, cb: cb });
  }
  var queueLen = this._queue.length;
  if (!this._curReq && queueLen && this._socket && this._socket.readable) {
    this._curReq = this._queue.shift();
    if (this._curReq.cmd === 'ABOR' && this._pasvSocket)
      this._pasvSocket.aborting = true;
    this._debug&&this._debug('[connection] > ' + inspect(this._curReq.cmd));

    let cmd = this._curReq.cmd + '\r\n';
    let buf = iconv.encode(cmd, 'gbk');
    this._socket.write(buf);
  } else if (!this._curReq && !queueLen && this._ending)
    this._reset();
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants