forked from kodira/carbone-docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
149 lines (125 loc) · 4.32 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
const path = require(`path`);
const fs = require(`fs-extra`);
const util = require(`util`);
const carbone = require(`carbone`);
const {tmpdir} = require('os');
const crypto = require('crypto');
const fastify = require('fastify')({
logger: true
});
const multer = require('fastify-multer');
const mkdirp = require('mkdirp');
const mimeTypes = require('mime-types');
const {replaceImages} = require('./images');
let maxFileSize = !isNaN(process.env.MAX_FILESIZE) ? parseInt(process.env.MAX_FILESIZE, 10) : 10000000; // 10 MB;
maxFileSize = maxFileSize === 0 ? Infinity : maxFileSize; // 10 MB;
const PORT = process.env.PORT || 3030;
const TEMP_DIR = tmpdir();
const TEMPLATES_DIR = process.env.TEMPLATES_DIR || './templates/';
const UPLOAD_DIR = process.env.UPLOAD_DIR || path.resolve(TEMP_DIR, 'carbone', 'uploads/');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
mkdirp.sync(UPLOAD_DIR);
cb(null, UPLOAD_DIR);
},
filename: function (req, file, cb) {
const ext = file.originalname.split('.').pop() || '';
cb(null, crypto.randomBytes(48).toString('base64url') + '.' + ext)
}
});
const upload = multer({
storage,
limits: {
fileSize: maxFileSize,
files: 1
}
});
const render = util.promisify(carbone.render);
const copyFile = util.promisify(fs.copyFile);
carbone.set({templatePath: TEMPLATES_DIR}); // With desired side effect to load translations
fastify.register(multer.contentParser)
if (process.env.DEMO) {
fastify.get('/', (req, reply) => {
fastify.log.info('Enable demo site')
const stream = fs.createReadStream(path.resolve(`./demo/test.html`))
reply.type('text/html').send(stream);
});
}
function getTemplatePath(req) {
if (req.file) {
return Promise.resolve(req.file.path);
}
const templateName = req.params.template;
const templatePath = path.resolve(TEMPLATES_DIR, templateName);
const ext = templatePath.split('.').pop() || '';
mkdirp.sync(path.resolve(TEMP_DIR, 'carbone'));
const dest = path.resolve(TEMP_DIR, 'carbone', crypto.randomBytes(48).toString('base64url') + '.' + ext);
return copyFile(templatePath, dest).then(() => dest);
}
fastify.post('/render/:template?', {
preHandler: upload.single('template')
}, async function (req, reply) {
if (!req.file && !req.params.template) {
return reply.status(400);
}
const templateName = req.file ? req.file.originalname : req.params.template;
const options = getOptions(req.body.options, templateName);
const data = getJson(req.body.data);
const images = getJson(req.body.images, []);
fastify.log.info({options}, 'Options')
let report = null;
let templatePath = null;
try {
templatePath = await getTemplatePath(req);
await replaceImages(images, templatePath, fastify.log);
report = await render(templatePath, data, options);
} catch (e) {
fastify.log.error(e);
if (e.code && e.code === 'ENOENT') {
return reply.status(404).send(`Template file not found.`);
} else if (typeof e === 'string' && e.includes('Unknown input file type')) {
return reply.status(415).send(e);
}
return reply.status(500).send(typeof e === 'string' ? e : e.message || 'Internal Server Error');
} finally {
if (templatePath) {
fastify.log.info({templatePath}, 'Delete tmp file')
fs.remove(templatePath);
}
}
reply.header(`Content-Disposition`, `attachment; filename=${options.outputName}`);
reply.header(`Content-Transfer-Encoding`, `binary`);
reply.header(`Content-Type`, `application/octet-stream`);
return reply.send(report);
});
function getOptions(optionsStr, templateName) {
const originalNameWOExt = templateName
.split(`.`)
.slice(0, -1)
.join(`.`);
const originalFormat = templateName.split(`.`).reverse()[0];
let options = getJson(optionsStr)
options.convertTo = options.convertTo || originalFormat;
options.outputName = options.outputName || `${originalNameWOExt}.${options.convertTo}`;
return options;
}
function getJson(data, def = {}) {
if (data && typeof data !== `object`) {
try {
return JSON.parse(data);
} catch (e) {
return {};
}
}
return data || def;
}
const serve = async () => {
try {
await fastify.listen(PORT, '0.0.0.0');
fastify.log.info({maxFileSize}, `Carbone wrapper listening on port ${PORT}!`)
} catch (e) {
fastify.log.error(e)
process.exit(1);
}
}
serve();