-
Notifications
You must be signed in to change notification settings - Fork 0
/
metafileDownloader.js
128 lines (120 loc) · 3.53 KB
/
metafileDownloader.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
/**
* @license MIT
* @author aAXEe (https://github.com/aAXEe)
*/
'use strict'
const FtpClient = require('ftp')
const async = require('async')
const fs = require('fs')
const mkdirp = require('mkdirp')
const path = require('path')
const c = new FtpClient()
// default callback handler
let callback = (err, filelist) => {
if (err) throw err
console.log(`downloaded ${filelist.length} files:`, filelist)
}
// default config
let config = {
protocol: 'ftp://',
host: 'localhost', // the ftp host
remotePath: '/', // the path on the ftp server
filterExtension: '.json', // file extension of the files to download
downloadPath: './download', // local path for downloaded files
metafilePath: './meta' // local path to write meta data for files
}
const writeMetaData = (config, entry, callback) => {
const meta = {
ftpData: entry,
config: config,
dataFile: path.resolve(config.downloadPath + '/' + entry.name)
}
const outName = config.metafilePath + '/' + entry.name + '.json'
fs.writeFile(
outName,
JSON.stringify(meta),
(err) => {
if (err) {
callback(err)
return
}
console.log(`wrote meta data "${outName}"`)
callback()
}
)
}
c.on('ready', function () {
console.log(`successfully connected .. trying to get the file list`)
// change ftp path
c.cwd(config.remotePath, (err) => {
if (err) {
callback(err)
return
}
// get file list
c.list(function (err, list) {
if (err) {
callback(err)
return
}
// get all filenames for meta files (filtered by extension)
let metafiles = []
list.forEach((entry) => {
const name = entry.name
if (!name.endsWith(config.filterExtension)) return
metafiles.push(entry)
})
const numberOfFiles = metafiles.length
console.log(`found ${numberOfFiles} files on server .. going to download them into "${config.downloadPath}".`)
let filesFinished = 0
// download all meta files
async.forEach(
metafiles,
(entry, callback) => {
// download data via ftp
c.get(entry.name, (err, dataStream) => {
if (err) {
callback(err)
return
}
// write data to local file
const writePath = config.downloadPath + '/' + entry.name
const writeStream = fs.createWriteStream(writePath)
writeStream.on('finish', () => {
writeMetaData(config, entry, (err) => {
if (err) {
callback(err)
return
}
filesFinished++
console.log(`finished file ${filesFinished} / ${numberOfFiles}: "${entry.name}"`)
callback()
})
})
writeStream.on('error', (err) => {
filesFinished++
console.log(`errored at file ${filesFinished} / ${numberOfFiles}: "${entry.name}"`)
callback(err)
})
dataStream.pipe(writeStream)
})
},
(err) => {
c.end()
console.log('finished downloading')
callback(err, metafiles)
}
)
})
})
})
module.exports = (ftpConfig, callbackFunc) => {
// save parameters
if (callbackFunc) callback = callbackFunc
Object.assign(config, ftpConfig)
mkdirp.sync(config.downloadPath)
mkdirp.sync(config.metafilePath)
// kick of connecting and loading (see above)
console.log(`going to connect to "${config.host}"`)
c.connect(ftpConfig)
}