Refactor uploader
This commit is contained in:
parent
a1a39778aa
commit
6ec5f456f3
12 changed files with 647 additions and 261 deletions
29
lib/sftp.js
29
lib/sftp.js
|
@ -1,40 +1,29 @@
|
|||
import { dirname } from 'path';
|
||||
import Client from 'ssh2-sftp-client';
|
||||
import { dirname, join } from 'path';
|
||||
import Client from './ssh2-sftp-client';
|
||||
import debugname from 'debug';
|
||||
const debug = debugname('hostr:sftp');
|
||||
|
||||
export function get(remotePath) {
|
||||
debug('fetching', join('hostr', 'uploads', 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);
|
||||
});
|
||||
})
|
||||
.then(() => sftp.get(join('hostr', 'uploads', remotePath), { encoding: null }));
|
||||
}
|
||||
|
||||
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();
|
||||
});
|
||||
})
|
||||
.then(() => sftp.put(localPath, remotePath, true))
|
||||
.catch(() => sftp.mkdir(dirname(remotePath), true)
|
||||
.then(() => sftp.put(localPath, remotePath, true)));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue