-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (127 loc) · 4.36 KB
/
server.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
var util = require('util'),
express = require('express'),
expressValidator = require('express-validator'),
app = express(),
email = require("./node_modules/emailjs/email"),
lessMiddleware = require('less-middleware'),
mustache = require('mustache'),
fs = require('fs'),
path = require('path'),
ExifImage = require('exif').ExifImage;
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(lessMiddleware(__dirname + '/public', {force: true}));
app.use(express.static(__dirname + '/public'));
var engines = require('consolidate');
app.engine('html', engines.mustache);
app.set('views', __dirname + '/views')
app.set('view engine', 'html');
app.get('/v2', function(req, res) {
console.log("sending html");
var files = (fs.readdirSync(__dirname + '/public/images/gallery'));
var images = Array();
for (var i in files) {
if(path.extname(files[i]) === ".jpg") {
images.push(files[i]);
}
}
var responseJson = Array();
for (var i = 0; i < images.length; i++) {
var imgName = images[i];
getImageExif(imgName, function(exifData, name) {
//console.log("imgpath: " + imgPath);
responseJson.push({
path: '/images/gallery/' + name,
year: exifData.exif.CreateDate.substr(0, 4),
name: name.split('.jpg')[0],
exif: exifData.exif
});
if (responseJson.length >= images.length) {
responseJson = responseJson.sort(alphabeticalCompare);
console.log(responseJson);
res.render('index', { images: responseJson});
}
});
}
});
function alphabeticalCompare(a,b) {
if (a.name < b.name)
return -1;
else {
return 1;
}
}
function getImageExif(imgName, callback) {
try {
new ExifImage({ image : __dirname + '/public/images/gallery/' + imgName }, function (error, exifData) {
if (error) {
console.log('Image: ' + imgName + ' - Error: '+error.message);
} else {
callback(exifData, imgName);
}
});
} catch (error) {
console.log('Error: ' + error.message);
}
}
app.post('/message', function(req, res){
req.sanitize('name').xss();
req.sanitize('email').xss();
req.sanitize('message').xss();
req.assert('name', 'Please enter your name').notEmpty();
req.assert('email', 'Please enter a valid email').isEmail();
req.assert('message', 'Please enter your message').notEmpty();
var name = req.param('name');
var email_address = req.param('email');
var message_content = req.param('message');
var copy = req.param('copy');
var mappedErrors = req.validationErrors();
if(mappedErrors){
console.log(mappedErrors);
// console.log('There have been validation errors: ' + util.inspect(mappedErrors));
res.send(util.inspect(mappedErrors), 422);
return;
}
console.log('form entry success');
// send email
var server = email.server.connect({
user: "[email protected]",
password:"",
host: "smtpout.secureserver.net",
ssl: true
});
// send the message and get a callback with an error or details of the message that was sent
server.send({
text: message_content,
from: name+" <"+email_address+">",
to: "Kaijian Gao <[email protected]>",
cc: "Website <[email protected]>",
subject: "Message from website"
}, function(err, message) {
// send a copy of the message if copy is requested
if(copy != undefined && copy == 'on'){
server.send({
text: "This email is a confirmation that you have sent the following message to Kaijian Gao: \n" + message_content,
from: "Kaijian Gao <[email protected]>",
to: name+" <"+email_address+">",
cc: "",
subject: "Confirmation for the message you sent to Gao"
}, function(err, message) { console.log(err || message); });
}
console.log(err || message);
if(err){
console.log('=====Error message!!=====: '+err);
res.send('Failed to send message because of error: '+err, 500);
} else {
//success!
res.json({
response: 'message has been sent!'
});
}
});
});
app.listen(3000, '', '', function(){
console.log('- website running on port 3000');
});