-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
190 lines (162 loc) · 5.44 KB
/
routes.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var passport = require('passport'),
UserModel = require('./model/user.js'),
AWS = require('aws-sdk'),
fs = require("fs"),
sendgrid = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD);
// AWS.config.loadFromPath('./config.json');
// Set your region for future requests.
AWS.config.update({region: 'eu-west-1'});
var s3 = new AWS.S3();
s3.listBuckets(function(err, data) {
for (var index in data.Buckets) {
var bucket = data.Buckets[index];
console.log("Bucket: ", bucket.Name, ' : ', bucket.CreationDate);
}
});
module.exports = function (app) {
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login');
}
// app.get('/', ensureAuthenticated, function (req, res) {
// res.render('index', {
// active: "home",
// user: req.user
// });
// });
app.get('/', function (req, res) {
res.render('index', {
user: req.user
});
});
app.get('/video', function (req, res) {
res.render('index', {
user: req.user
});
});
app.get('/mail', function (req, res) {
res.render('mail', {
active: "home",
user: req.user
});
});
app.get('/record', function (req, res) {
res.render('record', {
active: "record",
user: req.user
});
});
// list all available files in the system
app.get('/list', function (req, res) {
var params = {
Bucket: "raiseyourvoice2",
Prefix: "audio_files/audio"
};
s3.listObjects(params, function(err, data) {
if (err) {
console.log("Error uploading data: ", err);
} else {
data = data.Contents;
data = data.map(function(file) {
var dateString;
file.Key = "https://s3-eu-west-1.amazonaws.com/raiseyourvoice2/" + file.Key;
dateString = new Date(file.LastModified).toGMTString();
file.DateTime = dateString.substring(0, dateString.length-4);
return file;
});
res.render('list', {
active: "list",
user: req.user,
audioFiles: data.reverse(),
});
}
});
});
app.post('/record', function (req, res) {
if (req.files.hasOwnProperty("user_audio_blob")) {
fs.readFile(req.files.user_audio_blob.path, function (err, data) {
var s3data = {
Bucket: 'raiseyourvoice2',
Key: "audio_files/audio_" + new Date().toISOString() + ".wav",
Body: data,
ACL: "public-read"
};
s3.putObject(s3data, function(err, data) {
if (err) {
console.log("Error uploading data: ", err);
}
});
});
}
res.render('record', {
active: "home",
user: req.user
});
});
app.post('/', function (req, res) {
req.assert('email', 'required').notEmpty();
req.assert('email', 'valid email required').isEmail();
var errors = req.validationErrors();
if (!errors) {
var emailText = '<html> <head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-15"> </head> <body text="#000000" bgcolor="#FFFFFF"> Wir lieben Feedback! <br> <div class="moz-forward-container"><br> <video style="display: block;" autoplay="autoplay" clipid="532383" poster="https://sc.liveclicker.net/service/clip?kind=poster&ID=532383&type=1" controls="controls" src="https://sc.liveclicker.net/service/clip?kind=video&ID=532383&type=1" height="180" width="320"><a moz-do-not-send="true" href="http://em.liveclicker.net/service/clip?kind=clickthrough&ID=532383&type=1" alt="Redirect"><img moz-do-not-send="true" src="cid:[email protected]" alt="Video clip" style="display:block;" height="180" width="320" border="0"></a></video><br> Sag uns deine Meinung!<br> </div> <br> </body> </html>';
sendgrid.send({
to: req.body.email,
// toname: 'Lars Butzmann',
from: '[email protected]',
fromname: 'v0ice Inc.',
subject: 'Example email with HTML5 video',
text: 'Sending email with NodeJS through SendGrid!',
html: (req.body.inputText !== '') ? req.body.inputText : emailText
});
res.redirect('/');
} else {
res.render('index', {
message: '',
errors: errors
});
}
});
app.get('/register', function(req, res) {
res.render('register', {
active: "home"
});
});
app.post('/register', function(req, res) {
req.assert('username', 'Username is required').notEmpty();
req.assert('password', 'Password is too short').len(6, 20);
req.assert('password', 'Passwords do not match').equals(req.body.password_confirmation);
var errors = req.validationErrors();
if (!errors){
UserModel.register(new UserModel({ username : req.body.username }), req.body.password, function(err, account) {
if (err) {
return res.render('register', { user : account });
}
res.redirect('/');
});
} else {
res.render('register', {
message: '',
errors: errors
});
}
});
app.get('/login', function(req, res) {
res.render('login', {
active: "home"
});
});
app.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
})
);
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
// API
app.get('/api', function (req, res) {
res.send('API is running');
});
}