-
Notifications
You must be signed in to change notification settings - Fork 23
/
web.js
132 lines (115 loc) · 3.98 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
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
/* jshint strict: false */
/* global require: false, process: false, console: false, Buffer: false */
var fs = require('fs');
var pg = require('pg');
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');
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 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(''));
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) {
zip.updateFile('content.xml', new Buffer(content));
return zip.toBuffer();
};
var createText = function(data, settings) {
return odtprocessor.renderText(
files.content,
data,
EUCopyrightQuestions,
settings
);
};
var storeSubmission = function(data){
data = JSON.stringify(data);
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
var handleError = function(err) {
if(!err) return false;
console.info('Error storing data:' + err);
done(client);
return true;
};
client.query('INSERT INTO submission (timestamp, data) VALUES ($1, $2)',
[new Date(), data],
function(err) {
if(handleError(err)) return;
console.info('Stored data.');
done();
}
);
});
};
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.store) {
storeSubmission(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);