forked from okfde/eucopyright
-
Notifications
You must be signed in to change notification settings - Fork 5
/
web.js
106 lines (92 loc) · 3.36 KB
/
web.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
/* jshint strict: false */
/* global require: false, process: false, console: false, Buffer: false */
var fs = require('fs');
var yaml = require('js-yaml');
var EUCopyrightQuestions = yaml.safeLoad(fs.readFileSync('_data/questions.yml', 'utf8'));
var translations = yaml.safeLoad(fs.readFileSync('_data/translations.yml', 'utf8'));
var AdmZip = require('adm-zip');
var postmark = require('postmark')(process.env.POSTMARK_API_KEY);
var odtprocessor = require('./js/odtprocessor.js');
console.log(translations);
var express = require('express');
var app = express();
var files = {
content: fs.readFileSync('data/content.xml', 'utf8'),
manifest: fs.readFileSync('data/META-INF/manifest.xml'),
meta: fs.readFileSync('data/meta.xml'),
mimetype: fs.readFileSync('data/mimetype'),
settings: fs.readFileSync('data/settings.xml'),
styles: fs.readFileSync('data/styles.xml'),
};
var ODTContentType = 'application/vnd.oasis.opendocument.text';
var ODTFilename = 'consultation-document_en.odt';
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', process.env.ALLOWED_DOMAIN);
res.header('Access-Control-Allow-Methods', 'POST');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.configure(function() {
app.use(express.bodyParser());
app.use(allowCrossDomain);
});
var validateEmail = function(email) {
return (/\S+@\S+\.\S+/).test(email);
};
var createODT = function(content) {
var zip = new AdmZip();
zip.addFile('mimetype', files.mimetype);
zip.addFile('META-INF/manifest.xml', files.manifest);
zip.addFile('meta.xml', files.meta);
zip.addFile('settings.xml', files.settings);
zip.addFile('styles.xml', files.styles);
zip.addFile('content.xml', new Buffer(content));
return zip.toBuffer();
};
var createText = function(data, settings) {
return odtprocessor.renderText(
files.content,
data,
EUCopyrightQuestions,
settings
);
};
var sendEmail = function(email, name, buffer, lang){
var body = translations.emailBody[lang] || translations.emailBody.en;
name = name || translations.emailName[lang] || translations.emailName.en;
body = body.replace(/\{\{\s*name\s*\}\}/, name);
console.info('Sending email to ' + email);
postmark.send({
'From': process.env.FROM_EMAIL_ADDRESS,
'To': email,
'Subject': translations.emailSubject[lang] || translations.emailSubject.en,
'TextBody': body,
'Attachments': [{
'Content': buffer.toString('base64'),
'Name': ODTFilename,
'ContentType': ODTContentType
}]
}, function(error) {
if(error) {
console.error('Unable to send to ' + email + ' via postmark: ' + error.message);
return;
}
console.info('Sent email to ' + email);
});
};
app.post('/document', function(req, res){
var buffer = createODT(createText(req.body, {}));
if (req.body.email && validateEmail(req.body.email)) {
sendEmail(req.body.email, req.body.name, buffer, req.body.language || 'en');
if (req.query.redirect) {
return res.redirect(req.query.redirect);
}
}
res.setHeader('Content-Type', ODTContentType);
res.setHeader('Content-Length', buffer.length);
res.setHeader('Content-Disposition', 'attachment; filename="' + ODTFilename +'"');
res.end(buffer);
});
var port = process.env.PORT || 5000;
app.listen(port);
console.log('Listening on port ' + port);