adding photo resizing

This commit is contained in:
Andrew Davidson 2015-11-30 22:06:36 -05:00
parent c32c16d528
commit a3cb6e5626
15 changed files with 299 additions and 213 deletions

52
app.js
View file

@ -10,7 +10,9 @@ var markdown = require( "markdown" ).markdown;
var helper = require('./helper.js');
var db = require('./db.js');
var build = require('./build.js');
var genPosts = require('./genPosts.js');
var genStatic = require('./genStatic.js');
var genPhotos = require('./genPhotos.js');
var app = express();
@ -216,17 +218,17 @@ app.post('/admin/post/edit/:id',
});
});
app.get('/admin/post/rebuild/:id?',
app.get('/admin/post/regenerate/:id?',
function(req, res, next) {
if (req.params.id) {
build.buildStatic(function(err){ if (err) console.log(err) });
build.buildPost(req.params.id, function(err) {
genStatic.generateStatic(function(err){ if (err) console.log(err) });
genPosts.generatePost(req.params.id, function(err) {
if (!err) {
req.flash('successNotice', 'Post rebuilt successfully.');
req.flash('successNotice', 'Post regenerated successfully.');
res.redirect('/admin/post/view/'+req.params.id);
}
else {
req.flash('failureNotice', 'Post rebuild failed, check logs.');
req.flash('failureNotice', 'Post regeneration failed, check logs.');
res.redirect('/admin/post/view/'+req.params.id);
}
});
@ -291,6 +293,7 @@ app.post('/admin/photo/new',
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));
}
@ -307,6 +310,7 @@ app.post('/admin/photo/new',
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;
@ -314,7 +318,8 @@ app.post('/admin/photo/new',
var categoryName = req.body.category;
var path = req.file.path;
db.createPhoto(title, slug, markdown, photoDate, updatedDate, createDate, path, function(err, row) {
db.createPhoto(title, slug, markdown, extension, mimetype, photoDate, updatedDate, createDate, path, function(err, row) {
if (err) console.log(err);
console.log(row);
db.tagPhoto(row.id, tags);
req.flash('successNotice', 'Photo created.');
@ -366,7 +371,20 @@ app.post('/admin/photo/edit/:id',
});
});
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);
}
});
});
// Admin dashboard page.
app.get('/admin',
@ -397,8 +415,8 @@ app.get('/admin',
callback(null, count);
});
},
rebuildDate: function(callback) {
db.getLastRebuildDate(function(date) {
regenerateDate: function(callback) {
db.getLastRegenerateDate(function(date) {
var dateString = helper.epochToDateString(date.date).slice(0,-12);
callback(null, dateString);
});
@ -419,7 +437,7 @@ app.get('/admin',
galleries: results.galleries,
photos: results.photos,
tags: results.tags,
rebuildDate: results.rebuildDate,
regenerateDate: results.regenerateDate,
uploadDate: results.uploadDate,
user: req.user});
}
@ -431,7 +449,7 @@ app.get('/blog/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/build/blog/' + req.params[0];
var path = __dirname + '/' + config.genDir + '/blog/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
@ -459,7 +477,7 @@ app.get('/photos/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/build/photos/' + req.params[0];
var path = __dirname + '/' + config.genDir + '/photos/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
@ -487,7 +505,7 @@ app.get('/galleries/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/build/galleries/' + req.params[0];
var path = __dirname + '/' + config.genDir + '/galleries/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
@ -515,7 +533,7 @@ app.get('/static/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/build/static/' + req.params[0];
var path = __dirname + '/' + config.genDir + '/static/' + req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {
@ -539,11 +557,11 @@ app.get('/static/*',
}
);
app.get('/data/*',
app.get('/'+config.uploadDir+'/*',
require('connect-ensure-login').ensureLoggedIn(),
function(req, res, next) {
if (req.params[0] != '') {
var path = __dirname + '/data/' + req.params[0];
var path = __dirname + '/'+ config.uploadDir + '/'+ req.params[0];
console.log('Trying to serve: ' + path);
fs.exists(path, function(exists) {
if (exists) {

158
build.js
View file

@ -1,158 +0,0 @@
var fs = require('fs');
var mkdirp = require('mkdirp');
var jade = require('jade');
var markdown = require( "markdown" ).markdown;
var async = require('async');
var ncp = require('ncp').ncp;
var helper = require('./helper.js');
var db = require('./db.js');
// Build all archive pages associated with a particular post ID
// This currently calls the functions to make the yearly and monthly archives.
var buildPostArchives = function (id, cb) {
db.getPostById(id, function(post) {
console.log('Building dependencies for Post: '+id);
var date = new Date(post.postDate);
var year = date.getFullYear();
console.log('Building archive for year: '+year);
var month = date.getMonth()+1;
async.parallel({
monthArchiveErr: function(callback) {
buildPostMonthArchive(year, function(err) {
callback(null, err);
});
},
yearArchiveErr: function(callback) {
buildPostYearArchive(month, function(err) {
callback(null, err);
});
}
}, function(err, results) {
if (!yearArchiveErr && !monthArchiveErr) {
cb(null);
}
else if (yearArchiveErr) {
cb(yearArchiveErr);
}
else if (monthArchiveErr) {
cb(monthArchiveErr);
}
else {
cb(null);
}
});
});
}
exports.buildPostArchives = buildPostArchives;
// Function that builds the yearly archive pages.
// TODO: make this not a skeleton.
// TODO: create pagination.
var buildPostYearArchive = function (year, cb) {
console.log('Building archive for year: '+year);
cb(null);
}
exports.buildPostYearArchive = buildPostYearArchive;
// Function that builds the monthly archive pages.
// TODO: Make this not a skeleton.
// TODO: create pagination.
var buildPostMonthArchive = function (month, cb) {
console.log('Building archive for month: '+month);
cb(null);
}
exports.buildPostMonthArchive = buildPostMonthArchive;
// Function to build a single post page.
// TODO: (re)build images as required.
var buildPost = function (id, cb) {
db.getPostById(id, function(post) {
console.log('Fetching post id: '+id);
var title = post.title;
var content = markdown.toHTML(post.markdown);
var slug = post.slug;
var postDate = helper.epochToShortDateString(post.postDate);
var url = '/blog/'+postDate.slice(0,4)+'/'+postDate.slice(5,7)+'/'+slug+'/';
var filepath = 'build'+url;
var filename = filepath+'index.html';
var options = {
pretty: false,
title: title,
content: content,
slug: slug,
url: url,
postDate: postDate
};
console.log('Rendering post: '+title);
var jadeOut = jade.renderFile('views/render-post.jade', options);
console.log('Creating directory: '+filepath);
mkdirp(filepath, function(err) {
if (err) {
console.log(err);
}
else {
console.log('Writing to file: '+filename);
fs.writeFile(filename, jadeOut, 'utf-8', function(err) {
if (err) console.log(err);
cb(err);
});
}
});
});
}
exports.buildPost = buildPost;
// Function to build copy over the static folder to build directory if
// it has a earlier modified time.
var buildStatic = function(cb) {
var sourceFolder = 'data/static';
var destFolder = 'build/static';
async.parallel({
sourceMtime: function(cb) {
console.log('Getting stats for '+sourceFolder);
fs.stat(sourceFolder, function(err, sstats) {
if (err) console.log(err);
console.log(sourceFolder+' mtime: '+sstats.mtime);
cb(null, sstats.mtime);
});
},
destMtime: function(cb) {
console.log('Getting stats for '+destFolder);
fs.stat(destFolder, function(err, dstats) {
if (err) console.log(err);
console.log(destFolder+' mtime: '+dstats.mtime);
cb(null, dstats.mtime);
});
}
}, function(err, results) {
if (results.sourceMtime > results.destMtime) {
console.log(destFolder+' is older than '+sourceFolder);
fs.unlink(destFolder, function(err) {
console.log(destFolder+' deleted.');
ncp(sourceFolder, destFolder, function(err) {
if (!err) {
console.log(sourceFolder+' copied to '+destFolder);
cb(null);
}
else {
cb(err);
}
});
});
}
else {
console.log(destFolder+' is not older than '+sourceFolder+' ignoring...');
}
});
}
exports.buildStatic = buildStatic;

View file

29
db.js
View file

@ -28,15 +28,15 @@ exports.getUserById = function(id, cb) {
});
}
// Function to get the latest rebuild date
// Function to get the latest regenerate date
// Returns a epoch time in seconds
exports.getLastRebuildDate = function(cb) {
db.get('SELECT MAX(lastBuildDate) as date FROM ( \
SELECT lastBuildDate FROM posts \
exports.getLastRegenerateDate = function(cb) {
db.get('SELECT MAX(lastGenerateDate) as date FROM ( \
SELECT lastGenerateDate FROM posts \
UNION \
SELECT lastBuildDate FROM photos \
SELECT lastGenerateDate FROM photos \
UNION \
SELECT lastBuildDate FROM galleries);',
SELECT lastGenerateDate FROM galleries);',
function(err, row) {
cb(row);
});
@ -218,7 +218,7 @@ exports.createPost = function(title, slug, markdown, postDate, updatedDate,
postDate, \
createDate, \
updatedDate, \
lastBuildDate, \
lastGenerateDate, \
lastUpload) \
VALUES (\
"'+title+'", \
@ -492,29 +492,40 @@ exports.getPhotoById = function(id, cb) {
// Function to create a new photo
// Returns the photo id
exports.createPhoto = function(title, slug, markdown, photoDate, updatedDate,
exports.createPhoto = function(title, slug, markdown, extension, mimetype, photoDate, updatedDate,
createDate, path, cb) {
console.log('Creating new photo: '+slug);
db.run('INSERT INTO photos (\
title, \
slug, \
description, \
path, \
extension, \
mimetype, \
photoDate, \
createDate, \
updatedDate, \
lastBuildDate, \
lastGenerateDate, \
lastUpload) \
VALUES (\
"'+title+'", \
"'+slug+'", \
"'+markdown+'", \
"'+path+'", \
"'+extension+'", \
"'+mimetype+'", \
"'+photoDate+'", \
"'+createDate+'", \
"'+updatedDate+'", \
(strftime("%s","1900-01-01 00:00")*1000), \
(strftime("%s","1900-01-01 00:00")*1000));',
function(err, row) {
if (!err) {
console.log('Photo '+slug+' created.');
}
else {
console.log('Error: '+err);
}
db.get('SELECT * FROM photos WHERE title = "'+title+'" AND createDate = '+createDate+';', function(err, row){
if (err) console.log(err);
cb(err, row);

47
genPhotos.js Normal file
View file

@ -0,0 +1,47 @@
var fs = require('fs');
var gm = require('gm');
var mkdirp = require('mkdirp');
var config = require('./config.js').config;
var db = require('./db.js');
var helper = require('./helper.js');
var generatePhotoSizesById = function generatePhotoSizesById(id, cb) {
db.getPhotoById(id, function (photo) {
if (photo) console.log('Got photo record for: '+photo.slug);
var shortDate = helper.epochToShortDateString(photo.photoDate);
var failure = false;
var folder = config.genDir+'/images/'+photo.slug;
mkdirp(folder, function(err) {
if (err) console.log(err);
for (var size in config.imageSizes) {
console.log('Generating '+size+' for '+photo.slug);
var file = folder + '/'+size+'.'+photo.extension;
gm(photo.path)
.autoOrient()
.resize(config.imageSizes[size], config.imageSizes[size])
.write(file,
function(err) {
if (err) {
console.log(photo.slug+' resize failed.');
failure = true;
}
else {
console.log(photo.slug+' resized.');
}
}
);
}
});
cb(failure);
});
}
exports.generatePhotoSizesById = generatePhotoSizesById;

111
genPosts.js Normal file
View file

@ -0,0 +1,111 @@
var fs = require('fs');
var mkdirp = require('mkdirp');
var jade = require('jade');
var markdown = require( "markdown" ).markdown;
var async = require('async');
var helper = require('./helper.js');
var db = require('./db.js');
// Generate all archive pages associated with a particular post ID
// This currently calls the functions to make the yearly and monthly archives.
var generatePostArchives = function (id, cb) {
db.getPostById(id, function(post) {
console.log('Generating dependencies for Post: '+id);
var date = new Date(post.postDate);
var year = date.getFullYear();
console.log('Generating archive for year: '+year);
var month = date.getMonth()+1;
async.parallel({
monthArchiveErr: function(callback) {
generatePostMonthArchive(year, function(err) {
callback(null, err);
});
},
yearArchiveErr: function(callback) {
generatePostYearArchive(month, function(err) {
callback(null, err);
});
}
}, function(err, results) {
if (!yearArchiveErr && !monthArchiveErr) {
cb(null);
}
else if (yearArchiveErr) {
cb(yearArchiveErr);
}
else if (monthArchiveErr) {
cb(monthArchiveErr);
}
else {
cb(null);
}
});
});
}
exports.generatePostArchives = generatePostArchives;
// Function that generates the yearly archive pages.
// TODO: make this not a skeleton.
// TODO: create pagination.
var generatePostYearArchive = function (year, cb) {
console.log('Generating archive for year: '+year);
cb(null);
}
exports.generatePostYearArchive = generatePostYearArchive;
// Function that generates the monthly archive pages.
// TODO: Make this not a skeleton.
// TODO: create pagination.
var generatePostMonthArchive = function (month, cb) {
console.log('Generating archive for month: '+month);
cb(null);
}
exports.generatePostMonthArchive = generatePostMonthArchive;
// Function to generate a single post page.
// TODO: (re)generate images as required.
var generatePost = function (id, cb) {
db.getPostById(id, function(post) {
console.log('Fetching post id: '+id);
var title = post.title;
var content = markdown.toHTML(post.markdown);
var slug = post.slug;
var postDate = helper.epochToShortDateString(post.postDate);
var url = '/blog/'+postDate.slice(0,4)+'/'+postDate.slice(5,7)+'/'+slug+'/';
var filepath = 'generated'+url;
var filename = filepath+'index.html';
var options = {
pretty: false,
title: title,
content: content,
slug: slug,
url: url,
postDate: postDate
};
console.log('Rendering post: '+title);
var jadeOut = jade.renderFile('views/render-post.jade', options);
console.log('Creating directory: '+filepath);
mkdirp(filepath, function(err) {
if (err) {
console.log(err);
}
else {
console.log('Writing to file: '+filename);
fs.writeFile(filename, jadeOut, 'utf-8', function(err) {
if (err) console.log(err);
cb(err);
});
}
});
});
}
exports.generatePost = generatePost;

48
genStatic.js Normal file
View file

@ -0,0 +1,48 @@
var fs = require('fs');
var ncp = require('ncp').ncp;
// Function to copy over the static folder to generated directory if
// it has a earlier modified time.
var generateStatic = function(cb) {
var sourceFolder = 'data/static';
var destFolder = 'generated/static';
async.parallel({
sourceMtime: function(cb) {
console.log('Getting stats for '+sourceFolder);
fs.stat(sourceFolder, function(err, sstats) {
if (err) console.log(err);
console.log(sourceFolder+' mtime: '+sstats.mtime);
cb(null, sstats.mtime);
});
},
destMtime: function(cb) {
console.log('Getting stats for '+destFolder);
fs.stat(destFolder, function(err, dstats) {
if (err) console.log(err);
console.log(destFolder+' mtime: '+dstats.mtime);
cb(null, dstats.mtime);
});
}
}, function(err, results) {
if (results.sourceMtime > results.destMtime) {
console.log(destFolder+' is older than '+sourceFolder);
fs.unlink(destFolder, function(err) {
console.log(destFolder+' deleted.');
ncp(sourceFolder, destFolder, function(err) {
if (!err) {
console.log(sourceFolder+' copied to '+destFolder);
cb(null);
}
else {
cb(err);
}
});
});
}
else {
console.log(destFolder+' is not older than '+sourceFolder+' ignoring...');
}
});
}
exports.generateStatic = generateStatic;

View file

@ -3,22 +3,23 @@
"version": "0.0.0",
"private": true,
"dependencies": {
"async": "~1.5.0",
"body-parser": "~1.13.2",
"connect-ensure-login": "~0.1.1",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"express-flash": "~0.0.2",
"express-session": "~1.12.0",
"jade": "~1.11.0",
"markdown": "~0.5.0",
"morgan": "~1.6.1",
"multer": "~1.1.0",
"async": "^1.5.0",
"body-parser": "^1.13.2",
"connect-ensure-login": "^0.1.1",
"cookie-parser": "^1.3.5",
"debug": "^2.2.0",
"express": "^4.13.1",
"express-flash": "^0.0.2",
"express-session": "^1.12.0",
"gm": "^1.21.1",
"jade": "^1.11.0",
"markdown": "^0.5.0",
"morgan": "^1.6.1",
"multer": "^1.1.0",
"ncp": "^2.0.0",
"passport": "~0.3.0",
"passport-local": "~1.0.0",
"serve-favicon": "~2.3.0",
"sqlite3": "~3.1.0"
"passport": "^0.3.0",
"passport-local": "^1.0.0",
"serve-favicon": "^2.3.0",
"sqlite3": "^3.1.0"
}
}

View file

@ -28,7 +28,7 @@ CREATE TABLE "posts" (
"postDate" INTEGER, -- The date that the post should go live and be sorted by
"updatedDate" INTEGER, -- The date that the post was last updated
"createDate" INTEGER NOT NULL, -- The date the post was created
"lastBuildDate" INTEGER, -- The date this post was last built
"lastGenerateDate" INTEGER, -- The date this post was last built
"lastUpload" INTEGER -- The date the post was last uploaded
);
@ -42,7 +42,7 @@ CREATE TABLE "galleries" (
"postDate" INTEGER, -- The date that the gallery should go live and be sorted by
"updatedDate" INTEGER, -- The date that the gallery was last updated
"createDate" INTEGER NOT NULL, -- The date the gallery was created
"lastBuildDate" INTEGER, -- The date this gallery was last built
"lastGenerateDate" INTEGER, -- The date this gallery was last built
"lastUpload" INTEGER -- The date the gallery was last uploaded
);
@ -57,8 +57,16 @@ CREATE TABLE "photos" (
"photoDate" INTEGER, -- The date that the photo should go live and be sorted by
"updatedDate" INTEGER, -- The date that the photo was last updated
"createDate" INTEGER NOT NULL, -- The date the photo was created
"lastBuildDate" INTEGER, -- The date this photo was last built
"lastUpload" INTEGER -- The date the photo was last uploaded
"lastGenerateDate" INTEGER, -- The date this photo was last built
"lastUpload" INTEGER, -- The date the photo was last uploaded
"extension" TEXT, -- The original extension of the photo
"mimetype" TEXT, -- The original mimetype of the photo
"orientation" TEXT, -- The correct orientation of the photo (landscape, portrait)
"origWidth" INTEGER, -- The original width of the photo
"origHeight" INTEGER, -- The original height of the photo
"aperture" REAL, -- The aperture of the original photo
"speed" REAL, -- The shutter speed of the original photo (in seconds)
"location" TEXT -- The location data of the original photo
);
DROP TABLE IF EXISTS "galleryPhotos";

View file

@ -21,10 +21,10 @@ block content
div(class="col-md-6")
div(class="panel panel-danger")
div(class="panel-heading")
h3(class="panel-title") Last Rebuild (UTC)
h3(class="panel-title") Last Regenerate (UTC)
div(class="panel-body")
h3 #{rebuildDate}
h3 #{regenerateDate}
div(class="row")
div(class="col-md-4")

View file

@ -20,6 +20,6 @@ block content
| -
a(href="/admin/photo/delete/#{photo.id}") Delete
| -
a(href="/admin/photo/rebuild/#{photo.id}") Rebuild
a(href="/admin/photo/regenerate/#{photo.id}") Regenerate
| -
a(href="/admin/photo/publish/#{photo.id}") Publish

View file

@ -20,7 +20,7 @@ block content
| -
a(href="/admin/post/delete/#{post.id}") Delete
| -
a(href="/admin/post/rebuild/#{post.id}") Rebuild
a(href="/admin/post/regenerate/#{post.id}") Regenerate
| -
a(href="/admin/post/publish/#{post.id}") Publish

View file

@ -15,6 +15,6 @@ block content
| #{date} --
a(href="/admin/post/edit/#{post.id}") Edit
| -
a(href="/admin/post/rebuild/#{post.id}") Rebuild
a(href="/admin/post/regenerate/#{post.id}") Regenerate
div(class='row') !{content}

View file

@ -15,9 +15,9 @@ div(class="col-sm-2 sidebar")
li: a(href="/admin/gallery/list") All Galleries
ul(class="nav nav-sidebar")
form(action="/admin/rebuild/", method="get")
form(action="/admin/regenerate/", method="get")
button(class="btn btn-lg btn-success")
span(class="glyphicon glyphicon-repeat") Build
span(class="glyphicon glyphicon-repeat") Generate
ul(class="nav nav-sidebar")
form(action="/admin/publish/", method="get")

View file

@ -6,12 +6,12 @@ html
meta(http-equiv="X-UA-Compatible", content="IE=edge")
meta(name="viewport", content="width=device-width, initial-scale=1")
link(href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css", rel="stylesheet" integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous")
link(rel='stylesheet', href='/stylesheets/style.css')
link(rel='stylesheet', href='/static/stylesheets/style.css')
block head-addition
body(role="document")
div(class="container-fluid", role="main")
div(class="row")
div(class="col-sm-3")
img(class="img-responsive", src="/static/logo.png")
img(class="img-responsive", src="/static/images/logo.png")
div(class="col-sm-8 col-sm-offset-4")
block content