-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (124 loc) · 4.22 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
150
151
var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
var S3_BUCKET = process.env.S3_BUCKET
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
var express = require('express');
var Event = require('./mongo/model-event');
var formidable = require('formidable');
var fs = require('fs');
var morgan = require('morgan');
var aws = require('aws-sdk');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
var _ = require('underscore');
var events = [];
var MONGOHQ_URL="mongodb://heroku:qZb3jrB8TU34LStxxtKKCIiPo7mZoMQ6xZ2VNzyVBWNmy4tm2n8NsOFn0ebR0Wk7FaIqEuOhKN9XHRVO-ZKRRA@lennon.mongohq.com:10031/app32045436"
// npm install mongodb
var mongoose = require('mongoose');
var db = mongoose.connect(MONGOHQ_URL, function(err, db) {
// operate on the collection named "test"
console.log('oyooo');
});
var path = require('path');
app.use(morgan('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', './views');
app.set('view engine', 'jade');
var port = process.env.PORT || 3000
server.listen(port);
app.get('/add-person', function (req, res) {
res.render('add-person');
});
app.get('/', function(req, res, next) {
Event.find(function (err, events) {
if (err) return next(err);
console.log(events);
var eventObject = {};
events.forEach(function(event) {
eventObject[event.emailadres] = '/audio2/' + event.filename;
});
res.render('index', {events: eventObject});
})
});
app.get('/audio2/:filename', function(req, res, next) {
Event.find({'filename': req.params.filename}, function(err, events) {
if (err) return next(err);
if( !events || !events.length ) {
return next();
}
res.set("Content-Type", "audio/mpeg");
res.send(events[0].audio);
});
});
app.get('/embedmp3/audio2/:filename', function(req, res, next) {
res.render('embed-mp3', {mp3_audio: '/audio2/' + req.params.filename});
});
app.get('/sign_s3', function(req, res){
aws.config.update({accessKeyId: AWS_ACCESS_KEY, secretAccessKey: AWS_SECRET_KEY, region: 'eu-west-1'});
var s3 = new aws.S3();
var s3_params = {
Bucket: S3_BUCKET,
Key: req.query.s3_object_name,
Expires: 60,
ContentType: req.query.s3_object_type,
ACL: 'public-read'
};
console.log(s3_params);
s3.getSignedUrl('putObject', s3_params, function(err, data){
if(err){
console.log(err);
}
else{
var return_data = {
signed_request: data,
url: 'https://'+S3_BUCKET+'.s3-eu-west-1.amazonaws.com/'+req.query.s3_object_name
};
res.write(JSON.stringify(return_data));
res.end();
}
});
});
// app.get('/s3test', function(req,res){
// res.redirect('/s3test.html');
// });
app.post('/submit_form', function(req, res){
console.log('req:', req);
username = req.body.username;
full_name = req.body.full_name;
avatar_url = req.body.avatar_url;
// update_account(username, full_name, avatar_url); // TODO: create this function
console.log(username, full_name, avatar_url);
// TODO: Return something useful or redirect
});
app.get('/event', function(req, res) {
res.render('add-event', {done: req.query.done});
});
app.post('/event', function(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
if( !files['audio'].size ) {
// No file given!
console.log('no file received for adding event', fields['word']);
return res.redirect('/event');
}
var email = fields['emailadres'].replace('@', '');
Event.find({emailadres: email}, function(err, events) {
if(events.length) {
// Event already exists, do not add!
return res.redirect('/event');
}
var filedata = fs.readFileSync(files['audio'].path);
var eventInstance = new Event();
eventInstance.emailadres = email;
eventInstance.audio = filedata;
eventInstance.filename = files['audio'].name;
eventInstance.save(function(err, data) {
console.log(err);
});
res.redirect('/event?done=true');
});
});
});