Skip to content

Latest commit

 

History

History
300 lines (187 loc) · 7.4 KB

client.md

File metadata and controls

300 lines (187 loc) · 7.4 KB

Qusly-Core documentation

Table of contents

Class Client

An API, which provides access to FTP/FTPS/SFTP servers. It handles every method in a single queue.

Event: 'abort'

Emitted when the client.abort() has been called and before any reconnection is requested.

Event: 'connected'

Emitted when client has connected to a server.

Event: 'disconnected'

Emitted when client has disconnected from a server.

Event: 'progress'

Emitted when a chunk of a file has been sent to a server. You can access information like transfer speed or eta in progress. Basic file information, for example size and remote path in info.

const client = new Client();

client.on('progress', (progress, info) => {
  if (info.type === 'download') {
    console.log(
      `${info.remotePath} => ${info.localPath} - ${progress.eta} seconds left`,
    );
  }
});

client.abort()

  • Returns: Promise<void>

Emits the abort event. Then stops the current file transfer by reconnecting to a server, using the same config provided with the client.connect().

client.connect(config)

  • config IConfig
  • Returns: Promise<void>

Chooses which library to use, depending on the protocol provided in the config. Connects to a server and then the connected event is fired.

client.connected

  • Boolean

Indicates if client is connected to a server.

client.createBlank(type, [, path][, files])

  • type 'folder' | 'file'
  • path String (optional)
  • files IFile[] (optional)
  • Returns: Promise<string>

Creates an empty file or folder at path with an unique name. If you don't provide the files arg, it will fetch automatically. Returns name of the file.

const client = new Client();
const name = await client.createBlank('file', '/documents');

console.log(`Name: ${name}`); // Name: new file

client.delete(path)

  • path String
  • Returns: Promise<void>

Removes any files and folders at path.

client.disconnect()

  • Returns: Promise<void>

Aborts the current file transfer, if any. Then after client has disconnected from a server, the disconnected event is fired.

client.download(path, dest, [, options])

Downloads a remote file. Pipes data into dest. When a new chunk of a file has been sent, the progress event is fired. You can start at a given offset by setting options, which can be used to resume a transfer. Returns status of the transfer.

import { resolve } from 'path';

const client = new Client();

const remotePath = '/documents/new file.txt';
const localPath = resolve('downloads', 'new file.txt');

const status = await client.download(
  remotePath,
  createWriteStream(localPath, { flags: 'a' }),
  { startAt: 24000 }, // It will start at 24000 bytes
);

console.log(status); // finished

client.exists(path)

  • path String
  • Returns: Promise<boolean>

Checks if file at path exists.

const client = new Client();
const exists = await client.exists('/documents/new file.txt');

console.log(exists ? 'does exists!' : "doesn't exists!");

client.mkdir(path)

  • path String
  • Returns: Promise<void>

Creates a new folder at path.

client.move(srcPath, destPath)

  • srcPath String
  • destPath String
  • Returns: Promise<void>

Moves a file from srcPath to destPath. Can be used to rename a file.

client.pwd()

  • Returns: Promise<string>

Returns path of the current working directory.

client.readDir([, path])

  • path String (optional)
  • Returns: Promise<IFile[]>

Lists files and folders at path.

client.rimraf(path)

  • path string
  • Returns: Promise<void>

Deletes any file and folder at path.

client.send(command)

  • command String
  • Returns: Promise<string>

Sends a raw command to a server and returns the response.

client.size(path)

  • path String
  • Returns: Promise<number>

Returns size of the file at path in bytes.

client.stat(path)

  • path String
  • Returns: Promise<IStats>

Returns details about the file at path.

client.touch(path)

  • path String
  • Returns: Promise<void>

Creates an empty file at path.

client.unlink(path)

  • path String
  • Returns: Promise<void>

Deletes a single file (not a folder) at path.

client.upload(path, source, [, options])

Uploads a local file. When a new chunk of a file has been sent, the progress event is fired. Returns status of the transfer.

import { resolve } from 'path';

const client = new Client();

const localPath = resolve('uploads', 'new file.txt');
const remotePath = '/documents/new file.txt';

const status = await client.upload(remotePath, createReadStream(localPath));

console.log(status); // finished