crunch-node/routes-photo.js
2016-01-23 16:24:06 -05:00

168 lines
6.4 KiB
JavaScript

// Routes for photo administration.
var config = require('./config.js').config;
var multer = require('multer');
var upload = multer({ dest: config.uploadDir });
module.exports = function(app) {
// 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;
}
database.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});
});
}
);
app.get('/admin/photo/view/:id?',
function (req, res, next) {
if (req.params.id) {
database.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');
}
}
);
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(' ');
}
var extension = req.file.originalname.split('.').slice(-1);
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 != "" ){
var slug = helper.makeSlug(title+'-'+photoDate);
}
else {
var cleanFileName = req.file.originalname.split('.').slice(0,-1).join('-');
var slug = helper.makeSlug(cleanFileName+'-'+photoDate);
}
var mimetype = req.file.mimetype;
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;
database.createPhoto(title, slug, markdown, extension, mimetype, photoDate, updatedDate, createDate, path, function(err, row) {
if (err) console.log(err);
console.log(row);
database.tagPhoto(row.id, tags);
req.flash('successNotice', 'Photo created.');
res.redirect('/admin/photo/view/'+row.id);
});
}
);
app.get('/admin/photo/edit/:id',
function(req, res, next) {
var id = req.params.id;
async.parallel({
photoTags: function(callback) {
database.getPhotoTagsAsString(id, function(tagString) {
callback(null, tagString);
})
},
photo: function(callback) {
database.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,
srcPath: '/'+results.photo.path,
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');
database.tagPhoto(id, tags);
database.updatePhoto(id, title, slug, markdown, photoDate, function(err, row) {
req.flash('successNotice', 'Photo updated.');
res.redirect('/admin/photo/view/'+id);
});
}
);
app.get('/admin/photo/regenerate/:id',
function(req, res, next) {
console.log('Generating resized images for: '+req.params.id);
genStatic.generateStatic(function(err){ if (err) console.log(err) });
genPhotos.generatePhotoSizesById(req.params.id, function(err) {
if (!err) {
genPhotos.generatePhotoPage(req.params.id, function(err) {
if (!err) {
req.flash("successNotice", "Photo regenerated");
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);
}
})
}
else {
req.flash('failureNotice', 'Photo was unable to be resized.');
res.redirect('/admin/photo/view/'+req.params.id);
}
});
}
);
}