sftp上传文件

var fs = require('fs');
var stat = fs.stat;

let Client = require('ssh2-sftp-client');


// let remoteFilePath = '/opt/tomcat7/webapps/app/mobile'
// let localFilePath = '/Users/zxs/Documents/workSpace/万达/wwz/wd_mobile/dist'

// let host = 'xxx.xxx.xx.xx';
// let port = 22;
// let username = 'root';
// let password = '********';

let remoteFilePath = '/root/../tomcat7/webapps/110/app/mWeb'
let localFilePath = '/Users/xxx/Documents/workSpace/xxx/wwz/wd_mobile/dist'

let host = 'xxx.xxx.xxx.xx';
let port = 2222;
let username = 'root';
let password = '********';

let count = 0;
let finishCount = 0;

let sftp = new Client();
sftp.connect({
    host: host,
    port: port,
    username: username,
    password: password
}).then(() => {
    console.log('do');
    listFiles();
    checkDir(localFilePath, remoteFilePath);
}).then((data) => {
    console.log('connect success');
}).catch((err) => {
    console.log(err, 'connect error');
});

function listFiles() {
    // return sftp.list('/pathname');
    sftp.list(remoteFilePath).then(
        (data)=>{
            console.log(data);
        }
    )
}

function checkDir(localFilePath, remoteFilePath) {
    fs.exists(localFilePath, function(exists) {
        if (!exists) { //不存在
            console.log(localFilePath + '本地目录不存在');
            // sftp.end();
        } else { //存在
            countFiles(localFilePath);
            console.log('count:' + count);
            removeFiles(sftp, remoteFilePath);
        }
    })
}

function countFiles(fileDir) {
    fs.readdir(fileDir, function(err, paths) {
        paths.forEach(function(path) {
            var _src = fileDir + '/' + path;
            stat(_src, function(err, st) {
                if (err) {
                    throw err;
                }
                if (st.isFile()) {
                    count++;
                } else if (st.isDirectory()) {
                    countFiles(_src);
                }
            })
        })
    })
}

function putDir(localFilePath, remoteFilePath) {
    console.log('creating' + remoteFilePath);
    sftp.mkdir(remoteFilePath, true).then((data) => {
        console.log('creat dir success');
        fs.readdir(localFilePath, function(err, paths) {
            console.log(paths);
            if (err) {
                throw err;
            }
            paths.forEach(function(path) {
                var _src = localFilePath + '/' + path;
                var _dst = remoteFilePath + '/' + path;
                stat(_src, function(err, st) {
                    if (err) {
                        throw err;
                    }
                    if (st.isFile()) {
                        put(sftp, _src, _dst);
                    } else if (st.isDirectory()) {
                        putDir(_src, _dst);
                    }
                });
            });
        })
    }).catch((err) => {
        console.log(err, 'creat dir error');
    });
}

function put(sftp, fromSrc, toSrc, next) {
    sftp.put(fromSrc, toSrc).then((data) => {
        finishCount++;
        if (count == finishCount) {
            sftp.end();
            console.log('上传完毕!');
        }
        if (next) {
            next();
        }
    }).catch((err) => {
        console.log(err, 'uploadFile error');
    });
}

function removeFiles(sftp, dir, next) {
    sftp.rmdir(dir, true).then((data) => {
        console.log('clear files success!');
        putDir(localFilePath, remoteFilePath);
        if (next) {
            next(sftp, dir);
        }
    }).catch((err) => {
        console.log(err, 'clear files error');
        if (next) {
            next(sftp, dir);
        }
    });
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值