forked from isaudits/docker-carbone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.js
118 lines (103 loc) · 2.95 KB
/
generator.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
const fs = require('fs')
const path = require('path')
const mime = require('mime-types')
const carbone = require('carbone')
const util = require('util')
const AWS = require('aws-sdk')
const exec = util.promisify(require('child_process').exec)
const replaceImages = async (template, images) => {
let executed = false
let newTemplate = template.split('.')
newTemplate = 'temp.' + newTemplate[newTemplate.length - 1]
for (let i = 0; i < images.length; i++) {
let usedTemplate = i === 0 ? template : `/temp/${newTemplate}`
const image = images[i]
try {
await exec(
`sh replace-image.sh ${usedTemplate} ${newTemplate} ${image.destination} ${image.source}`
)
if (!executed) {
executed = true
}
} catch (err) {
throw new Error(err)
}
}
if (executed) {
// temp is temporary folder defined in shell script above
return `temp/${newTemplate}`
}
return template
}
const render = (template, data, options) => {
return new Promise((resolve, reject) => {
carbone.render(template, data, options, (err, result) => {
if (err) {
reject(new Error(err))
} else {
if (template.includes('temp/')) {
fs.rmSync(path.join(__dirname, 'temp'), { recursive: true, force: true });
}
resolve(result)
}
})
})
}
const saveToS3 = (Body, Key) => {
const s3Client = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION
})
return new Promise((resolve, reject) => {
s3Client.upload({
ACL: 'public-read',
Bucket: process.env.AWS_BUCKET_NAME,
Body,
Key
}, function (err, data) {
if (err) {
reject(new Error(err))
} else {
resolve(data.Location)
}
})
})
}
module.exports = async (res, params, download = false) => {
let { template, filename, data, options, imagesReplace } = params
if (!template.includes('templates')) {
template = `templates/${template}`
}
if (imagesReplace) {
template = await replaceImages(template, imagesReplace)
}
if (options.convertTo) {
//change filename extension of converted (ie to PDF)
filename = filename.replace(/\.[^/.]+$/, '')
filename = filename + '.' + options.convertTo
}
try {
const buffer = await render(template, data, options)
if (download) {
res.set({
'Access-Control-Expose-Headers': 'Content-Disposition',
'Content-Type': mime.lookup(filename),
'Content-Disposition': `attachment; filename=${filename.split('/').pop()}`
})
res.end(buffer)
} else {
try {
const url = await saveToS3(buffer, filename)
res.json({ url })
} catch (e) {
console.log(e)
res.statusCode = 500
res.json({ message: e.message || 'Error save to S3' })
}
}
} catch (e) {
res.statusCode = 500
res.json({ message: e.message })
}
}