-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (57 loc) · 1.6 KB
/
index.js
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
'use strict';
const fs = require('fs');
const path = require('path');
const watt = require('gigawatts');
function download(ftp, urlObj, outputFile, callback, callbackProgress) {
let progress = 0;
const file = fs.createWriteStream(outputFile);
watt(function* (next) {
const total = yield ftp.size(urlObj.pathname, next);
const stream = yield ftp.get(urlObj.pathname, next);
yield new Promise((resolve, reject) => {
stream
.once('close', function () {
ftp.end();
})
.on('data', function (data) {
if (!callbackProgress) {
return;
}
progress += data.length;
callbackProgress(progress, total);
})
.on('error', reject)
.pipe(file)
.on('finish', function () {
/* HACK: see xHttp. */
const fd = fs.openSync(outputFile, 'r');
fs.closeSync(fd);
resolve();
});
});
})(callback);
}
exports.get = function (urlObj, outputFile, callback, callbackProgress) {
const xFs = require('xcraft-core-fs');
const Ftp = require('ftp');
xFs.mkdir(path.dirname(outputFile));
const ftp = new Ftp();
ftp.on('ready', () => {
download(ftp, urlObj, outputFile, callback, callbackProgress);
});
ftp.connect({
host: urlObj.hostname,
});
};
exports.size = watt(function* (urlObj, next) {
const Ftp = require('ftp');
const ftp = new Ftp();
ftp.on('ready', next.parallel());
ftp.connect({host: urlObj.hostname});
yield next.sync();
try {
return yield ftp.size(urlObj.pathname, next);
} finally {
ftp.end();
}
});