Refactor uploader
This commit is contained in:
parent
a1a39778aa
commit
6ec5f456f3
12 changed files with 647 additions and 261 deletions
|
@ -1,35 +1,50 @@
|
|||
import fs from 'fs';
|
||||
import createError from 'http-errors';
|
||||
import { get as getFile } from './sftp';
|
||||
import { get as getSFTP } from './sftp';
|
||||
import { get as getS3 } from './s3';
|
||||
|
||||
import debugname from 'debug';
|
||||
const debug = debugname('hostr:file-stream');
|
||||
|
||||
export default function* hostrFileStream(localPath, remotePath) {
|
||||
function writer(localPath, remoteRead) {
|
||||
return new Promise((resolve, reject) => {
|
||||
remoteRead.once('error', () => {
|
||||
debug('remote error');
|
||||
reject(createError(404));
|
||||
});
|
||||
const localWrite = fs.createWriteStream(localPath);
|
||||
localWrite.once('finish', () => {
|
||||
debug('local write end');
|
||||
resolve(fs.createReadStream(localPath));
|
||||
});
|
||||
|
||||
remoteRead.once('readable', () => {
|
||||
debug('writing');
|
||||
remoteRead.pipe(localWrite);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export default function hostrFileStream(localPath, remotePath) {
|
||||
const localRead = fs.createReadStream(localPath);
|
||||
return new Promise((resolve, reject) => {
|
||||
localRead.once('error', () => {
|
||||
debug('local error');
|
||||
const remoteFile = getFile(remotePath);
|
||||
|
||||
remoteFile.then((remoteRead) => {
|
||||
const localWrite = fs.createWriteStream(localPath);
|
||||
localWrite.once('finish', () => {
|
||||
debug('local write end');
|
||||
resolve(fs.createReadStream(localPath));
|
||||
debug('not found locally');
|
||||
getSFTP(remotePath)
|
||||
.then((remoteRead) => writer(localPath, remoteRead))
|
||||
.then(resolve)
|
||||
.catch((err) => {
|
||||
debug('not on sftp', err);
|
||||
writer(localPath, getS3(remotePath))
|
||||
.then(resolve)
|
||||
.catch((s3err) => {
|
||||
debug('not on s3');
|
||||
reject(s3err);
|
||||
});
|
||||
});
|
||||
remoteRead.pipe(localWrite);
|
||||
|
||||
remoteRead.once('error', () => {
|
||||
debug('remote error');
|
||||
reject(createError(404));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
localRead.once('readable', () => {
|
||||
debug('local readable');
|
||||
debug('found locally');
|
||||
resolve(localRead);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue