SHARE
TWEET

Untitled

a guest Jul 12th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Первый код:
  2.  
  3. const jsftp = require('jsftp');
  4.  
  5. export const createFtpClient = async (host: string, port: number, username: string, password: string): Promise => {
  6.   const client: FtpWithAuth = new jsftp({
  7.     host,
  8.     port,
  9.   });
  10.  
  11.   return new Promise((resolve, reject) => {
  12.     client.on('connect', () => {
  13.       client.auth(username, password, (err, res) => {
  14.         if (err) {
  15.           quit(client);
  16.           reject(err);
  17.         } else {
  18.           resolve({
  19.             put: (sourcePath: string, pathToPut: string) => put(client, sourcePath, pathToPut),
  20.             ls: (pathToLs: string) => ls(client, pathToLs),
  21.             quit: () => quit(client),
  22.           });
  23.         }
  24.       });
  25.     });
  26.   });
  27. });
  28.  
  29.  
  30. interface FtpClient {
  31.   put: (sourcePath: string, pathToPut: string) => Promise;
  32.   ls: (pathToLs: string) => Promise<LsResponse[]>;
  33.   quit: () => void;
  34. }
  35.  
  36. interface FtpWithAuth extends Ftp {
  37.   auth: (username: string, password: string, callback: (error, response) => void) => void;
  38. }
  39.  
  40. interface LsResponse {
  41.   name: string;
  42. }
  43.  
  44. Второй код:
  45.  
  46. const ftpServer = require('ftp-srv');
  47. const bunyan = require(‘bunyan’);
  48.  
  49. export const startFtpServer = async (host: string, port: number, user: string, pass: string) => {  
  50.   const quietLog = bunyan.createLogger({
  51.     name: 'quiet-logger',
  52.     level: 60,
  53.   });
  54.  
  55.   const server = new ftpServer(`ftp://${host}:${port}`, {
  56.     log: quietLog,
  57.     pasv_range: '8400-8500', // change this to an open port range for your app
  58.   });
  59.  
  60.   server.on('login', ({ connection, username, password }, resolve, reject) => {
  61.     if (username === user && password === pass) {
  62.       // If connected, add a handler to confirm file uploads
  63.       connection.on('STOR', (error, fileName) => {
  64.         if (error) {
  65.           console.error(`FTP server error: could not receive file ${fileName} for upload ${error}`);
  66.         }
  67.         console.info(`FTP server: upload successfully received - ${fileName}`);
  68.       });
  69.       resolve();
  70.     } else {
  71.       reject(new Error('Unable to authenticate with FTP server: bad username or password'));
  72.     }
  73.   });
  74.  
  75.   server.on('client-error', ({ context, error }) => {
  76.     console.error(`FTP server error: error interfacing with client ${context} ${error} on ftp://${host}:${port} ${JSON.stringify(error)}`);
  77.   });
  78.  
  79.   const closeFtpServer = async () => {
  80.     await server.close();
  81.   };
  82.  
  83.   // The types are incorrect here - listen returns a promise
  84.   await server.listen();
  85.  
  86.   return {
  87.     shutdownFunc: async () => {
  88.       // server.close() returns a promise - another incorrect type
  89.       await closeFtpServer();
  90.     },
  91.   };
  92. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top