-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·438 lines (381 loc) · 12.8 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
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
#!/usr/bin/env node
var express = require('express');
var bodyParser = require('body-parser');
var program = require('commander');
var uuidv4 = require('uuid/v4');
var fs = require('fs');
var vm = require('vm2');
var csv = require('csv');
var xlsx = require('xlsx');
var path = require('path');
var winston = require('winston');
var winstonDRF = require('winston-daily-rotate-file');
program
.version('1.0.0')
.option('-p, --port [value]', 'TCP port of server ', 3000)
.option('-o, --output-dir [value]', 'Output directory', 'shared')
.option('-d, --disable-scripts-executing [value]', 'Whether script execution disabled', false)
.option('--log-level [value]', 'Level of logging. Possible values: error, warn, info, verbose, debug, silly', 'info')
.option('--log-file [value]', 'Path to log file. File will created and will rotate by daily.', null)
.option('--disable-playground [value]', 'Disable playground app.', false);
program.parse(process.argv);
var blankImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
var allowableImageParams = ['aspect-ratio', 'height', 'width', 'background', 'border', 'blur', 'contrast', 'crop', 'frame', 'gamma', 'monochrome', 'negative', 'noise', 'quality'];
//region --- Configure logs
function tsFormat() {return (new Date()).toLocaleTimeString()}
var logTransports = [];
var logLevel = program.logLevel;
var loggingToFile = !!program.logFile;
if (program.logFile) {
if (!path.isAbsolute(program.logFile)) {
program.logFile = path.join(__dirname, program.logFile);
}
var logDirName = path.dirname(program.logFile);
var logFileName = path.basename(program.logFile);
fs.access(logDirName, fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK, function(err) {
if (err) {
if (err.code === 'ENOENT') {
fs.mkdirSync(logDirName);
} else {
loggingToFile = false;
}
}
});
if (loggingToFile) {
logTransports.push(new (winstonDRF)({
timestamp: tsFormat,
datePattern: 'yyyy-MM-dd',
prepend: true,
filename: path.join(logDirName, '-' + logFileName),
level: logLevel
}))
}
}
if (!loggingToFile) {
logTransports.push(new (winston.transports.Console)({
timestamp: tsFormat,
level: logLevel,
colorize: true
}));
}
var logger = new (winston.Logger)({
transports: logTransports
});
logger.cli();
winston.addColors({error: 'red', warn: 'yellow', info: 'blue', verbose: 'grey', debug: 'black', silly: 'white'});
logger.verbose('log level:', logLevel);
//endregion
//region --- AnyChart module configure
var anychart_nodejs = require('anychart-nodejs');
// var anychart_nodejs = require('../AnyChart-NodeJS');
//endregion
//region --- Pdfmake configure
var pdfMake = require('pdfmake');
var fontDescriptors = {
Roboto: {
normal: path.join(__dirname, 'fonts', 'Roboto-Regular.ttf'),
bold: path.join(__dirname, 'fonts', 'Roboto-Medium.ttf'),
italics: path.join(__dirname, 'fonts', 'Roboto-Italic.ttf'),
bolditalics: path.join(__dirname, 'fonts', 'Roboto-Italic.ttf')
}
};
var printer = new pdfMake(fontDescriptors);
//endregion
//region --- Express server configure
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({parameterLimit: 100000, limit: '50mb', extended: true}));
//endregion
function partial(fn, var_args) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
var newArgs = args.slice();
newArgs.push.apply(newArgs, arguments);
return fn.apply(this, newArgs);
};
}
function applyImageParams(params, chartSettings) {
for (var i = 0, len = allowableImageParams.length; i < len; i++) {
var paramName = allowableImageParams[i];
var value = chartSettings[paramName];
if (value)
params[paramName] = value
}
}
function recursiveTraverse(obj, func) {
if (Object.prototype.toString.call(obj) === '[object Array]') {
for (var i = 0; i < obj.length; i++) {
if (i in obj)
recursiveTraverse(obj[i], func);
}
} else if (typeof obj === 'object') {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (key === 'chart') {
func(obj)
} else {
recursiveTraverse(obj[key], func)
}
}
}
}
}
function convertCharts(obj, callback) {
var charts = [];
recursiveTraverse(obj, function(config) {
charts.push(config)
});
var chartsToConvert = charts.length;
var handler = function(config, index) {
var key = 'chart';
var chartConfig = config[key];
var data = chartConfig.data;
var dataType = chartConfig.dataType;
var containerId = chartConfig.containerId || 'container';
if (dataType === 'javascript' && program.disableScriptsExecuting) {
logger.warn('Script executing disabled.');
data = null;
}
if (data) {
logger.info('----> PDF Report. Chart ' + index + ' exporting.');
var imgConvertCallback = partial(function imgConvertCallback(dataType, chartNum, userData, err, data) {
logger.info('<---- PDF Report. Chart ' + chartNum + ' exporting.');
if (data) {
config.image = 'data:image/png;base64,' + data.toString('base64');
} else {
config.image = blankImage;
logger.error('PDF Report. Convert %s to %s. Data: %s. Container id: %s. Chart was replaced with blank Image.',
dataType.toUpperCase(), 'PDF', userData, containerId, err);
}
delete config[key];
chartsToConvert--;
if (chartsToConvert === 0) {
callback(obj)
}
}, dataType, index, data);
var params = {
outputType: 'png',
dataType: dataType,
// document: iframeDoc,
containerId: containerId
};
applyImageParams(params, chartConfig);
anychart_nodejs.exportTo(data, params, imgConvertCallback);
} else {
config.image = blankImage;
delete config[key];
logger.error('Chart data not found. Chart was replaced with blank Image. Convert %s to %s. Data: %s. Container id: %s.',
dataType.toUpperCase(), 'PDF', data, containerId, err);
}
};
for (var i = 0, len = charts.length; i < len; i ++) {
handler(charts[i], i);
}
if (chartsToConvert === 0) {
callback(obj)
}
}
function getContentType(type) {
var contentType;
switch (type) {
case 'png':
contentType = 'image/png';
break;
case 'jpg':
contentType = 'image/jpg';
break;
case 'tiff':
contentType = 'image/tiff';
break;
case 'svg':
contentType = 'image/svf+xml';
break;
case 'pdf':
contentType = 'application/pdf';
break;
case 'ps':
contentType = 'application/postscript';
break;
case 'xlsx':
contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'csv':
contentType = 'text/csv';
break;
case 'json':
contentType = 'application/json';
break;
default:
contentType = 'text/plain'
}
return contentType;
}
function sendResult(req, res, data, fileType) {
var autoFileName = 'anychart_' + uuidv4() + '.' + fileType;
var responseType = (req.body.response_type || 'file').toLowerCase();
var fileName = responseType === 'file' ? req.body.file_name || autoFileName : autoFileName;
if (responseType === 'file') {
res.writeHead(200, {
'Content-Type': getContentType(fileType),
'Content-Length': data.length,
'Content-Disposition': 'attachment; filename=' + fileName
});
res.end(data);
} else if (responseType === 'base64') {
var base64Data = data.toString('base64');
var result = {data: base64Data};
res.set('Content-Type', getContentType('json'));
res.send(JSON.stringify(result));
} else if (responseType === 'url') {
var path = program.outputDir + '/' + autoFileName;
fs.access(program.outputDir, fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK, function(err) {
if (err) {
if (err.code === 'ENOENT') {
fs.mkdirSync(program.outputDir);
logger.info('Directory ' + program.outputDir + ' was created.');
} else {
logger.error(err);
return;
}
}
fs.writeFile(path, data, function(err) {
if (err) {
logger.error(err);
} else {
logger.info('Written to file ' + path);
res.set('Content-Type', getContentType('json'));
res.send(JSON.stringify({'url': path}));
}
});
});
}
}
function generateOutput(req, res) {
var dataType = req.body.data_type && req.body.data_type.toLowerCase();
var fileType = (req.body.file_type || 'png').toLowerCase();
var data = req.body.data;
var containerId = req.body.containerId || 'container';
var resources = req.body.resources || [];
if (dataType === 'javascript' && program.disableScriptsExecuting) {
logger.warn('Script executing disabled.');
data = null;
}
if (data) {
logger.info('----> Input. Convert %s to %s. Image.', dataType.toUpperCase(), fileType.toUpperCase());
var imgConvertCallback = partial(function imgConvertCallback(fileType, dataType, userData, err, data) {
if (err)
logger.error('Convert %s to %s. Data: %s. Container id: %s.',
dataType.toUpperCase(), fileType.toUpperCase(), userData, containerId, err);
else
logger.info('<---- Output. Convert %s to %s. Image.', dataType.toUpperCase(), fileType.toUpperCase());
if (!data || err) {
res.status(500).send({error: err ? err.message : 'Image generation error'});
} else {
sendResult(req, res, data, fileType);
}
}, fileType, dataType, data);
var params = {
outputType: fileType,
dataType: dataType,
// document: iframeDoc,
containerId: containerId,
resources: resources
};
applyImageParams(params, req.body);
anychart_nodejs.exportTo(data, params, imgConvertCallback);
} else {
logger.error('Chart data not found. Convert %s to %s. Data: %s. Container id: %s.',
dataType.toUpperCase(), fileType.toUpperCase(), data, containerId, err);
res.status(500).send({error: 'Chart data not found'});
}
}
app.post('/pdf-report', function (req, res) {
if (!program.disableScriptsExecuting) {
try {
var script = new vm.VM();
var data = script.run(req.body.data);
script = null;
} catch (e) {
logger.error('PDF config evaluating failed. DataL %s', req.body.data, e);
res.status(500).send({error: e.message});
return;
}
} else {
logger.warn('Script executing disabled.');
res.status(500).send({error: 'Script executing disabled.'});
return;
}
convertCharts(data, function(dd) {
try {
var pdfDoc = printer.createPdfKitDocument(dd);
var chunks = [];
pdfDoc.on('data', function(chunk) {
chunks.push(chunk);
});
pdfDoc.on('end', function() {
sendResult(req, res, Buffer.concat(chunks), 'pdf');
pdfDoc = null;
});
pdfDoc.on('error', function(e) {
logger.error('PDF generation error. Data: %s', req.body.data, e)
});
pdfDoc.end();
} catch (e) {
logger.error('PDF generation error. Data: %s', req.body.data, e);
}
});
});
app.post('/vector-image', function (req, res) {
try {
if (!req.body.file_type)
req.body.file_type = 'pdf';
generateOutput(req, res);
} catch (e) {
logger.error('Vector image generation failed. Request body: %s.', req.body, e);
}
});
app.post('/raster-image', function (req, res) {
try {
generateOutput(req, res);
} catch (e) {
logger.error('Raster image generation failed. Request body: %s.', req.body, e);
}
});
app.post('/data-file', function (req, res) {
try {
var data = req.body.data;
var fileType = (req.body.file_type || 'xlsx').toLowerCase();
if (fileType === 'xlsx') {
csv.parse(data, function(err, data) {
var ws = xlsx.utils.aoa_to_sheet(data);
var wb = xlsx.utils.book_new();
wb.SheetNames.push('Sheet 1');
wb.Sheets['Sheet 1'] = ws;
data = xlsx.write(wb, {'type': 'buffer'});
sendResult(req, res, data, fileType)
})
} else if (fileType === 'csv') {
sendResult(req, res, data, fileType)
}
} catch (e) {
logger.error('Data file generation failed. Request body: %s.', req.body, e);
}
});
if (!program.disablePlayground) {
app.use(express.static(path.join(__dirname, 'playground')));
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/playground/template.html'));
});
}
app.get('/status', function (req, res) {
res.send('ok');
});
app.listen(program.port, function () {
logger.info('Export server listening on port ' + program.port + '!')
});
if (global.gc) {
setInterval(function() {
global.gc()
}, 3000);
}
module.exports = app;