forked from Bignotto/NFe2Ftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.js
123 lines (103 loc) · 3.81 KB
/
robot.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
const fs = require('fs')
const path = require('path')
const ftp = require('basic-ftp')
const _config = require('./config')
async function start() {
const content = {}
let timeDate = new Date();
let currentDate = timeDate.getFullYear()+'-'+(timeDate.getMonth()+1)+'-'+timeDate.getDate();
let currentTime = timeDate.getHours() + ":" + timeDate.getMinutes() + ":" + timeDate.getSeconds();
//console.log(currentDate+' '+currentTime)
//check for new files
content.files = getXmlFilesList()
content.path = _config.originFolder
if(content.files.length === 0) {
console.log(currentDate + ' ' + currentTime + ': no XML files found on ' + _config.originFolder)
return
} else {
console.log(currentDate + ' ' + currentTime + ': found ' + content.files.length + ' XML files')
}
//conect to the FTP server
const connection = await conectFtpServer()
//upload all xml files to FTP server
await upoladFiles()
//validate if all files were uploaded
if (await validateUploadedFiles() !== true) {
await connection.close()
console.log('Some file were not uploaded.')
return
} else {
console.log('all files ok!!')
}
//deleteUploadedFiles
await deleteUploadedFiles()
await connection.close()
//list all XML files to upload to FTP and return an array with their names
function getXmlFilesList() {
var xmlFiles = [];
//XML match pattern
var pattern=/\.[0-9a-z]+$/i;
try {
xmlFiles = fs.readdirSync(_config.originFolder)
} catch (err) {
console.error(err)
}
//keep only files with .XML extension
return xmlFiles.filter(function(fileName) {
var ext = fileName.match(pattern);
if(ext.toString().toLowerCase() == '.xml') return true
else false
})
}
//connects to the FTP server and return a connection
async function conectFtpServer() {
const client = new ftp.Client()
client.ftp.verbose = true
try {
await client.access({
host: _config.ftpServer,
user: _config.ftpUser,
password: _config.ftpPass,
secure: false
})
return client
} catch (error) {
console.error(error)
}
}
//send files in the array to the ftp server
async function upoladFiles() {
await connection.ensureDir(_config.destinationFolder)
for(i = 0;i<content.files.length;i++) {
await connection.upload(fs.createReadStream(content.path + '/' + content.files[i]),content.files[i])
}
}
//validate if all files were uploaded
async function validateUploadedFiles() {
await connection.ensureDir(_config.destinationFolder)
let filesList = await connection.list()
console.log('FTP server: ' + filesList.length + ' XML found')
//validade number of files
if(filesList.length !== content.files.length)
return false
//validade file names on both local folder and ftp folder
for(i = 0;i<filesList.length;i++) {
if(content.files.indexOf(filesList[i].name,0)===-1) {
return false
}
}
return true
}
async function deleteUploadedFiles() {
await fs.readdir(_config.originFolder, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(_config.originFolder, file), err => {
if (err) throw err;
});
}
});
}
}
//5 minutes
setInterval(start,300 * 1000)