This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
204 lines (174 loc) · 5.17 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
203
204
var express = require('express');
var passport = require('passport');
var settings = require('./settings');
// settings.credentials().db_name
var mysql = require("mysql");
var fs = require('fs');
var _ = require('underscore');
var multer = require('multer');
var bodyParser = require("body-parser");
var upload = multer({
dest: 'public/images',
rename: function(fieldname, filename) {
return filename;
},
});
var LocalStrategy = require('passport-local').Strategy;
var db = require('./db');
// Configure the local strategy for use by Passport.
//
// The local strategy require a `verify` function which receives the credentials
// (`username` and `password`) submitted by the user. The function must verify
// that the password is correct and then invoke `cb` with a user object, which
// will be set at `req.user` in route handlers after authentication.
passport.use(new LocalStrategy(
function(username, password, cb) {
db.users.findByUsername(username, function(err, user) {
if (err) { return cb(err); }
if (!user) { return cb(null, false); }
if (user.password != password) { return cb(null, false); }
return cb(null, user);
});
}));
// Configure Passport authenticated session persistence.
//
// In order to restore authentication state across HTTP requests, Passport needs
// to serialize users into and deserialize users out of the session. The
// typical implementation of this is as simple as supplying the user ID when
// serializing, and querying the user record by ID from the database when
// deserializing.
passport.serializeUser(function(user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
db.users.findById(id, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
});
// Create a new Express application.
var app = express();
// Configure view engine to render EJS templates.
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// Use application-level middleware for common functionality, including
// logging, parsing, and session handling.
app.use(require('morgan')('combined'));
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json());
app.use(express.static('public'));
/**
* Homepage
*/
app.get('/',
function(req, res) {
res.render('index', { user: req.user });
});
/**
* User login page
*/
app.get('/login',
function(req, res){
if (!req.user) {
res.render('login');
}
else {
res.redirect('upload');
}
});
/**
* Handle submission of the user login form
*/
app.post('/login',
passport.authenticate('local', {
successRedirect: 'upload',
failureRedirect: 'logout'
}));
/**
* Allow the user to logout
*/
app.get('/logout',
function(req, res){
req.logout();
res.render('index');
});
/**
* Generate the image upload page
*/
app.get('/upload',
function(req, res){
res.render('upload');
});
/**
* Handle submission of the file upload form
*/
app.post('/file-upload',
upload.single('image'), function (req, res, next) {
if (req.user) {
//Get the database username and password
var connection = mysql.createConnection(settings.credentials());
//Open a connection to the database
connection.connect();
//Write to the database
connection.query('INSERT INTO images SET ?', {path: req.file.originalname}, function(err, result) {
if (err) {
throw err;
}
else {
//Rename files after they are uploaded
fs.rename(req.file.path, 'public/images/' + req.file.originalname);
//Return to the upload form for more uploading
res.redirect('upload');
}
});
//Close the database connection
connection.end()
}
else {
res.send('no');
}
});
/**
* List all the photos currently in the system
*/
app.get('/api/pets',
function(req, res){
//Get the database username and password
var connection = mysql.createConnection(settings.credentials());
//Open a connection to the database
connection.connect();
//Write to the database
connection.query('SELECT path FROM images', function(err, result){
_.each(result, function(item, index, list){
result[index] = item.path;
});
res.send(result);
});
//todo, cache this
});
/**
* Allow deletion of a photo
*/
app.post('/delete', function (req, res){
if (req.user) {
fs.unlink('public/images/' + req.body.image);
//Delete from the database
//@todo make this work
// var connection = mysql.createConnection(settings.credentials());
// connection.connect();
// connection.query('DELETE FROM images WHERE path="' + req.body.image + '"')
res.send('yes');
}
else {
//todo, does this work?
res.send('no');
}
});
app.listen(3000);
console.log("its alive!!!!!");