UNPKG

4.32 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 ftpFileJob_1 = require("./../job/ftpFileJob");
9var EasyFtp = require("easy-ftp"), tmp = require("tmp"), fs = require("fs"), async = require("async");
10var FtpNest = (function (_super) {
11 __extends(FtpNest, _super);
12 function FtpNest(e, host, port, username, password, checkEvery) {
13 _super.call(this, e, host);
14 this.config = {
15 host: host,
16 port: port,
17 username: username,
18 password: password
19 };
20 this.checkEvery = checkEvery;
21 this.checkEveryMs = this.checkEvery * 60000;
22 }
23 FtpNest.prototype.getClient = function () {
24 return new EasyFtp();
25 };
26 FtpNest.prototype.load = function () {
27 var ftp = this;
28 try {
29 var ftp_client_1 = ftp.getClient();
30 ftp_client_1.connect(ftp.config);
31 ftp_client_1.ls("/", function (err, list) {
32 if (err) {
33 ftp_client_1.close();
34 }
35 ftp.e.log(1, "FTP ls found " + list.length + " files.", ftp);
36 async.eachSeries(list, function (file, done) {
37 // Create temp file
38 ftp.e.log(1, "FTP found file \"" + file.name + "\".", ftp);
39 var job = new ftpFileJob_1.FtpFileJob(ftp.e, file.name);
40 // Download to the temp job location
41 ftp_client_1.download(file.name, job.getPath(), function (err) {
42 if (err) {
43 ftp.e.log(3, "Download error: \"" + err + "\".", ftp);
44 done();
45 }
46 else {
47 job.setLocallyAvailable(true);
48 // Delete on success
49 ftp_client_1.rm(file.name, function (err) {
50 if (err) {
51 ftp.e.log(3, "Remove error: \"" + err + "\".", ftp);
52 }
53 ftp.arrive(job);
54 done();
55 });
56 }
57 });
58 }, function (err) {
59 if (err) {
60 ftp.e.log(3, "Async series download error: \"" + err + "\".", ftp);
61 }
62 ftp.e.log(0, "Completed " + list.length + " synchronous download(s).", ftp);
63 ftp_client_1.close();
64 });
65 //
66 // list.forEach(function (file, index) {
67 //
68 // });
69 });
70 }
71 catch (e) {
72 ftp.e.log(3, e, ftp);
73 }
74 };
75 FtpNest.prototype.watch = function () {
76 var ftp = this;
77 ftp.e.log(1, "Watching FTP directory.", ftp);
78 var count = 0;
79 setInterval(function () {
80 count++;
81 ftp.e.log(1, "Re-checking FTP, attempt " + count + ".", ftp);
82 ftp.load();
83 }, ftp.checkEveryMs, count);
84 };
85 FtpNest.prototype.arrive = function (job) {
86 _super.prototype.arrive.call(this, job);
87 };
88 FtpNest.prototype.take = function (job, callback) {
89 var ftp = this;
90 try {
91 var ftp_path = "/" + job.getName();
92 var ftp_client_2 = ftp.getClient();
93 ftp_client_2.connect(ftp.config);
94 ftp_client_2.upload(job.getPath(), ftp_path, function (err) {
95 if (err) {
96 ftp.e.log(3, "Error uploading " + job.getName() + " to FTP.", ftp);
97 }
98 fs.unlinkSync(job.getPath());
99 ftp_client_2.close();
100 var ftpJob = job;
101 ftpJob.setLocallyAvailable(false);
102 callback(ftpJob);
103 });
104 }
105 catch (e) {
106 ftp.e.log(3, "Take upload error, " + e, ftp);
107 }
108 };
109 return FtpNest;
110}(nest_1.Nest));
111exports.FtpNest = FtpNest;