-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTS.js
533 lines (451 loc) · 17.3 KB
/
HTS.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
const axios = require('axios')
const https = require ('https')
const qs = require('query-string')
const fs = require ('fs')
const path = require ('path')
const isurl = require('is-url')
const _ = require('lodash')
const sftp = require('ssh2-sftp-client');
const tmp = require('tmp');
const isValidPath = require('is-valid-path');
const streamWrapper = require('through2');
const Duplex = require('stream').Duplex;
const mime = require('mime');
const util= require('util')
const HOSTNAME=process.env.HTS_HOSTNAME
const CID=process.env.HTS_CID
const PASS=process.env.HTS_PASS
const TMKEY = process.env.HTS_TMKEY
const SFTP_ENDPOINT = process.env.SFTP_ENDPOINT
const SFTP_PORT = (typeof process.env.SFTP_PORT !== 'undefined') ? process.env.SFTP_PORT : 22
const SFTP_USERNAME = process.env.SFTP_USERNAME
const SFTP_PASSWORD = process.env.SFTP_PASSWORD
const HTTP_ENDPOINT = process.env.HTTP_ENDPOINT
const DATAFOLDER = 'data'
const LANGUAGESFOLDER = 'languages'
class HTS {
constructor() {
this.cid = CID
this.password = PASS
this.languagesList = null
if (!fs.existsSync(path.join(__dirname, DATAFOLDER, LANGUAGESFOLDER))){
fs.mkdirSync(path.join(__dirname, DATAFOLDER, LANGUAGESFOLDER),{recursive: true})
}
this._loadLangagesList().then(() => {
//console.log (this.languagesList)
})
}
/* UTILS */
_isUrl(url) {
return isurl(url)
}
_formatLanguageList (json_list) {
let list = { name: [], iso3066:[], iso6391:[]}
Object.values(json_list)
.filter ((lang) => {
//console.log("NAME ", lang.name, typeof (lang.name) === 'undefined')
return typeof(lang.name) !== 'undefined'
})
.map ((lang) => {
list.name.push(lang.name)
list.iso3066.push(lang.rfc3066)
list.iso6391.push(lang.iso6391)
})
return list
}
/* VALIDATORS */
_getDefaultParameters (){
let parameters = {}
parameters.cid = this.cid
parameters.p = this.password
parameters.of = 'json'
parameters.verbose = 'true'
return parameters
}
_validateLanguageListParameter(){
let parameters = this._getDefaultParameters()
parameters.f = 'll'
return parameters
}
_validateQuoteParams(source_language, target_languages, pn, jt, df, words, text, delivery_endpoint, tm, subject, instructions) {
if (!source_language) {
throw new Error("Missing source language")
}
if (!target_languages) {
throw new Error("Missing target languages")
}
if (words && text) {
throw new Error("Use only one between 'w' and 'text'. ")
}
if (!words && !text) {
throw new Error("Missing 'w' (words) or 'text' (text to be translated) ")
}
if (!text && words && !_.isInteger(words)) {
throw new Error("Invalid 'w' (words) parameter. Interger expected. Example w=1000 ")
}
if (!['T', 'R', 'P'].includes(jt)) {
throw new Error("Invalid job type (T=Translation, R=Revision, P=Postediting are the allowed values)")
}
if (delivery_endpoint && !this._isUrl(delivery_endpoint)) {
throw new Error("delivery parameter can only contain a valid public url. Example: delivery =https://site.com/receiveTranslation")
}
if (_.isArray(target_languages)){
// HTS currently dows not support multiple target as array. It only support csv list of langages
target_languages = target_languages.join()
}
let parameters = this._getDefaultParameters()
parameters.f = 'quote'
parameters.s = source_language
parameters.t = target_languages
if (pn) {
parameters.pn = pn
}
if (jt) {
parameters.jt = jt
}
if (df) {
parameters.df = df
}
if (delivery_endpoint) {
parameters.delivery = delivery_endpoint
}
if (tm) {
parameters.tm = tm
}
if (subject) {
parameters.subject = subject
}
if (words) {
parameters.w = words
}
if (text) {
parameters.text = text
}
if (instructions) {
parameters.instructions = instructions
}
return parameters
}
_validateConfirmParams(pid = 0) {
if (!_.isInteger(pid) || pid === 0) {
throw new Error("Missing pid (project id)")
}
let parameters = this._getDefaultParameters()
parameters.f = 'confirm'
parameters.c = 1
parameters.pid = pid
return parameters
}
_validateStatusParams(pid = 0) {
if (!_.isInteger(pid) || pid === 0) {
throw new Error("Missing pid (project id)")
}
let parameters = this._getDefaultParameters()
parameters.f = 'status'
parameters.pid = pid
return parameters
}
async _post(params){
return new Promise(async(resolve, reject) => {
try {
// console.log("params ", params)
let res = await axios.post(HOSTNAME, qs.stringify(params, {arrayFormat: 'bracket', timeout: 300000}))
if (res.data.code == 1) {
resolve(res.data)
} else {
console.log ('HTS library _post reported HTS error ', res)
if (res.data.message) {
reject(res.data.message)
} else if (res.data) {
reject(res.data)
} else {
reject(res)
}
}
} catch (error) {
console.log ('HTS library _post reported throwed error ', error)
reject (error)
}
})
}
async quote(source_language, target_languages = [], pn = '', jt = 'T', df = 'txt', words = 0, text = "", delivery_endpoint = HTTP_ENDPOINT, tm = TMKEY, subject = "", instructions = "") {
let params = this._validateQuoteParams(source_language, target_languages, pn, jt, df, words, text, delivery_endpoint, tm, subject, instructions)
return await this._post(params)
}
async confirm(pid = 0) {
let params = this._validateConfirmParams(pid)
return await this._post(params)
}
async status(pid = 0, structureOutput=false) {
let params = this._validateStatusParams(pid)
let status = await this._post(params)
return (structureOutput ? this._formatStatusEndpointOutput(status): status)
}
async getSupportedLanguagesList (){
return this.languagesList
}
async get_documentation_url() {
return "https://translated.com/translation-api-specs"
}
async _storeLanguagesList() {
let params = this._validateLanguageListParameter()
let list = await this._post(params)
let languages_list = this._formatLanguageList(list)
fs.writeFileSync(path.join(__dirname, DATAFOLDER, LANGUAGESFOLDER, 'languages.json'), JSON.stringify(languages_list), "utf8")
return languages_list
}
async _loadLangagesList(){
if (!fs.existsSync(path.join(__dirname, DATAFOLDER, LANGUAGESFOLDER, 'languages.json'))){
this.languagesList = await this._storeLanguagesList()
}
this.languagesList = JSON.parse(fs.readFileSync(path.join(__dirname, DATAFOLDER, LANGUAGESFOLDER, 'languages.json'), "utf8"))
}
//HELPERS
_formatStatusEndpointOutput(result) {
let res = {}
res['summary'] = { code: result.code, message: result.message, num_jobs: result.count }
delete result.code
delete result.message
delete result.count
res['details'] = {}
// look for translation objects
Object.keys(result).forEach((key) => {
//console.log("key is ", key)
if (result[key].type == 'TRANSLATION') {
let obj = _.cloneDeep(result[key])
let t = obj.target
let jid = obj.jid
delete obj.type
delete obj.jid
delete obj.target
delete obj.id_job_revising //alwais zero in case of translation
obj.revisions = {}
if (!(t in res.details)) {
res.details[t] = {}
}
res.details[t][jid] = obj
//reduce object
delete result[key]
}
})
// look for revision objects
Object.keys(result).forEach((key) => {
//console.log("key is ", key)
if (result[key].type == 'REVISION' && result[key].id_job_revising>0) {
let obj = _.cloneDeep(result[key])
let t = obj.target
let jid = obj.jid
let id_job_revising = obj.id_job_revising
delete obj.type
delete obj.jid
delete obj.target
delete obj.id_job_revising
if (!(id_job_revising in res.details[t])) {
res.details[t][id_job_revising] = {}
res.details[t][id_job_revising].revisions = {}
}
res.details[t][id_job_revising].revisions[jid] = obj
//reduce object
delete result[key]
}
})
// console.log ("res1" , util.inspect(res, {showHidden: false, depth: null}), "------")
return res
}
lang_iso6391FromName(langName = 'English') {
let index = _.indexOf(this.languagesList.name, langName)
if (index !== -1) {
return this.languagesList.iso6391[index]
}
return null
}
lang_iso3066FromName(langName = 'English') {
let index = _.indexOf(this.languagesList.name, langName)
if (index !== -1) {
return this.languagesList.iso3066[index]
}
return null
}
lang_nameFromIso(isoCode = 'en') {
let index = _.indexOf(this.languagesList.iso3066, isoCode)
if (index === -1) {
index = _.indexOf(this.languagesList.iso6391, isoCode)
}
if (index !== -1) {
return this.languagesList.name[index]
}
return null
}
isSupportedLang (lang){
if (!lang){
return null
}
let index = 0
index = _.indexOf(this.languagesList.iso6391, lang)
if (index === -1){
index = _.indexOf(this.languagesList.iso3066, lang)
if (index === -1) {
index = _.indexOf(this.languagesList.name, lang)
}
}
if (index ===-1) {
return null
}
return { name: this.languagesList.name[index], iso6391: this.languagesList.iso6391[index], iso3066: this.languagesList.iso3066[index], }
}
// DOWNLOAD DELIVERED
async _streamToResult (responseStream , savePath, enc='utf8'){
//console.log(typeof responseStream)
let writer = new Object()
if (savePath) {
if (isValidPath(savePath)) {
writer = fs.createWriteStream(savePath)
} else {
throw new Error(`invalid save path ${savePath}`)
}
} else {
writer = streamWrapper()
}
responseStream.pipe(writer)
return new Promise((resolve, reject) => {
let responseString = ''
writer.on('finish', () => {
if (!savePath) {
resolve({ 'content': responseString })
} else {
resolve(true)
}
})
writer.on('error', reject)
writer.on('data', (data) => {
if (!savePath) {
console.log("<< ", data)
responseString += (typeof enc === 'string') ? data.toString(enc) : data.toString()
}
})
})
}
async _getSFTPClient (){
const config = {
host: SFTP_ENDPOINT,
port: SFTP_PORT,
username: SFTP_USERNAME
};
if (SFTP_PASSWORD) {
config.password = SFTP_PASSWORD
} else {
if (!fs.existsSync(path.join(process.cwd(), '/config/hts_sftp_endpoint.key'))) {
throw new Error("SFTP Error : neither password or pub key provided ")
}
config.privateKey = fs.readFileSync(path.join(process.cwd(), '/config/hts_sftp_endpoint.key'))
}
let sftp_client = new sftp()
await sftp_client.connect(config)
return sftp_client
}
async renameDeliveredSFTP(ftp_srcpath, ftp_dstpath = ''){
let sftp_client = await this._getSFTPClient()
let path_type_src = await sftp_client.exists(ftp_srcpath)
let path_type_dst = await sftp_client.exists(ftp_dstpath)
if (!path_type_src) {
let error = new Error(`SFTP Error : the path ${ftp_srcpath} does not exists`)
error.name = "ftp_file_not_exists"
await sftp_client.end();
throw error
}
if (path_type_src !== '-') { // d => folder ; - => file ; l => link
let error = new Error(`SFTP Error : the path ${ftp_srcpath} is not a regular file (type is ${path_type_src})`)
error.name = "ftp_regular_file_expected"
await sftp_client.end();
throw error
}
if (!path_type_dst) {
if (!sftp_client.exists(path.dirname(ftp_dstpath))){
let error = new Error(`SFTP Error : the path ${ftp_dstpath} does not exists`)
error.name = "ftp_file_not_exists"
await sftp_client.end();
throw error
}
}else {
if (path_type_dst === 'd'){
ftp_dstpath = path.join(ftp_dstpath, path.basename(ftp_srcpath)) // if folder is passed as dst path, than the moved file will have the same name as the source file
}
}
await sftp_client.rename(ftp_srcpath, ftp_dstpath)
await sftp_client.end();
}
async getDeliveredSFTPFileList(ftp_filepath) {
let sftp_client = await this._getSFTPClient()
let path_parts = path.parse(ftp_filepath)
let path_to_check = ftp_filepath
let pattern = path_parts.base
if (pattern.includes("*")) {
path_to_check = path_parts.dir.includes("*") ? '' : path_parts.dir
}
let path_type = await sftp_client.exists(path_to_check)
if (!path_type) {
let error = new Error(`SFTP Error : the path ${path_to_check} does not exists`)
error.name = "ftp_file_not_exists"
await sftp_client.end()
throw error
}
if (path_type !== 'd') { // d => folder ; - => file ; l => link
let error = new Error(`SFTP Error : the path ${ftp_filepath} is not a regular file (type is ${path_type})`)
error.name = "ftp_folder_expected"
await sftp_client.end()
throw error
}
let list = await sftp_client.list(path_to_check, pattern)
await sftp_client.end();
let finalList = list.map((file) => {
return {'name':file.name, 'type': file.type}
})
return finalList
}
async getDeliveredSFTP(ftp_filepath, savePath = '', enc = 'utf8') {
let sftp_client = await this._getSFTPClient()
// console.log (await sftp_client.list("."))
let path_type = await sftp_client.exists(ftp_filepath)
if (!path_type) {
let error = new Error(`SFTP Error : the path ${ftp_filepath} does not exists`)
error.name = "ftp_file_not_exists"
await sftp_client.end()
throw error
}
if (path_type !== '-') { // d => folder ; - => file ; l => link
let error = new Error(`SFTP Error : the path ${ftp_filepath} is not a regular file (type is ${path_type})`)
error.name = "ftp_regular_file_expected"
await sftp_client.end()
throw error
}
let stream = new Duplex();
let contentBuffer = await sftp_client.get(ftp_filepath)
stream.push(contentBuffer)
stream.push(null) // to end the streaming
await sftp_client.end()
let content = await this._streamToResult(stream, savePath, enc)
if (!savePath && _.isObject(content)) {
content.extension = path.extname(ftp_filepath)
}
return content
}
async getDeliveredHTTPS(url, savePath='', enc='utf8') {
const response = await axios({
url: url,
method: 'GET',
responseType: 'stream'
})
try {
let content = await this._streamToResult(response.data,savePath, enc)
if (!savePath && _.isObject(content)) {
content.extension = mime.getExtension(response.headers['content-type'])
}
return content
} catch (e){
console.error(e)
throw(e)
}
}
// DOWNLOAD DELIVERED END
}
exports.HTS = HTS