Refactor uploads

This commit is contained in:
Jonathan Cremin 2016-05-25 21:03:07 +01:00
parent 75a46212da
commit f59b9a5d22
14 changed files with 297 additions and 269 deletions

40
lib/sftp.js Normal file
View file

@ -0,0 +1,40 @@
import { dirname } from 'path';
import Client from 'ssh2-sftp-client';
import debugname from 'debug';
const debug = debugname('hostr:sftp');
export function get(remotePath) {
const sftp = new Client();
return sftp.connect({
host: process.env.SFTP_HOST,
port: process.env.SFTP_PORT,
username: process.env.SFTP_USERNAME,
password: process.env.SFTP_PASSWORD,
}).then(() => {
return sftp.get('hostr/uploads/' + remotePath, true);
});
}
export function upload(localPath, remotePath) {
debug('SFTP connecting');
const sftp = new Client();
return sftp.connect({
host: process.env.SFTP_HOST,
port: process.env.SFTP_PORT,
username: process.env.SFTP_USERNAME,
password: process.env.SFTP_PASSWORD,
}).then(() => {
return sftp.put(localPath, remotePath, true).then(() => {
sftp.end();
});
}).catch(() => {
debug('Creating ' + dirname(remotePath));
return sftp.mkdir(dirname(remotePath), true).then(() => {
return sftp.put(localPath, remotePath, true).then(() => {
sftp.end();
});
});
}).then(() => {
sftp.end();
});
}