This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
move-file.js
255 lines (246 loc) · 7.87 KB
/
move-file.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
module.exports = function (args) {
const fs = require('fs')
const path = require('path')
const tmpDir = 'tmp'
const rimraf = require('rimraf')
let noClobber = true
if (args.options['no-clobber'] === 'false' || process.env.NO_CLOBBER === 'false'){
noClobber = false
}
const sftsHost =
args.options['sfts-host'] ||
process.env.SFTS_HOST ||
'filetransfer.gov.bc.ca'
const sftsUser =
args.options['sfts-user'] || process.env.SFTS_USER
const sftsPassword =
args.options['sfts-password'] || process.env.SFTS_PASSWORD
const sftsFolder =
args.options['sfts-folder'] || process.env.SFTS_FOLDER || '/'
const s3Bucket =
args.options['s3-bucket'] || process.env.S3_BUCKET
const s3PathPrefix =
args.options['s3-path-prefix'] || process.env.S3_PATH_PREFIX
const awsAccessKeyId =
args.options['aws-access-key-id'] || process.env.AWS_ACCESS_KEY_ID
const awsSecretAccessKey =
args.options['aws-secret-access-key'] || process.env.AWS_SECRET_ACCESS_KEY
const concurrency =
args.options['concurrency'] || process.env.CONCURRENCY || 10
const mode = args.options['mode'] || process.env.MODE || 'mv'
const mv = ( mode === 'mv' )
const queue = require('async/queue')
const AWS = require('aws-sdk')
AWS.config.update({
accessKeyId: awsAccessKeyId,
secretAccessKey: awsSecretAccessKey,
})
const s3 = new AWS.S3({
apiVersion: '2019-11-13',
})
const { spawn } = require('child_process')
async function goToSftsFolder() {
return new Promise((resolve, reject) => {
const xfer = spawn(
'java',
[
'-classpath',
`${path.join(__dirname, 'xfer', 'xfer.jar')}${
path.delimiter
}${path.join(__dirname, 'xfer', 'jna.jar')}`,
'xfer',
`-user:${sftsUser}`,
`-password:${sftsPassword}`,
sftsHost,
],
{ cwd: path.join(__dirname, tmpDir) }
)
xfer.stdin.setEncoding('utf-8')
xfer.stdout.setEncoding('utf-8')
let output = ''
xfer.stdout.on('readable', () => {
while ((data = xfer.stdout.read())) {
output += data
}
if (!output.endsWith('> ')) return
if (!output.startsWith(`User ${sftsUser} signed on`)) {
reject('login failed')
}
xfer.stdout.removeAllListeners('readable')
xfer.stdin.write(`cd ${sftsFolder}` + '\n')
output = ''
xfer.stdout.on('readable', () => {
while ((data = xfer.stdout.read())) {
output += data
}
if (!output.endsWith('> ')) return
xfer.stdout.removeAllListeners('readable')
xfer.stderr.removeAllListeners('data')
resolve(xfer)
})
})
xfer.stderr.once('data', (data) => {
reject(data)
})
})
}
// Processes an operation against a file through MOVEit XFer
// The list of possible operations is at:
// https://docs.ipswitch.com/MOVEit/DMZ90/FreelyXfer/MOVEitXferManual.html#commands
function processFile(operation) {
return new Promise(async (resolve, reject) => {
try {
const xfer = await goToSftsFolder()
let output = ''
xfer.stdin.write(operation + '\n')
xfer.stdout.on('readable', () => {
while ((data = xfer.stdout.read())) {
output += data
}
if (!output.endsWith('> ')) return
xfer.stdout.removeAllListeners('readable')
xfer.stdin.end('quit\n')
})
xfer.stderr.once('data', (data) => {
reject(data)
})
xfer.on('close', (code) => {
if (code !== 0) {
return reject(code)
}
resolve(output)
})
} catch (ex) {
reject(ex)
}
})
}
// Return a list of files in S3 under the prefix
function s3FileList() {
return new Promise(async (resolve, reject) => {
try {
s3.listObjectsV2(
{
Bucket: s3Bucket,
Prefix: s3PathPrefix
}, (err, data) => {
if (err) { reject (err) }
else {
let files = []
const regex = /[^/]+$/g;
for (const o of data.Contents) {
let fn = o.Key.match(regex)
if (fn !== null && fn[0] !== ''){
files.push(fn[0])
}
}
resolve(files) }
}
)
}
catch (ex) {
reject(ex)
}
})
}
// Triggered by the call to moveFile() from index.js
return async function () {
// Fetch the files from SFTS
console.info('started processing')
try {
// Create a temporary directory if it doesn't exist
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir)
}
// Store the string response of files in SFTS as a list
let files = await processFile('ls')
files = files.split('\n').slice(0, -1)
if (noClobber) {
// Store the list of files at the S3 folder
let s3Files = await s3FileList()
// Choose the files to move or copy from SFTS to S3
files = files.filter(x => !s3Files.includes(x));
}
if (files.length > 0) {
files = files.reduce((v, e) => ((v[e.trim()] = {}), v), {})
}
for (const fn of Object.keys(files)) {
try {
await processFile(`get ${fn}`)
if (!fs.existsSync(path.join(__dirname, tmpDir, fn))) {
throw new Error('download failed')
}
console.info(`file ${fn} downloaded`)
} catch (ex) {
console.error(`file ${fn} download failed`)
files[fn].downloaded = false
}
}
const q = queue((file, cb) => {
// Upload the file to s3 with the body of the file as a read stream
s3.upload(
{
Bucket: s3Bucket,
Key: s3PathPrefix + `/${file}`,
Body: fs.createReadStream(path.join(__dirname, tmpDir, file)),
},
function (err, data) {
if (err) {
files[file].uploaded = false
console.error(`error uploading file ${file}: ${err}`)
// If a file upload fails, attempt to touch a file of the same prefixed with /bad
s3.upload(
{
Bucket: s3Bucket,
Key: s3PathPrefix + `/bad/${file}`,
Body: `Error transferring file : ${sftsFolder}${file}\n`
+ `Triggered by SFTS user: ${sftsUser}\n`
+ `Timestamp of error: ${Date()}.`
},
function (errBad, dataBad) {
if (errBad){
console.error(`error touching a reference to the `
+ `file under the /bad path on S3: ${errBad}`)
}
}
)
return cb(err)
}
console.info(`uploaded file ${file}`)
return cb()
}
)
}, concurrency)
q.drain(async () => {
// Only drain SFTS folder if the mode is set to `mv`
if (mv) {
for (const fn of Object.keys(files)) {
try {
if (
files[fn].downloaded !== false &&
files[fn].uploaded !== false
) {
await processFile(`delete ${fn}`)
console.info(`file ${fn} deleted`)
}
} catch (ex) {
console.error(`file ${fn} deletion failed`)
}
}
}
// delete the files in tmpDir
rimraf.sync(path.join(__dirname, tmpDir))
console.info('finished processing')
})
const downloadedFiles = Object.keys(files).reduce((a, v) => {
if (files[v].downloaded !== false) {
a.push(v)
}
return a
}, [])
q.push(downloadedFiles)
} catch (ex) {
console.error(`error transfering files: ${ex}`)
}
}
}