-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (188 loc) · 6.57 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"use strict";
const fs = require("fs");
const path = require("path");
const FtpClient = require("ftp");
const JSZip = require("jszip");
const mkdirp = require("mkdirp");
const cisjr = "ftp.cisjr.cz";
const jdfPath = "JDF";
const jdfFileName = "JDF.zip";
const localDataPath = "data";
const localJdfPath = path.join(localDataPath, jdfFileName);
const remoteJdfPath = path.posix.join(jdfPath, jdfFileName);
const linePrefix = "100";
const myLines = ["103", "348", "368", "369", "514"];
const mkdir = (dir) => {
return new Promise((resolve, reject) => {
mkdirp(dir, function (err, made) {
if (err) {
reject(err);
}
else {
resolve(made);
}
});
});
};
const indexOfJdf = (list) => {
for (let i = 0; i < list.length; i++) {
if (list[i].name === jdfFileName) {
return i;
}
}
return -1;
};
const getFileStat = (thepath) => {
return new Promise((resolve) => {
fs.stat(thepath, (err, stats) => {
if (err) {
resolve(null);
}
else {
resolve(stats);
}
});
});
};
const getJdf = (localStat) => {
return new Promise((resolve) => {
const ftp = new FtpClient();
ftp.on("error", (err) => {
ftp.end();
console.log(err);
resolve(localStat);
});
ftp.on("ready", () => {
ftp.list(jdfPath, (err, list) => {
if (err) {
ftp.end();
console.log(err);
resolve(localStat);
}
else {
const index = indexOfJdf(list);
if (index >= 0) {
const remoteStat = list[indexOfJdf(list)];
if (localStat
&& (localStat.size === parseInt(remoteStat.size)
&& localStat.mtime.getTime() == remoteStat.date.getTime())) {
ftp.end();
resolve(localStat);
}
else {
ftp.get(remoteJdfPath, (err, stream) => {
if (err) {
ftp.end();
console.log(err);
resolve(localStat);
}
else {
stream.once("close", () => {
ftp.end();
});
stream.pipe(fs.createWriteStream(localJdfPath).once("close", () => {
fs.utimesSync(localJdfPath, new Date, remoteStat.date);
resolve(getFileStat(localJdfPath));
}));
}
});
}
}
else {
ftp.end();
resolve(localStat);
}
}
});
});
ftp.connect({
host: cisjr
});
});
};
const unzipJdf = (stats) => {
return new Promise((resolve, reject) => {
if (stats) {
const jszip = new JSZip();
fs.readFile(localJdfPath, function (err, data) {
if (err) {
reject(err);
}
;
resolve(jszip.loadAsync(data));
});
}
else {
reject("Error: The system cannot find the file specified.");
}
});
};
const unzipFile = (file) => {
return new Promise((resolve, reject) => {
file.async("nodebuffer").then(function success(content) {
const jszip = new JSZip();
jszip.loadAsync(content).then((zip) => {
const files = zip.files;
files["Linky.txt"].async("string").then((line) => {
const lines = myLines.map((el) => linePrefix + el);
line = line.split(",")[0].replace(/"/g, "");
if (lines.indexOf(line) >= 0) {
const folder = path.basename(file.name, path.extname(file.name));
const destination = path.join(localDataPath, jdfPath, folder);
mkdir(destination).then(() => {
const promises = [];
for (let key in files) {
if (files.hasOwnProperty(key)) {
const f = files[key];
const promise = new Promise((resolve, reject) => {
f["nodeStream"]()
.pipe(fs.createWriteStream(path.join(destination, f.name)))
.on("finish", () => {
resolve(f.name);
})
.on("error", (e) => {
reject(e);
});
});
promises.push(promise);
}
}
Promise.all(promises).then(() => {
resolve(file.name);
});
}).catch((e) => {
reject(e);
});
}
else {
resolve(null);
}
});
});
}, function error(e) {
reject(e);
});
});
};
const unzipFiles = (zip) => {
return new Promise((resolve, reject) => {
const files = zip.files;
const promises = [];
for (let key in files) {
if (files.hasOwnProperty(key)) {
const file = files[key];
promises.push(unzipFile(file));
}
}
resolve(Promise.all(promises));
});
};
mkdir(localDataPath).then(() => localJdfPath).then(getFileStat).then(getJdf).then(unzipJdf).then(unzipFiles).then((lines) => {
lines.filter((el) => !!el).forEach(el => {
console.log(el);
});
}).catch((e) => {
setTimeout(() => {
throw e;
});
});