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

FTP.ls returning empty array #287

Open
jdalrymple opened this issue Jul 4, 2018 · 4 comments
Open

FTP.ls returning empty array #287

jdalrymple opened this issue Jul 4, 2018 · 4 comments

Comments

@jdalrymple
Copy link

Summary

When using the ls function, the return is always an empty array. If i use the list function, i see all the directories contents.

I've also tested by using the parse-listing library on the .list functions results and this seems to work well, so im unsure as to why the ls function is returning no results

@tinboyko
Copy link

Got same error

@jdalrymple
Copy link
Author

I didnt have the time to debug it, ended up making my own little wrapper

@tinboyko
Copy link

@jdalrymple are you parsing raw list? Can you share solution please.

@jdalrymple
Copy link
Author

jdalrymple commented Aug 22, 2018

import Promise from 'bluebird';
import fs from 'fs';
import FTP from 'ftp';

class FTPClient {
  constructor({ user, pass, host }) {
    if (!(host && user)) {
      throw new Error('Missing required params');
    }

    this.user = user;
    this.pass = pass;
    this.host = host;
  }

  get(path, output) {
    const c = new FTP();

    return new Promise((resolve, reject) => {
      c.on('ready', () => {
        c.get(path, (err, stream) => {
          if (err) reject(err);

          let contents = null;

          if (!output) {
            contents = '';
            stream.on('data', (chunk) => { contents += chunk; });
          } else {
            stream.pipe(fs.createWriteStream(output));
          }

          stream.once('close', () => {
            resolve(contents);
          });
        });
      });

      c.connect({ user: this.user, host: this.host, password: this.pass });
    }).finally(() => { c.end(); });
  }

  list(path) {
    const c = new FTP();

    return new Promise((resolve, reject) => {
      c.on('ready', () => {
        c.list(path, (err, list) => {
          if (err) reject(err);

          resolve(list);
        });
      });

      c.connect({ user: this.user, host: this.host, password: this.pass });
    }).finally(() => { c.end(); });
  }

  put(input, destination) {
    const c = new FTP();

    return new Promise((resolve, reject) => {
      c.on('ready', () => {
        c.put(input, destination, (err) => {
          if (err) reject(err);

          resolve();
        });
      });

      c.connect({ user: this.user, host: this.host, password: this.pass });
    }).finally(() => { c.end(); });
  }

  upload(path, outputPath) {
    return this.put(path, outputPath);
  }
}

export default FTPClient;

Example

  const client = new FTPClient(credentials);
  let filenames = await client.list(ftpPath);

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

2 participants