-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftp-uploader.js
143 lines (130 loc) · 3.08 KB
/
ftp-uploader.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use strict";
var Ftp = require("ftp");
var fs = require("fs");
var path = require("path");
var FtpUploader = function(config) {
this.config = config;
this.client = new Ftp({
host: config.auth.host,
port: config.auth.port,
connTimeout: 600000 // 10 minutes
});
};
FtpUploader.prototype.ping = function() {
var self = this;
this._connect(function() {
self._log("FTP connection OK");
self.stop();
});
};
FtpUploader.prototype.start = function() {
var self = this;
this._connect(function() {
self.queue = [];
var processedDirs = {};
var dirs = [];
var files = [];
var recursiveAdd = function(dir) {
var name = "/";
var parts = dir.split("/");
// remove first and last elements because they are empty
parts.pop();
parts.shift();
parts.map(function(n) {
name += n + "/";
if (!processedDirs[name]) {
processedDirs[name] = true;
dirs.push(name);
}
});
};
self.config.uploads.map(function(upload) {
upload.src.sort();
upload.src.map(function(name) {
var destName = upload.dest + name;
files.push({
"src": upload.cwd + name,
"dest": destName
});
recursiveAdd(path.dirname(destName) + "/");
});
});
dirs.sort();
dirs.map(function(dir) {
self.enqueue("makeDir", dir);
});
files.map(function(file) {
self.enqueue("uploadFile", file);
});
self.currentStep = 1;
self.totalSteps = self.queue.length;
self._next();
});
};
FtpUploader.prototype.stop = function(success) {
this.client.end();
this.config.complete(success);
};
FtpUploader.prototype.enqueue = function() {
var args = Array.prototype.slice.call(arguments, 0);
var name = args.shift();
this.queue.push(function() {
this["_" + name].apply(this, args);
});
};
FtpUploader.prototype._connect = function(callback) {
var self = this;
this.client.on("connect", function() {
self.client.auth(self.config.auth.user, self.config.auth.password, function(err) {
if (err) {
self._error(err.message);
return;
}
callback && callback();
});
});
this.client.connect();
};
FtpUploader.prototype._uploadFile = function(file) {
var self = this;
this._log(this.currentStep++ + "/" + this.totalSteps + ": " + file.dest);
if (self.config.debug) {
this._next();
return;
}
this.client.put(fs.createReadStream(file.src), file.dest, function(err) {
if (err) {
self._error(err.message);
} else {
self._next();
}
});
};
FtpUploader.prototype._makeDir = function(name) {
var self = this;
this._log(this.currentStep++ + "/" + this.totalSteps + ": " + name);
if (self.config.debug) {
this._next();
return;
}
this.client.mkdir(name, function() {
self._next();
});
};
FtpUploader.prototype._next = function() {
if (!this.queue.length) {
this.stop();
return;
}
this.queue.shift().call(this);
};
FtpUploader.prototype._log = function(text) {
var log = this.config.logger && this.config.logger.log || function() {};
log(text);
};
FtpUploader.prototype._error = function(text) {
var error = this.config.logger && this.config.logger.error || function() {};
error(text);
this.stop(false);
};
module.exports = FtpUploader;