-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoSave.js
89 lines (82 loc) · 3.65 KB
/
autoSave.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
const axios = require('axios')
const fs = require('fs')
class autoSaver {
constructor() {
this.token = null
this.config = {}
}
generateToken = async () => {
const data = {
username: process.env.CONVERGENCE_USERNAME,
password: process.env.CONVERGENCE_PASSWORD
}
try {
const res = await axios.post(`${process.env.OT_SERVER_URL}/rest/auth/login`, data)
this.token = res.data.body.token
this.config = {
headers: {
"Authorization": `SessionToken ${this.token}`
}
}
}catch (e) {
console.error(`AutoSave Error : Convergence login error`)
}
}
save = async (projectId) => {
let res;
try {
res = await axios.get(`${process.env.OT_SERVER_URL}/rest/domains/convergence/default/models/${projectId}`, this.config)
} catch (e) {
if(e.response && (e.response.status === 401 || e.response.status === 403)){
await this.generateToken()
try{
res = await axios.get(`${process.env.OT_SERVER_URL}/rest/domains/convergence/default/models/${projectId}`, this.config)
} catch (e) {
console.error(`AutoSave Error : Convergence generated token not accepted for roomID ${projectId}`)
return;
}
} else {
console.error(`AutoSave Error : Convergence API error occurred for roomID ${projectId}`)
return
}
}
this.saveRec(res.data.body.data["tree"], projectId, `${process.env.USER_DATA_BASE_PATH}/${projectId}`, "root").then()
}
saveRec = async (dataTree, projectId, basePath, dir) => {
const files = dataTree.nodes[dir].children;
// Create directory if it does not exist
if (!fs.existsSync(basePath))
fs.mkdirSync(basePath)
files.map(async (file) => {
// If node-data has children, it's a directory so call saveRec recursively else save file
if (dataTree.nodes[file].children) {
this.saveRec(dataTree, projectId, `${basePath}/${dataTree.nodes[file].name}`, file).then()
} else {
let fileRes;
try {
fileRes = await axios.get(`${process.env.OT_SERVER_URL}/rest/domains/convergence/default/models/${file}`, this.config)
} catch (e) {
if(e.response && (e.response.status === 401 || e.response.status === 403)){
await this.generateToken()
try{
fileRes = await axios.get(`${process.env.OT_SERVER_URL}/rest/domains/convergence/default/models/${file}`, this.config)
} catch (e) {
console.error(`AutoSave Error : Convergence generated token not accepted for roomID ${projectId}`)
return;
}
} else {
console.error(`AutoSave Error : Convergence API error occurred for roomID ${projectId}`)
}
}
const filename = dataTree.nodes[file].name
fs.writeFile(`${basePath}/${filename}`, fileRes.data.body.data.content + "\n", (err) => {
if (err)
console.log(err)
else
console.log(`AutoSave : Saved file ${basePath.split(process.env.USER_DATA_BASE_PATH)[1].substr(1)}/${filename}`)
})
}
})
}
}
module.exports = autoSaver