UNPKG

3.12 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || function (d, b) {
3 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4 function __() { this.constructor = d; }
5 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6};
7var nest_1 = require('./nest');
8var fileJob_1 = require('./../job/fileJob');
9var EasyFtp = require('easy-ftp'), tmp = require('tmp');
10var Ftp = (function (_super) {
11 __extends(Ftp, _super);
12 function Ftp(e, host, port, username, password, checkEvery) {
13 if (port === void 0) { port = 21; }
14 if (username === void 0) { username = ''; }
15 if (password === void 0) { password = ''; }
16 if (checkEvery === void 0) { checkEvery = 0; }
17 _super.call(this, e, host);
18 this.client = new EasyFtp();
19 this.config = {
20 host: host,
21 port: port,
22 username: username,
23 password: password
24 };
25 this.checkEvery = checkEvery;
26 this.checkEveryMs = this.checkEvery * 60000;
27 this.load();
28 if (checkEvery) {
29 this.watch();
30 }
31 }
32 Ftp.prototype.load = function () {
33 var ftp = this;
34 try {
35 ftp.client.connect(ftp.config);
36 ftp.client.ls("/", function (err, list) {
37 ftp.e.log(1, "FTP ls found " + list.length + " files.");
38 // Download and insert new Job
39 list.forEach(function (file, index) {
40 // Create temp file
41 tmp.file(function _tempFileCreated(err, temp_path, fd, cleanupCallback) {
42 if (err)
43 throw err;
44 ftp.e.log(1, "FTP is downloading file \"" + file.name + "\".");
45 ftp.client.download(file.name, temp_path, function (err) {
46 if (err) {
47 ftp.e.log(3, "FTP error: \"" + err + "\".");
48 }
49 });
50 var job = new fileJob_1.FileJob(ftp.e, temp_path);
51 //job.setPath(temp_path);
52 ftp.arrive(job);
53 // If we don't need the file anymore we could manually call the cleanupCallback
54 // But that is not necessary if we didn't pass the keep option because the library
55 // will clean after itself.
56 cleanupCallback();
57 });
58 });
59 });
60 }
61 catch (e) {
62 ftp.e.log(3, e);
63 }
64 };
65 Ftp.prototype.watch = function () {
66 var ftp = this;
67 ftp.e.log(1, "Watching FTP directory.");
68 var count = 0;
69 setInterval(function () {
70 count++;
71 ftp.e.log(1, "Re-checking FTP, attempt " + count + ".");
72 ftp.load();
73 }, ftp.checkEveryMs, count);
74 };
75 Ftp.prototype.arrive = function (job) {
76 _super.prototype.arrive.call(this, job);
77 };
78 return Ftp;
79}(nest_1.Nest));
80exports.Ftp = Ftp;