-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
127 lines (103 loc) · 3.09 KB
/
index.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
let express = require("express");
let bodyParser = require('body-parser');
let uuidv4 = require("uuid/v4");
let fs = require("fs");
let { spawn } = require("child_process");
let apikeyhelper = require('./apikeyhelper');
let cors = require('cors');
var app = express();
app.use(bodyParser.json());
app.use(cors());
app.use(function(req, res, next) {
"use strict";
if (!req.headers["content-type"] || req.headers["content-type"].toLowerCase() !== "application/json") {
res.status(400).json({ status: 400, error: true, reason: "Content-Type must be application/json" });
return;
}
next();
});
app.use(function(req, res, next) {
"use strict";
if (!apikeyhelper.verifyApiKey(req.headers["x-api-key"])) {
res.status(401).json({ status: 401, error: true, reason: "Unauthorized" });
return;
}
next();
});
// FAIL HELPERS
var failMalformed = function(res) {
res.status(400).json({ status: 400, error: true, reason: "request malformed" });
}
var failServerError = function(res) {
res.status(500).json({ status: 500, error: true, reason: "internal server error" });
}
// END FAIL HELPERS
app.post("/generate", async function(req, res) {
"use strict";
var context = uuidv4();
var directory = "/tmp/" + context;
await fs.mkdir(directory, function() {});
var cleanup = function() {
spawn("rm", ["-r", directory]);
}
var files = req.body.files;
if (!(files && files.length)) {
cleanup();
failMalformed(res);
return;
}
for (var i = 0; i < files.length; i++) {
var name = files[i].filename;
var content = files[i].content;
if (!(name && content)) {
cleanup();
failMalformed(res);
return;
}
await fs.writeFile(directory + "/" + name, Buffer.from(content, "base64"), function(err) { });
}
var mainfile = req.body.mainfile;
var mainfileExtension = req.body.mainfileExtension;
if (!(mainfile && mainfileExtension)) {
cleanup();
failMalformed(res);
return;
}
var commandElements = [directory + "/" + mainfile + "." + mainfileExtension, "--build-once"]
if (req.body.locals) {
function addslashes(str) {
return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}
var variables = JSON.stringify(req.body.locals);
commandElements.push("--locals");
commandElements.push(variables);
}
var relaxed = spawn("relaxed", commandElements);
relaxed.stdout.on('data', function(data) {
console.info('relaxed stdout: ' + data);
});
relaxed.stderr.on('data', function(data) {
console.error('relaxed stderr: ' + data);
});
relaxed.on("close", code => {
// TODO: Check if file exists else return internal server error.
if (code == 0) {
var fileName = directory + "/" + mainfile + ".pdf";
res.status(200).sendFile(fileName, function(err) {
if (err) {
failServerError(res);
}
cleanup();
})
} else {
failServerError(res);
cleanup();
}
});
relaxed.on("error", err => { });
});
var port = process.env.PORT || 8000;
app.listen(port, function() {
"use strict";
console.log("Listening...");
});