This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
147 lines (135 loc) · 3.26 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
const carbone = require('carbone');
const fs = require('fs-extra');
const path = require('path');
const {v4: uuidv4} = require('uuid');
const asyncRender = async (template, data, options) => {
return new Promise(((resolve, reject) => {
carbone.render(template, data, options, (err, result, reportName) => {
if (err) {
reject(err);
} else {
resolve({report: result, reportName: reportName});
}
});
}));
};
module.exports.fileTypes = {
'csv': [
'doc',
'docx',
'html',
'odt',
'pdf',
'rtf',
'txt',
'csv'
],
'docx': [
'doc',
'docx',
'html',
'odt',
'pdf',
'rtf',
'txt'
],
'html': [
'html',
'odt',
'pdf',
'rtf',
'txt'
],
'odt': [
'doc',
'docx',
'html',
'odt',
'pdf',
'rtf',
'txt'
],
'pptx': [
'odt',
'pdf'
],
'rtf': [
'docx',
'pdf'
],
'txt': [
'doc',
'docx',
'html',
'odt',
'pdf',
'rtf',
'txt'
],
'xlsx': [
'odt',
'pdf',
'rtf',
'txt',
'csv',
'xls',
'xlsx'
]
};
// initialize carbone formatters, add a marker to indicate defaults...
// unfortunately carbone is a singleton and we cannot set formatters for each render call,
const DEFAULT_CARBONE_FORMATTERS = Object.freeze(Object.assign({}, carbone.formatters));
const addFormatters = formatters => {
if (Object.keys(formatters).length) {
carbone.formatters = Object.assign({}, DEFAULT_CARBONE_FORMATTERS);
carbone.addFormatters(formatters);
return true;
}
return false;
};
const resetFormatters = reset => {
if (reset) {
carbone.formatters = Object.assign({}, DEFAULT_CARBONE_FORMATTERS);
}
};
module.exports.startFactory = (converterFactoryTimeout) => {
carbone.set({startFactory: true, converterFactoryTimeout: converterFactoryTimeout});
};
module.exports.render = async (template, data = {}, options = {}, formatters = {}) => {
const result = {success: false, errorType: null, errorMsg: null, reportName: null, report: null};
if (!template) {
result.errorType = 400;
result.errorMsg = 'Template not specified.';
return result;
}
if (!fs.existsSync(template)) {
result.errorType = 404;
result.errorMsg = 'Template not found.';
return result;
}
// some defaults if options not set...
if (!options.convertTo || !options.convertTo.trim().length) {
// set convert to template type (no conversion)
options.convertTo = path.extname(template).slice(1);
}
if (!options.reportName || !options.reportName.trim().length) {
// no report name, set to UUID
options.reportName = `${uuidv4()}.${options.convertTo}`;
}
// ensure the reportName has the same extension as the convertTo...
if (options.convertTo !== path.extname(options.reportName).slice(1)) {
options.reportName = `${path.parse(options.reportName).name}.${options.convertTo}`;
}
let reset = addFormatters(formatters);
try {
const renderResult = await asyncRender(template, data, options);
result.report = renderResult.report;
result.reportName = renderResult.reportName;
result.success = true;
} catch (e) {
result.errorType = 500;
result.errorMsg = `Could not render template. ${e.message}`;
}
resetFormatters(reset);
return result;
};