-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
202 lines (180 loc) · 5.89 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
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
191
192
193
194
195
196
197
198
199
200
201
202
require('dotenv').config();
const express = require('express'),
app = express(),
imgur = require('imgur'),
thumbnails = require('imgur-thumbnails'),
exphbs = require('express-handlebars'),
utils = require('./lib/utils');
const images = utils.db.get('images'),
semesters = utils.semesters,
contactMessages = utils.db.get('contactMessages');
var hbs = exphbs.create({
defaultLayout: 'main',
partialsDir: ['views/partials/'],
helpers: {
ifGroupEveryOpen: function (index, every, options) {
if( index%every === 0 ){
return options.fn(this);
} else {
return options.inverse(this);
}
},
ifGroupEveryClose: function (index, every, options) {
if( options.data.last || (index + 1)%every === 0 ){
return options.fn(this);
} else {
return options.inverse(this);
}
},
getLocaleTimestampFromObjectId: function (id){
return id.getTimestamp().toLocaleString();
},
getDateFromObjectId: function (id){
return id.getTimestamp().toISOString().slice(0,10);
},
last6FromMD5: function (md5){
return md5.substr(md5.length - 6)
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use('/public', express.static('public'));
app.use('/analytics', require('./lib/routes/analytics'));
imgur.setClientId(process.env.IMGUR_CLIENTID);
app.locals.GA_TRACK_ID = process.env.GA_TRACK_ID;
app.locals.RECAPTCHA_KEY = process.env.RECAPTCHA_KEY;
app.locals.PAYPAL_DONATE_ID = process.env.PAYPAL_DONATE_ID;
app.locals.useCaptchaOnUploads = utils.settings.useCaptchaOnUploads;
app.locals.serverStarted = new Date().toGMTString();
app.get('/',function(req, res){
semesters.find({}, {sort: {_id: 1}})
.then((docs)=>{
res.render('home', {semesters: docs});
})
.catch((err)=>{
utils.sendToErrorPage(err, req, res);
});
});
app.get('/all',function(req, res){
images.find({active: true}, {sort: {_id: -1}})
.then((docs)=>{
const metadata = {
courseName: "Όλα τα μαθήματα",
courseKey: '',
year: 'Όλα',
count: docs.length
};
res.render('gallery', {meta: metadata, images: docs});
})
.catch((err)=>{
utils.sendToErrorPage(err, req, res);
});
});
app.get('/about',function(req, res){
res.render('about');
});
app.route('/contact')
.get(function(req, res){
res.render('contact');
})
.post(utils.multerUpload.fields([]), utils.verifyReCaptchaForContact, function(req, res){
const data = {
name: req.body.name,
email: req.body.email,
msg: req.body.msg
}
contactMessages.insert(data)
.then((docs) => {
res.render('contact', {error: false, resultMessage: 'Το μήνυμα αποθηκεύτηκε'});
})
.catch((err) => {
console.log('Error while saving contact message: ', data, 'error: ', err);
res.render('contact', {error: true, resultMessage: 'Πρόβλημα κατά την αποθήκευση του μηνύματος, δοκιμάστε ξανά αργότερα'});
});
});
app.get('/course/:course/year/:year', function(req, res, next){
if(utils.courses.keyExists(req.params.course) && (req.params.year==='all') || utils.yearExists(req.params.year)){
let filter = {"courseKey": req.params.course, active: true};
if(req.params.year!=='all'){
filter.year = req.params.year;
}
images.find(filter, {sort: {year: -1}})
.then((docs)=>{
const metadata = {
courseName: utils.courses.getNameFromKey(req.params.course),
courseKey: req.params.course,
year: req.params.year!=='all'?req.params.year:'Όλα',
count: docs.length
};
res.render('gallery', {meta: metadata, images: docs});
})
.catch((err)=>{
utils.sendToErrorPage(err, req, res);
});
} else {
next(); //default 404
}
});
app.get('/course/:course/exam/:id', function(req, res, next){
if(utils.courses.keyExists(req.params.course) && utils.isValidMongoID(req.params.id)){
images.findOne({"courseKey": req.params.course, "_id": req.params.id, active: true})
.then((doc)=>{
if(doc){
const metadata = {
courseName: utils.courses.getNameFromKey(doc.courseKey)
};
res.render('exam', {meta: metadata, image: doc});
} else {
next(); //default 404
}
})
.catch((err)=>{
next(); //default 404
});
} else {
next(); //default 404
}
});
app.route('/upload')
.get(function(req, res){
res.render('upload', {courses: utils.courses.all, years: utils.settings.years});
})
.post(utils.checkUploadedFile, utils.verifyReCaptchaForUpload, utils.checkRequiredInputs, utils.checkPhotoContent, function(req, res){
const albumId = process.env.IMGUR_ALBUM_DELETE_HASH, //delete hash because the album is anonymous
courseKey = utils.courses.getKeyFromName(req.body.courseName);
imgur.uploadBase64(req.file.buffer.toString('base64'), albumId )
.then(function (imgurRes) {
const httpsUrl = utils.httpsUrl(imgurRes.data.link);
return images.insert({
'url': httpsUrl,
'thumbnailUrl': thumbnails.medium(httpsUrl),
'deleteHash': imgurRes.data.deletehash,
'courseKey': courseKey,
'year': req.body.year,
'period': req.body.period,
'active': utils.settings.defaultImageActive,
'uploader': utils.settings.defaultUploaderName,
});
})
.then((docs)=>{
utils.courseItemIncrease(courseKey, req.body.year);
res.render('upload', {error: false, resultMessage: 'Το θέμα αποθηκεύτηκε!', courses: utils.courses.all, years: utils.settings.years});
})
.catch(function (err) {
if(utils.settings.log500){
utils.log500(err, req);
}
res.render('upload', {error: true, resultMessage: 'Κάτι πήγε στραβά, παρακαλώ δοκιμάστε ξανά.', courses: utils.courses.all, years: utils.settings.years});
});
});
app.use(function (req, res, next) { //default 404
if(utils.settings.log404 && req.originalUrl != '/favicon.ico'){
utils.log404(req);
}
res.status(404).render("404");
});
const port = process.env.PORT || utils.settings.defaultPort;
app.listen(port, function () {
console.log('App listening on port '+port+'!');
});