2015-11-15 03:07:13 +00:00
|
|
|
var express = require('express');
|
|
|
|
var flash = require('express-flash');
|
|
|
|
var path = require('path');
|
|
|
|
var passport = require('passport');
|
|
|
|
var Strategy = require('passport-local').Strategy;
|
|
|
|
var async = require('async');
|
2015-11-22 14:36:02 +00:00
|
|
|
var multer = require('multer');
|
|
|
|
var fs = require('fs');
|
2015-11-22 20:03:33 +00:00
|
|
|
var markdown = require( "markdown" ).markdown;
|
2015-11-15 03:07:13 +00:00
|
|
|
|
|
|
|
var helper = require('./helper.js');
|
|
|
|
var db = require('./db.js');
|
2015-12-01 03:06:36 +00:00
|
|
|
var genPosts = require('./genPosts.js');
|
|
|
|
var genStatic = require('./genStatic.js');
|
|
|
|
var genPhotos = require('./genPhotos.js');
|
2015-11-15 03:07:13 +00:00
|
|
|
|
|
|
|
var app = express();
|
|
|
|
|
2015-11-22 15:05:41 +00:00
|
|
|
// Get config variables
|
|
|
|
var config = require('./config.js').config;
|
|
|
|
|
2015-11-15 03:07:13 +00:00
|
|
|
// view engine setup
|
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
|
app.set('view engine', 'jade');
|
|
|
|
|
|
|
|
// Make HTML pretty while we're setting up our jade templates
|
|
|
|
// but it doesn't need to be pretty in production.
|
|
|
|
|
|
|
|
// uncomment after placing your favicon in /public
|
|
|
|
//app.use(require('serve-favicon')(path.join(__dirname, 'public',
|
|
|
|
// 'favicon.ico')));
|
|
|
|
app.use(flash());
|
|
|
|
app.use(require('morgan')('dev'));
|
|
|
|
app.use(require('cookie-parser')());
|
|
|
|
app.use(require('body-parser').urlencoded({ extended: true }));
|
|
|
|
app.use(require('express-session')({ secret: 'amdasdfasdfamd',
|
|
|
|
resave: true, saveUninitialized: true }));
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
|
2015-11-22 15:05:41 +00:00
|
|
|
var upload = multer({ dest: config.uploadDir });
|
2015-11-15 03:07:13 +00:00
|
|
|
|
|
|
|
// Setup authentication via twitter.
|
|
|
|
passport.use(new Strategy(
|
|
|
|
function(username, password, done) {
|
|
|
|
db.getUser(username, password, function(user) {
|
|
|
|
if (!user) { return done(null, false); }
|
|
|
|
return done(null, user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
passport.serializeUser(function(user, done) {
|
|
|
|
done(null, user.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
passport.deserializeUser(function(id, done) {
|
|
|
|
db.getUserById(id, function (user) {
|
|
|
|
if (!user) { return done(false); }
|
|
|
|
done(null, user);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
|
2015-11-22 14:36:02 +00:00
|
|
|
// Require logins for all admin pages.
|
|
|
|
app.all('/admin/*', require('connect-ensure-login').ensureLoggedIn());
|
|
|
|
|
|
|
|
// User management routing
|
2015-11-15 03:07:13 +00:00
|
|
|
app.get('/login', function(req, res) {
|
|
|
|
res.render('admin-login', {user: req.user});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/login',
|
|
|
|
passport.authenticate('local', { successReturnToOrRedirect: '/admin', failureRedirect: '/login' })
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get('/logout', function(req, res) {
|
|
|
|
req.logout();
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
|
|
|
|
2015-11-22 14:36:02 +00:00
|
|
|
// Post management routing
|
2015-11-15 03:07:13 +00:00
|
|
|
app.get('/admin/post/list/:start?',
|
|
|
|
function(req, res, next) {
|
|
|
|
var count = 25;
|
|
|
|
if (req.params.start) {
|
|
|
|
var start = req.params.start;
|
|
|
|
} else {
|
|
|
|
var start = 0;
|
|
|
|
}
|
|
|
|
db.listPosts(count, start, function(posts){
|
|
|
|
for (post in posts) {
|
|
|
|
var date = new Date(posts[post].postDate);
|
|
|
|
posts[post].dateString = date.getFullYear() + '-' +
|
|
|
|
("0" + (date.getMonth()+1)).slice(-2) + '-' +
|
|
|
|
("0" + date.getDate()).slice(-2);
|
|
|
|
}
|
|
|
|
res.render('admin-post-list', {posts, user: req.user});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-11-22 20:03:33 +00:00
|
|
|
app.get('/admin/post/view/:id?',
|
|
|
|
function (req, res, next) {
|
|
|
|
if (req.params.id) {
|
|
|
|
db.getPostById(req.params.id, function(row) {
|
|
|
|
res.render('admin-post-view', {
|
|
|
|
successNotice: req.flash('successNotice'),
|
|
|
|
failureNotice: req.flash('failureNotice'),
|
|
|
|
date: helper.epochToShortDateString(row.postDate),
|
|
|
|
post: row,
|
|
|
|
content: markdown.toHTML(row.markdown),
|
|
|
|
user: req.user
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.redirect('/admin/post/list');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-11-15 03:07:13 +00:00
|
|
|
app.get('/admin/post/new',
|
|
|
|
function(req, res, next) {
|
|
|
|
db.listCategories(function(rows){
|
|
|
|
res.render('admin-post-new', { categories: rows, user: req.user });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/admin/post/new',
|
|
|
|
function(req, res, next) {
|
|
|
|
var title = req.body.title;
|
|
|
|
if (req.body.slug != "") {
|
2015-11-22 14:36:02 +00:00
|
|
|
var slug = req.body.slug;
|
2015-11-15 03:07:13 +00:00
|
|
|
} else {
|
2015-11-22 14:36:02 +00:00
|
|
|
var slug = helper.makeSlug(title);
|
2015-11-15 03:07:13 +00:00
|
|
|
}
|
|
|
|
var markdown = req.body.markdown;
|
2015-11-22 14:36:02 +00:00
|
|
|
if (req.body.postDate != "") {
|
|
|
|
var postDate = helper.dateToEpoch(new Date(req.body.postDate));
|
|
|
|
} else {
|
|
|
|
var postDate = helper.dateToEpoch(new Date());
|
|
|
|
}
|
2015-11-15 03:07:13 +00:00
|
|
|
var updatedDate = postDate;
|
|
|
|
var createDate = helper.dateToEpoch(new Date());
|
|
|
|
var categoryName = req.body.category;
|
2015-11-22 14:36:02 +00:00
|
|
|
var tags = helper.parseTags(req.body.tags);
|
2015-11-15 03:07:13 +00:00
|
|
|
|
|
|
|
db.createPost(title, slug, markdown, postDate, updatedDate, createDate, function(err, row) {
|
|
|
|
db.setPostCategory(row.id, categoryName, function(category) {
|
2015-11-22 14:36:02 +00:00
|
|
|
db.tagPost(row.id, tags);
|
2015-11-15 03:07:13 +00:00
|
|
|
req.flash('successNotice', 'Post created.');
|
|
|
|
res.redirect('/admin/post/edit/'+row.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/admin/post/edit/:id',
|
|
|
|
function(req, res, next) {
|
|
|
|
var id = req.params.id;
|
|
|
|
async.parallel({
|
|
|
|
categories: function(callback) {
|
|
|
|
db.listCategories(function(categories) {
|
|
|
|
callback(null, categories);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
postCategory: function(callback) {
|
|
|
|
db.getPostCategory(id, function(category) {
|
|
|
|
callback(null, category);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
postTags: function(callback) {
|
|
|
|
db.getPostTagsAsString(id, function(tagString) {
|
|
|
|
callback(null, tagString);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
post: function(callback) {
|
|
|
|
db.getPostById(id, function(post) {
|
|
|
|
callback(null, post);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(err, results) {
|
2015-11-22 14:36:02 +00:00
|
|
|
if (results.postCategory) {
|
|
|
|
var categoryName = results.postCategory.name;
|
|
|
|
} else {
|
|
|
|
var categoryName = null;
|
|
|
|
}
|
2015-11-15 03:07:13 +00:00
|
|
|
res.render('admin-post-edit', {
|
|
|
|
successNotice: req.flash('successNotice'),
|
|
|
|
failureNotice: req.flash('failureNotice'),
|
|
|
|
categories: results.categories,
|
2015-11-22 14:36:02 +00:00
|
|
|
postCategory: categoryName,
|
2015-11-15 03:07:13 +00:00
|
|
|
post: results.post,
|
|
|
|
postTags: results.postTags,
|
|
|
|
formattedDate: helper.epochToDateString(results.post.postDate),
|
|
|
|
user: req.user
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/admin/post/edit/:id',
|
|
|
|
function(req, res, next) {
|
|
|
|
var id = req.params.id;
|
|
|
|
var title = req.body.title;
|
2015-11-22 14:36:02 +00:00
|
|
|
var slug = req.body.slug;
|
2015-11-15 03:07:13 +00:00
|
|
|
var markdown = req.body.markdown;
|
|
|
|
var postDate = helper.dateToEpoch(new Date(req.body.postDate));
|
|
|
|
var categoryName = req.body.category;
|
|
|
|
var tags = helper.parseTags(req.body.tags);
|
2015-11-22 14:36:02 +00:00
|
|
|
console.log('Post '+id+' update request received');
|
2015-11-15 03:07:13 +00:00
|
|
|
db.tagPost(id, tags);
|
|
|
|
db.updatePost(id, title, slug, markdown, postDate, function(err, row) {
|
|
|
|
db.setPostCategory(id, categoryName, function(err) {
|
|
|
|
req.flash('successNotice', 'Post updated.');
|
|
|
|
res.redirect('/admin/post/edit/'+id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-12-01 03:06:36 +00:00
|
|
|
app.get('/admin/post/regenerate/:id?',
|
2015-11-15 03:07:13 +00:00
|
|
|
function(req, res, next) {
|
|
|
|
if (req.params.id) {
|
2015-12-01 03:06:36 +00:00
|
|
|
genStatic.generateStatic(function(err){ if (err) console.log(err) });
|
|
|
|
genPosts.generatePost(req.params.id, function(err) {
|
2015-11-15 03:07:13 +00:00
|
|
|
if (!err) {
|
2015-12-01 03:06:36 +00:00
|
|
|
req.flash('successNotice', 'Post regenerated successfully.');
|
2015-11-22 20:03:33 +00:00
|
|
|
res.redirect('/admin/post/view/'+req.params.id);
|
2015-11-15 03:07:13 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-12-01 03:06:36 +00:00
|
|
|
req.flash('failureNotice', 'Post regeneration failed, check logs.');
|
2015-11-22 20:03:33 +00:00
|
|
|
res.redirect('/admin/post/view/'+req.params.id);
|
2015-11-15 03:07:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.redirect('/admin/post/list');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-11-22 14:36:02 +00:00
|
|
|
// Photo management Routing
|
|
|
|
app.get('/admin/photo/list/:start?',
|
|
|
|
function(req, res, next) {
|
|
|
|
var count = 25;
|
|
|
|
if (req.params.start) {
|
|
|
|
var start = req.params.start;
|
|
|
|
} else {
|
|
|
|
var start = 0;
|
|
|
|
}
|
|
|
|
db.listPhotos(count, start, function(photos){
|
|
|
|
for (photo in photos) {
|
|
|
|
var date = new Date(photos[photo].photoDate);
|
|
|
|
photos[photo].dateString = date.getFullYear() + '-' +
|
|
|
|
("0" + (date.getMonth()+1)).slice(-2) + '-' +
|
|
|
|
("0" + date.getDate()).slice(-2);
|
|
|
|
}
|
|
|
|
res.render('admin-photo-list', {photos, user: req.user});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-11-22 15:05:41 +00:00
|
|
|
app.get('/admin/photo/view/:id?',
|
|
|
|
function (req, res, next) {
|
|
|
|
if (req.params.id) {
|
|
|
|
db.getPhotoById(req.params.id, function(row) {
|
|
|
|
res.render('admin-photo-view', {
|
|
|
|
successNotice: req.flash('successNotice'),
|
|
|
|
failureNotice: req.flash('failureNotice'),
|
|
|
|
photo: row,
|
|
|
|
srcPath: '/'+row.path,
|
|
|
|
user: req.user
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.redirect('/admin/photo/list');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-11-22 14:36:02 +00:00
|
|
|
app.get('/admin/photo/new',
|
|
|
|
function(req, res, next) {
|
|
|
|
res.render('admin-photo-new', { user: req.user });
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/admin/photo/new',
|
|
|
|
upload.single('photo'),
|
|
|
|
function(req, res, next) {
|
|
|
|
console.log(req.file);
|
|
|
|
if ( req.body.title != "") {
|
|
|
|
var title = req.body.title;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var title = req.file.originalname.split('.').slice(0,-1).join(' ');
|
|
|
|
}
|
2015-12-01 03:06:36 +00:00
|
|
|
var extension = req.file.originalname.split('.').slice(-1);
|
2015-11-22 14:36:02 +00:00
|
|
|
if (req.body.photoDate != "") {
|
|
|
|
var photoDate = helper.dateToEpoch(new Date(req.body.photoDate));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var photoDate = helper.dateToEpoch(new Date());
|
|
|
|
}
|
|
|
|
if (req.body.slug != "") {
|
|
|
|
var slug = req.body.slug;
|
|
|
|
}
|
|
|
|
else if ( req.body.title != "" ){
|
2015-11-22 15:05:41 +00:00
|
|
|
var slug = helper.makeSlug(title+'-'+photoDate);
|
2015-11-22 14:36:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var cleanFileName = req.file.originalname.split('.').slice(0,-1).join('-');
|
|
|
|
var slug = helper.makeSlug(cleanFileName+'-'+photoDate);
|
|
|
|
}
|
2015-12-01 03:06:36 +00:00
|
|
|
var mimetype = req.file.mimetype;
|
2015-11-22 14:36:02 +00:00
|
|
|
var tags = helper.parseTags(req.body.tags);
|
|
|
|
var markdown = req.body.markdown;
|
|
|
|
var updatedDate = photoDate;
|
|
|
|
var createDate = helper.dateToEpoch(new Date());
|
|
|
|
var categoryName = req.body.category;
|
|
|
|
var path = req.file.path;
|
|
|
|
|
2015-12-01 03:06:36 +00:00
|
|
|
db.createPhoto(title, slug, markdown, extension, mimetype, photoDate, updatedDate, createDate, path, function(err, row) {
|
|
|
|
if (err) console.log(err);
|
2015-11-22 14:36:02 +00:00
|
|
|
console.log(row);
|
|
|
|
db.tagPhoto(row.id, tags);
|
|
|
|
req.flash('successNotice', 'Photo created.');
|
2015-11-22 15:05:41 +00:00
|
|
|
res.redirect('/admin/photo/view/'+row.id);
|
2015-11-22 14:36:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/admin/photo/edit/:id',
|
|
|
|
function(req, res, next) {
|
|
|
|
var id = req.params.id;
|
|
|
|
async.parallel({
|
|
|
|
photoTags: function(callback) {
|
|
|
|
db.getPhotoTagsAsString(id, function(tagString) {
|
|
|
|
callback(null, tagString);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
photo: function(callback) {
|
|
|
|
db.getPhotoById(id, function(photo) {
|
|
|
|
callback(null, photo);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(err, results) {
|
|
|
|
res.render('admin-photo-edit', {
|
|
|
|
successNotice: req.flash('successNotice'),
|
|
|
|
failureNotice: req.flash('failureNotice'),
|
|
|
|
photo: results.photo,
|
2015-11-22 15:05:41 +00:00
|
|
|
srcPath: '/'+results.photo.path,
|
2015-11-22 14:36:02 +00:00
|
|
|
photoTags: results.photoTags,
|
|
|
|
formattedDate: helper.epochToDateString(results.photo.photoDate),
|
|
|
|
user: req.user
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/admin/photo/edit/:id',
|
|
|
|
function(req, res, next) {
|
|
|
|
var id = req.params.id;
|
|
|
|
var title = req.body.title;
|
|
|
|
var slug = req.body.slug;
|
|
|
|
var markdown = req.body.markdown;
|
|
|
|
var photoDate = helper.dateToEpoch(new Date(req.body.photoDate));
|
|
|
|
var tags = helper.parseTags(req.body.tags);
|
|
|
|
console.log('Post '+id+' update request received');
|
|
|
|
db.tagPhoto(id, tags);
|
|
|
|
db.updatePhoto(id, title, slug, markdown, photoDate, function(err, row) {
|
|
|
|
req.flash('successNotice', 'Photo updated.');
|
2015-11-22 15:05:41 +00:00
|
|
|
res.redirect('/admin/photo/view/'+id);
|
2015-11-22 14:36:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-12-01 03:06:36 +00:00
|
|
|
app.get('/admin/photo/regenerate/:id',
|
|
|
|
function(req, res, next) {
|
|
|
|
console.log('Generating resized images for: '+req.params.id);
|
|
|
|
genPhotos.generatePhotoSizesById(req.params.id, function(err) {
|
|
|
|
if (!err) {
|
|
|
|
req.flash('successNotice', 'Resized images created.');
|
|
|
|
res.redirect('/admin/photo/view/'+req.params.id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
req.flash('failureNotice', 'Photo was unable to be resized.');
|
|
|
|
res.redirect('/admin/photo/view/'+req.params.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-11-22 14:36:02 +00:00
|
|
|
|
|
|
|
// Admin dashboard page.
|
2015-11-15 03:07:13 +00:00
|
|
|
app.get('/admin',
|
|
|
|
function(req, res, next) {
|
|
|
|
async.parallel({
|
|
|
|
categories: function(callback) {
|
|
|
|
db.countCategories(function(count) {
|
|
|
|
callback(null, count);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
posts: function(callback) {
|
|
|
|
db.countPosts(function(count) {
|
|
|
|
callback(null, count);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
galleries: function(callback) {
|
|
|
|
db.countGalleries(function(count) {
|
|
|
|
callback(null, count);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
photos: function(callback) {
|
|
|
|
db.countPhotos(function(count) {
|
|
|
|
callback(null, count);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tags: function(callback) {
|
|
|
|
db.countTags(function(count) {
|
|
|
|
callback(null, count);
|
|
|
|
});
|
|
|
|
},
|
2015-12-01 03:06:36 +00:00
|
|
|
regenerateDate: function(callback) {
|
|
|
|
db.getLastRegenerateDate(function(date) {
|
2015-11-22 14:36:02 +00:00
|
|
|
var dateString = helper.epochToDateString(date.date).slice(0,-12);
|
2015-11-15 03:07:13 +00:00
|
|
|
callback(null, dateString);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
uploadDate: function(callback) {
|
|
|
|
db.getLastUploadDate(function(date) {
|
2015-11-22 14:36:02 +00:00
|
|
|
var dateString = helper.epochToDateString(date.date).slice(0,-12);
|
2015-11-15 03:07:13 +00:00
|
|
|
callback(null, dateString);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(err, results) {
|
|
|
|
res.render('admin-dashboard', {
|
2015-11-22 14:36:02 +00:00
|
|
|
successNotice: req.flash('successNotice'),
|
|
|
|
failureNotice: req.flash('failureNotice'),
|
2015-11-15 03:07:13 +00:00
|
|
|
categories: results.categories,
|
|
|
|
posts: results.posts,
|
|
|
|
galleries: results.galleries,
|
|
|
|
photos: results.photos,
|
|
|
|
tags: results.tags,
|
2015-12-01 03:06:36 +00:00
|
|
|
regenerateDate: results.regenerateDate,
|
2015-11-15 03:07:13 +00:00
|
|
|
uploadDate: results.uploadDate,
|
|
|
|
user: req.user});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2015-11-22 14:36:02 +00:00
|
|
|
// Routes for previewing sent versions of the built items.
|
|
|
|
app.get('/blog/*',
|
|
|
|
require('connect-ensure-login').ensureLoggedIn(),
|
|
|
|
function(req, res, next) {
|
|
|
|
if (req.params[0] != '') {
|
2015-12-01 03:06:36 +00:00
|
|
|
var path = __dirname + '/' + config.genDir + '/blog/' + req.params[0];
|
2015-11-22 14:36:02 +00:00
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(path + ' does not exist...');
|
|
|
|
if (path.slice(-1) != '/') path += '/';
|
|
|
|
path += 'index.html'
|
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get('/photos/*',
|
|
|
|
require('connect-ensure-login').ensureLoggedIn(),
|
|
|
|
function(req, res, next) {
|
|
|
|
if (req.params[0] != '') {
|
2015-12-01 03:06:36 +00:00
|
|
|
var path = __dirname + '/' + config.genDir + '/photos/' + req.params[0];
|
2015-11-22 14:36:02 +00:00
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(path + ' does not exist...');
|
|
|
|
if (path.slice(-1) != '/') path += '/';
|
|
|
|
path += 'index.html'
|
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get('/galleries/*',
|
|
|
|
require('connect-ensure-login').ensureLoggedIn(),
|
|
|
|
function(req, res, next) {
|
|
|
|
if (req.params[0] != '') {
|
2015-12-01 03:06:36 +00:00
|
|
|
var path = __dirname + '/' + config.genDir + '/galleries/' + req.params[0];
|
2015-11-22 14:36:02 +00:00
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(path + ' does not exist...');
|
|
|
|
if (path.slice(-1) != '/') path += '/';
|
|
|
|
path += 'index.html'
|
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-11-22 20:03:33 +00:00
|
|
|
app.get('/static/*',
|
|
|
|
require('connect-ensure-login').ensureLoggedIn(),
|
|
|
|
function(req, res, next) {
|
|
|
|
if (req.params[0] != '') {
|
2015-12-01 03:06:36 +00:00
|
|
|
var path = __dirname + '/' + config.genDir + '/static/' + req.params[0];
|
2015-11-22 20:03:33 +00:00
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(path + ' does not exist...');
|
|
|
|
if (path.slice(-1) != '/') path += '/';
|
|
|
|
path += 'index.html'
|
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-12-01 03:06:36 +00:00
|
|
|
app.get('/'+config.uploadDir+'/*',
|
2015-11-22 15:05:41 +00:00
|
|
|
require('connect-ensure-login').ensureLoggedIn(),
|
|
|
|
function(req, res, next) {
|
|
|
|
if (req.params[0] != '') {
|
2015-12-01 03:06:36 +00:00
|
|
|
var path = __dirname + '/'+ config.uploadDir + '/'+ req.params[0];
|
2015-11-22 15:05:41 +00:00
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(path + ' does not exist...');
|
|
|
|
if (path.slice(-1) != '/') path += '/';
|
|
|
|
path += 'index.html'
|
|
|
|
console.log('Trying to serve: ' + path);
|
|
|
|
fs.exists(path, function(exists) {
|
|
|
|
if (exists) {
|
|
|
|
console.log(path + ' exists serving...');
|
|
|
|
res.sendFile(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2015-11-22 14:36:02 +00:00
|
|
|
// Have to have some sort of home page.
|
2015-11-15 03:07:13 +00:00
|
|
|
app.get('/', function(req, res, next) {
|
|
|
|
res.render('admin-index', { title: 'AMDavidson.com', user: req.user });
|
|
|
|
});
|
|
|
|
|
|
|
|
// catch 404 and forward to error handler
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
var err = new Error('Not Found');
|
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
// error handlers
|
|
|
|
|
|
|
|
// development error handler
|
|
|
|
// will print stacktrace
|
|
|
|
if (app.get('env') === 'development') {
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('admin-error', {
|
|
|
|
message: err.message,
|
|
|
|
error: err,
|
|
|
|
user: req.user
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// production error handler
|
|
|
|
// no stacktraces leaked to user
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
user: req.user,
|
|
|
|
error: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.set('port', process.env.PORT || 3000)
|
|
|
|
var server = require('http').createServer(app)
|
|
|
|
|
|
|
|
server.listen(app.get('port'), function() {
|
|
|
|
console.log("Server listening on port " + app.get('port'));
|
|
|
|
});
|