File size: 2,761 Bytes
5273cd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Client } from 'ssh2';
import fs from 'fs';
import path from 'path';

export default class RemoteFetcher {
    constructor(config) {
        this.config = {
            host: config.host,
            port: config.port || 22,
            username: config.username,
            password: config.password,
            readyTimeout: 40000,
            keepaliveInterval: 10000
        };
        this.conn = new Client();
    }

    connect() {
        return new Promise((resolve, reject) => {
            this.conn.on('ready', () => {
                this.conn.sftp((err, sftp) => {
                    if (err) return reject(err);
                    this.sftp = sftp;
                    resolve();
                });
            })
            .on('error', (err) => reject(err))
            .connect(this.config);
        });
    }

    // Volvemos a fastGet pero con configuración optimizada para velocidad/estabilidad
    async fetchFile(remotePath, localPath, onProgress) {
        return new Promise((resolve, reject) => {
            this.sftp.fastGet(remotePath, localPath, {
                concurrency: 6, // 6 descargas en paralelo para mayor velocidad
                chunkSize: 32768, // Bloques de 32KB
                step: (transferred, chunk, total) => {
                    if (onProgress) onProgress(transferred, total);
                }
            }, (err) => {
                if (err) return reject(err);
                resolve();
            });
        });
    }

    async listFiles(remoteDir, extension = '') {
        return new Promise((resolve, reject) => {
            this.sftp.readdir(remoteDir, (err, list) => {
                if (err) return reject(err);
                const files = list
                    .filter(f => f.attrs.isFile())
                    .map(f => f.filename);
                
                if (extension) {
                    resolve(files.filter(f => f.toLowerCase().endsWith(extension.toLowerCase())));
                } else {
                    resolve(files);
                }
            });
        });
    }

    async listDirectories(remoteDir) {
        return new Promise((resolve, reject) => {
            this.sftp.readdir(remoteDir, (err, list) => {
                if (err) return reject(err);
                resolve(list.filter(f => f.attrs.isDirectory() && f.filename !== '.' && f.filename !== '..').map(f => f.filename));
            });
        });
    }

    async getFileStats(remotePath) {
        return new Promise((resolve, reject) => {
            this.sftp.stat(remotePath, (err, stats) => {
                if (err) return reject(err);
                resolve(stats);
            });
        });
    }

    disconnect() {
        this.conn.end();
    }
}