-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (157 loc) · 4.82 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
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
const fs = require('fs');
const fsp = fs.promises;
const path = require('path');
const spawn = require('child_process').spawn;
const express = require('express');
const multer = require('multer');
const moment = require('moment');
const phin = require('phin');
const PORT = process.env.PORT || 8080;
const HOME = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
const GAMEDIR = process.env.GAMEDIR || path.join(HOME, (process.platform === 'win32') ? 'Documents' : '.config', 'OpenRCT2');
const PARKDIR = process.env.PARKDIR || path.join(GAMEDIR, 'save');
const SCREENSHOTDIR = process.env.SCREENSHOTDIR || path.join(GAMEDIR, 'screenshot');
const FILENUMMAX = 100000;
const TIMEOUT = process.env.TIMEOUT || 20000;
const app = express();
const upload = multer({ dest: PARKDIR });
let filenum = 0;
function getFileNum() {
return (filenum = (filenum + 1) % FILENUMMAX);
}
function getScreenshot(file, options = {}) {
return new Promise((resolve, reject) => {
let destination = path.join(SCREENSHOTDIR, `screenshot_${moment().format('HHmmssSS')}_${getFileNum()}.png`);
let proc = spawn('openrct2-cli', ['screenshot', `${file}`, destination, 'giant', Math.min(Math.abs(parseInt(options.zoom || 3)), 7), parseInt(options.rotation || 0) % 4], {
stdio: ['ignore', process.stdout, process.stderr]
});
let timeout = setTimeout(() => {
reject('Timed out');
proc.kill();
}, TIMEOUT);
proc.on('exit', (code) => {
if (code !== null) {
clearTimeout(timeout);
try {
resolve(destination);
}
catch (ex) {
reject(ex);
}
}
});
});
}
app.set('trust proxy', 1);
app.set('view engine', 'ejs');
app.use('/assets/', express.static('assets'));
app.get('/healthcheck', async (req, res) => {
res.send('Healthy');
});
app.post('/upload', upload.single('park'), async (req, res) => {
try {
if (!req.file) {
res.status(400).send({
status: 'bad'
});
}
else {
let options = {
zoom: req.body.zoom || req.query.zoom,
rotation: req.body.rotation || req.query.rotation,
};
let image = await getScreenshot(req.file.path, options);
res.sendFile(image, (err) => {
if (err) {
res.status(500).send({
status: 'bad'
});
}
fs.unlink(image, (err) => {
if (err) {
console.log(err);
}
});
});
fs.unlink(req.file.path, (err) => {
if (err) {
console.log(err);
}
});
}
}
catch (ex) {
console.log(ex);
res.status(500).send({
status: 'bad'
});
}
});
app.get('/upload', async (req, res) => {
try {
if (!req.query || !req.query.url) {
res.status(400).send({
status: 'bad'
});
}
else {
let park = await phin({
url: req.query.url,
followRedirects: true
});
let filename = path.join(PARKDIR, `download_${moment().format('YYYYMMDD')}_${getFileNum()}.sv6`);
await fsp.writeFile(filename, park.body);
let image = await getScreenshot(filename, req.query);
res.sendFile(image, (err) => {
if (err) {
res.status(500).send({
status: 'bad'
});
}
fs.unlink(image, (err) => {
if (err) {
console.log(err);
}
});
fs.unlink(filename, (err) => {
if (err) {
console.log(err);
}
});
});
}
}
catch (ex) {
console.log(ex);
res.send({
status: 'bad'
});
}
});
app.get('/', (req, res) => {
res.render('index',
function (err, html) {
if (!err) {
res.send(html);
}
else {
console.log(err);
res.send();
}
}
)
});
let server = app.listen(PORT, () => {
console.log(`RCT Screenshotter listening on port ${PORT}.`);
fs.mkdir(PARKDIR, { recursive: true }, err => {
if (err) {
console.log(err);
}
});
fs.mkdir(SCREENSHOTDIR, { recursive: true }, err => {
if (err) {
console.log(err);
}
})
});
process.on('SIGTERM', server.close);