data. Made a lot of progress on #1 with additional work on the rendering templates.
195 lines
7.3 KiB
JavaScript
195 lines
7.3 KiB
JavaScript
// Routes for photo administration.
|
|
var config = require('./config.js').config;
|
|
var multer = require('multer');
|
|
var ExifImage = require('exif').ExifImage;
|
|
var jade = require('jade');
|
|
var upload = multer({ dest: config.uploadDir });
|
|
var uuid = require('node-uuid');
|
|
|
|
module.exports = function(app) {
|
|
|
|
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;
|
|
}
|
|
|
|
Photo.getPhotos(count, start, function(err, photos) {
|
|
res.render('admin-photo-list', {
|
|
successNotice: req.flash('successNotice'),
|
|
failureNotice: req.flash('failureNotice'),
|
|
photos,
|
|
user: req.user
|
|
});
|
|
});
|
|
}
|
|
);
|
|
|
|
app.get('/admin/photo/view/:uuid?',
|
|
function (req, res, next) {
|
|
if (req.params.uuid) {
|
|
Photo.getByUUID(req.params.uuid, function(err, photo) {
|
|
var jadeOut = jade.renderFile('views/partial-photo.jade', {
|
|
pretty: true,
|
|
photo: photo
|
|
});
|
|
res.render('admin-photo-view', {
|
|
successNotice: req.flash('successNotice'),
|
|
failureNotice: req.flash('failureNotice'),
|
|
photo,
|
|
htmlSnippet: jadeOut,
|
|
user: req.user
|
|
});
|
|
});
|
|
}
|
|
else {
|
|
res.redirect('/admin/photo/list');
|
|
}
|
|
}
|
|
);
|
|
|
|
app.get('/admin/photo/new',
|
|
function(req, res, next) {
|
|
res.render('admin-photo-new', {
|
|
successNotice: req.flash('successNotice'),
|
|
failureNotice: req.flash('failureNotice'),
|
|
user: req.user
|
|
});
|
|
}
|
|
);
|
|
|
|
app.post('/admin/photo/new',
|
|
upload.single('photo'),
|
|
function(req, res, next) {
|
|
var photo = new Photo({'uuid': uuid.v1()});
|
|
if (req.body.title != '') {
|
|
photo.set('title', req.body.title);
|
|
} else {
|
|
photo.set('title', req.file.originalname.split('.').slice(0,-1).join(' '));
|
|
}
|
|
photo.set('extension', req.file.originalname.split('.').slice(-1));
|
|
if (req.body.photoDate != "") {
|
|
photo.set('photoDate', new Date(req.body.photoDate));
|
|
} else {
|
|
photo.set('photoDate', new Date());
|
|
}
|
|
if (req.body.slug != "") {
|
|
photo.set('slug', req.body.slug);
|
|
} else {
|
|
photo.set('slug', photo.makeSlug());
|
|
}
|
|
photo.set('mimetype', req.file.mimetype);
|
|
photo.set('markdown', req.body.markdown);
|
|
photo.set('createdDate', new Date());
|
|
photo.set('masterPath', req.file.path);
|
|
photo.tagPhoto(req.body.tags);
|
|
if (req.body.published) {
|
|
photo.set("published", true);
|
|
} else {
|
|
photo.set("published", false);
|
|
}
|
|
|
|
ExifImage({image: photo.get('masterPath')}, function (err, exifData) {
|
|
if (!err) {
|
|
photo.set('exif', exifData.exif || {});
|
|
photo.set('gps', exifData.gps || {});
|
|
photo.save(null, function (err) {
|
|
if (!err) {
|
|
req.flash('successNotice', 'Photo created.');
|
|
res.redirect('/admin/photo/view/'+photo.get('uuid'));
|
|
} else {
|
|
console.log(err);
|
|
req.flash('failureNotice', 'Photo failed to be created');
|
|
res.redirect(req.header('Referer') || '/admin');
|
|
}
|
|
});
|
|
} else {
|
|
console.log(err);
|
|
req.flash('failureNotice', 'Photo failed to be created');
|
|
res.redirect(req.header('Referer') || '/admin');
|
|
}
|
|
});
|
|
}
|
|
);
|
|
|
|
app.get('/admin/photo/edit/:uuid',
|
|
function(req, res, next) {
|
|
Photo.getByUUID(req.params.uuid, function (err, photo) {
|
|
var tags = ""
|
|
for (i=0; i<photo.get("tags").length; i++) {
|
|
tags += photo.get("tags")[i].name + " ";
|
|
}
|
|
res.render('admin-photo-edit', {
|
|
successNotice: req.flash('successNotice'),
|
|
failureNotice: req.flash('failureNotice'),
|
|
photo: photo,
|
|
tags: tags,
|
|
user: req.user
|
|
});
|
|
})
|
|
}
|
|
);
|
|
|
|
app.post('/admin/photo/edit/:uuid',
|
|
function(req, res, next) {
|
|
Photo.getByUUID(req.params.uuid, function (err, photo) {
|
|
photo.set('title', req.body.title);
|
|
if (req.body.slug != '') {
|
|
photo.set('slug', req.body.slug);
|
|
} else {
|
|
photo.set('slug', photo.makeSlug());
|
|
}
|
|
photo.set('markdown', req.body.markdown);
|
|
photo.set('photoDate', new Date(req.body.photoDate));
|
|
photo.tagPhoto(req.body.tags);
|
|
if (req.body.published) {
|
|
photo.set("published", true);
|
|
} else {
|
|
photo.set("published", false);
|
|
}
|
|
console.log('Updating photo: '+photo.get('title'));
|
|
photo.save(null, function (err) {
|
|
if (!err) {
|
|
console.log('Photo updated.');
|
|
req.flash('successNotice', 'Photo updated.');
|
|
res.redirect('/admin/photo/edit/'+photo.get('uuid'));
|
|
} else {
|
|
console.log(err);
|
|
req.flash('failureNotice', 'Photo failed to update.');
|
|
res.redirect('/admin/photo/edit/'+photo.get('uuid'));
|
|
}
|
|
});
|
|
});
|
|
}
|
|
);
|
|
|
|
app.get('/admin/photo/build/:uuid',
|
|
function(req, res, next) {
|
|
if (req.params.uuid) {
|
|
Static.updateBuildFolder(function (err) {
|
|
if (err) console.log(err);
|
|
});
|
|
Photo.getByUUID(req.params.uuid, function (err, photo) {
|
|
console.log('Building '+photo.get('title'));
|
|
photo.build(function (err) {
|
|
if (!err) {
|
|
console.log(photo.get('title') + ' built.');
|
|
req.flash("successNotice", 'Photo "'+photo.get('title')+'" built');
|
|
res.redirect(req.header('Referer') || '/admin');
|
|
}
|
|
else {
|
|
console.log(err);
|
|
req.flash('failureNotice', 'Unable to build photo.');
|
|
res.redirect(req.header('Referer') || '/admin');
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
res.redirect('/admin/photos/list');
|
|
}
|
|
}
|
|
);
|
|
}
|