All files / src/services ftp.service.ts

26.92% Statements 7/26
100% Branches 0/0
14.29% Functions 1/7
20.83% Lines 5/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 991x 1x 1x     1x         19x                                                                                                                                                                                
import { FtpConfigs } from '@logchain/configs/ftp-configs';
import { bind, BindingScope } from '@loopback/core';
import Client from 'ssh2-sftp-client';
 
@bind({ scope: BindingScope.TRANSIENT })
export class FTPService {
  // This client is used to connect to FTP server
  public client;
 
  constructor() {
    this.client = new Client();
  }
 
  /**
   * Connects to the FTP server using the provided credentials.
   *
   * @return {Promise<void>} A promise that resolves when the connection is established.
   */
  async connect(): Promise<void> {
    const options ={
      host: FtpConfigs.host,
      user: FtpConfigs.user,
      password: FtpConfigs.pass,
      secure: true,
    };
    try {
      await this.client.connect(options);
    } catch (err) {
      console.log('Failed to connect:', err);
    }
  }
 
  /**
   * Disconnects the client from the server.
   *
   * @return {Promise<void>} A promise that resolves when the client is successfully disconnected.
   */
  async disconnect(): Promise<void> {
    await this.client.end();
  }
 
  /**
   * Lists files in a remote directory.
   *
   * @param {string} remoteDir - The remote directory to list files from.
   * @return {Promise<any>} - A promise that resolves with the list of files.
   */
  async listFiles(remoteDir: string) {
    console.log(`Listing ${remoteDir} ...`);
    return this.client.list(remoteDir);
  }
 
  /**
   * Uploads a file from the local system to a remote location.
   *
   * @param {string} localFile - The path of the local file to be uploaded.
   * @param {string} remoteFile - The path of the remote file where the local file will be uploaded to.
   */
  async uploadFile(localFile: string, remoteFile: string) {
    console.log(`Uploading ${localFile} to ${remoteFile} ...`);
    try {
      await this.client.put(localFile, remoteFile);
    } catch (err) {
      console.error('Uploading failed:', err);
    }
  }
 
  /**
   * Downloads a file from a remote location and saves it locally.
   *
   * @param {string} remoteFile - The URL or path of the remote file to download.
   * @param {string} localFile - The path where the downloaded file should be saved locally.
   * @return {Promise<void>} - A Promise that resolves when the file has been downloaded successfully, or rejects with an error if the download fails.
   */
  async downloadFile(remoteFile: string, localFile: string): Promise<void> {
    console.log(`Downloading ${remoteFile} to ${localFile} ...`);
    try {
      await this.client.get(remoteFile, localFile);
    } catch (err) {
      console.error('Downloading failed:', err);
    }
  }
 
  /**
   * Deletes the specified remote file.
   *
   * @param {string} remoteFile - The path of the remote file to delete.
   * @return {Promise<void>} A promise that resolves when the file is successfully deleted.
   */
  async deleteFile(remoteFile: string): Promise<void> {
    console.log(`Deleting ${remoteFile}`);
    try {
      await this.client.delete(remoteFile);
    } catch (err) {
      console.error('Deleting failed:', err);
    }
  }
}