-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
88 lines (63 loc) · 2.2 KB
/
server.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
const {spawn} = require('child_process');
const fs = require('fs');
const path = require('path');
//const createPath = (core, ...args) => path.resolve(core.options.root, 'vfs', req.session.user.username, ...args);
const createPath = (core, user, ...args) => path.resolve(core.options.root, 'vfs/', user, ...args);
const runcmd = (core, cmd, user, respond, file) => new Promise((resolve, reject) => {
console.log("cmd:" + cmd);
console.log("user:" + user);
console.log("file.path:" + file.path);
if (cmd === "lilypond") {
const tmpFilename= file.path.replace(/^.*[\\\/]/, '');
const pdfFile= tmpFilename.replace(/\..+$/, '') + ".pdf";
const pdfPath= file.path.replace(/\..+$/, '') + ".pdf";
const realOutput = {path: pdfPath, filename: pdfFile};
const i = createPath(core, user, file.path.replace(/^home:\//, ''));
// const o= i.replace(/\..+$/, '') + ".pdf";
// const o= i.replace(/\..+$/, '') + ".pdf";
const o = i.substr(0, i.lastIndexOf(".")) + ".pdf";
console.log(cmd, [
'-o',
o,
'--pdf',
i
]);
var p = spawn(cmd, [
'-o',
o.replace(/\.pdf$/, ''),
'--pdf',
i
]);
}
p.stdout.on('data', data => respond(data.toString()));
p.stderr.on('data', data => respond(data.toString()));
p.on('error', error => reject(error));
p.on('exit', code => {
return !code ? resolve() : reject('Failed');
});
});
module.exports = (core, proc) => ({
// When server initializes
init: async () => {
// HTTP Route example (see index.js)
core.app.post(proc.resource('/test'), (req, res) => {
res.json({hello: 'World'});
});
// WebSocket Route example (see index.js)
// NOTE: This creates a new connection. You can use a core bound socket instead
core.app.ws(proc.resource('/socket'), (ws, req) => {
// ws.send('Hello World');
});
},
// When server starts
start: () => {},
// When server goes down
destroy: () => {},
// When using an internally bound websocket, messages comes here
onmessage: (ws, respond, msg) => {
const {cmd, user, args} = JSON.parse(msg);
// runcmd(core, cmd, user, respond, ...args);
runcmd(core, cmd, user, respond, ...args);
// respond('Internal socket');
}
});