Moving photos away from the old model to the new style, probably buggy. #7
This commit is contained in:
parent
074dcd4a31
commit
e57fbee48f
3 changed files with 295 additions and 91 deletions
91
genPhotos.js
91
genPhotos.js
|
@ -1,91 +0,0 @@
|
||||||
var fs = require('fs');
|
|
||||||
var gm = require('gm');
|
|
||||||
var mkdirp = require('mkdirp');
|
|
||||||
var markdown = require( "markdown" ).markdown;
|
|
||||||
var jade = require('jade');
|
|
||||||
|
|
||||||
|
|
||||||
var config = require('./config.js').config;
|
|
||||||
var db = require('./db.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+'/photos/'+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;
|
|
||||||
|
|
||||||
(function(size){
|
|
||||||
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+' '+size+' generated.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
})(size);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
cb(failure);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.generatePhotoSizesById = generatePhotoSizesById;
|
|
||||||
|
|
||||||
var generatePhotoPage = function (id, cb) {
|
|
||||||
db.getPhotoById(id, function(photo) {
|
|
||||||
console.log('Fetching photo id: '+id);
|
|
||||||
var description = markdown.toHTML(photo.description);
|
|
||||||
var photoDate = helper.epochToShortDateString(photo.photoDate);
|
|
||||||
var url = '/photos/'+photo.slug+'/';
|
|
||||||
var filepath = config.genDir+url;
|
|
||||||
var filename = filepath+'index.html';
|
|
||||||
var options = {
|
|
||||||
pretty: false,
|
|
||||||
title: photo.title,
|
|
||||||
description: description,
|
|
||||||
slug: photo.slug,
|
|
||||||
url: url,
|
|
||||||
extension: photo.extension,
|
|
||||||
postDate: photo.photoDate
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log('Rendering photo: '+photo.title);
|
|
||||||
var jadeOut = jade.renderFile('views/render-photo.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.generatePhotoPage = generatePhotoPage;
|
|
279
photo.js
Normal file
279
photo.js
Normal file
|
@ -0,0 +1,279 @@
|
||||||
|
var moment = require("moment");
|
||||||
|
var _ = require("lodash");
|
||||||
|
var fs = require('fs');
|
||||||
|
var mkdirp = require('mkdirp');
|
||||||
|
var jade = require('jade');
|
||||||
|
var markdown = require( "markdown" ).markdown;
|
||||||
|
var path = require('path');
|
||||||
|
var schemas = require('./schemas.js');
|
||||||
|
var gm = require('gm');
|
||||||
|
var config = require("./config.js").config;
|
||||||
|
|
||||||
|
var Photo = function(data) {
|
||||||
|
this.data = this.sanitize(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Prototype Setup
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
Photo.prototype.data = {}
|
||||||
|
|
||||||
|
Photo.prototype.get = function (name) {
|
||||||
|
return this.data[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
Photo.prototype.set = function (name, value) {
|
||||||
|
this.data[name] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Photo.prototype.sanitize = function (data) {
|
||||||
|
data = data || {};
|
||||||
|
schema = schemas.photo;
|
||||||
|
return _.pick(_.defaults(data, schema), _.keys(schema));
|
||||||
|
}
|
||||||
|
|
||||||
|
Photo.prototype.save = function (date, callback) {
|
||||||
|
var self = this;
|
||||||
|
if (date) {
|
||||||
|
this.set('updatedDate', date);
|
||||||
|
} else {
|
||||||
|
this.set("updatedDate", new Date());
|
||||||
|
}
|
||||||
|
this.data = this.sanitize(this.data);
|
||||||
|
db.collection("photos").update({uuid: this.get("uuid")}, this.data, {upsert: true}, function(err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Prototype Functions
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Parse a string of tags
|
||||||
|
// Returns a list of tags
|
||||||
|
Post.prototype.tagPhoto = function(str) {
|
||||||
|
this.set("tags", []);
|
||||||
|
|
||||||
|
// we don't need no stinking commas
|
||||||
|
var str = str.replace(/,/g,'');
|
||||||
|
|
||||||
|
// remove accents, swap ñ for n, etc
|
||||||
|
var from = "àáäâèéëêìíïîòóöôùúüûñç·/,:;&%";
|
||||||
|
var to = "aaaaeeeeiiiioooouuuunc-------";
|
||||||
|
for (var i=0, l=from.length ; i<l ; i++) {
|
||||||
|
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make into a sorted, unique array
|
||||||
|
var l = str.split(" ");
|
||||||
|
var list = []
|
||||||
|
for (i = 0; i < l.length; i ++) {
|
||||||
|
if (l[i]) {
|
||||||
|
list[i] = l[i].toLowerCase().trim()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list = list.sort();
|
||||||
|
var unique = list.filter(function(elem, index, self) {
|
||||||
|
return index == self.indexOf(elem);
|
||||||
|
})
|
||||||
|
|
||||||
|
// Push these into the post.
|
||||||
|
for (var i = 0, size = unique.length; i < size; i++) {
|
||||||
|
if (unique[i]) {
|
||||||
|
this.data.tags.push({name: list[i]});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get a YYYY-MM-DD format of the current photoDate
|
||||||
|
// Arguments: None
|
||||||
|
// Returns: Date as String
|
||||||
|
Photo.prototype.getShortDate = function () {
|
||||||
|
return moment(this.get("photoDate")).format("YYYY-MM-DD");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to return rendered markdown of a photo
|
||||||
|
// Arguments: None
|
||||||
|
// Returns: Content as String
|
||||||
|
Photo.prototype.renderMarkdown = function () {
|
||||||
|
return markdown.toHTML(this.get("markdown"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get the relative url of a photo, this sets the permalink structure
|
||||||
|
// Arguments: None
|
||||||
|
// Returns: URL as String
|
||||||
|
Photo.prototype.getURL = function () {
|
||||||
|
var url = "/photo/";
|
||||||
|
url += moment(this.get("photoDate")).format("YYYY/MM/");
|
||||||
|
url += this.get("slug") + '/';
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to return the path of the photo index file
|
||||||
|
// Arguments: None
|
||||||
|
// Returns: Path as a string
|
||||||
|
Photo.prototype.getIndexPath = function () {
|
||||||
|
var file = path.join(this.getDirectory(),'index.html');
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to return the photo build directory
|
||||||
|
// Arguments: None
|
||||||
|
// Returns: Directory path as String
|
||||||
|
Photo.prototype.getDirectory = function () {
|
||||||
|
var dir = path.join(config.buildDir,this.getURL());
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Build a slug from a title string
|
||||||
|
// Arguments: None
|
||||||
|
// Returns: Photo Slug as String
|
||||||
|
Photo.prototype.makeSlug = function () {
|
||||||
|
str = this.get("title").replace(/^\s+|\s+$/g, ''); // trim
|
||||||
|
str = str.toLowerCase();
|
||||||
|
str = str.trim();
|
||||||
|
|
||||||
|
// remove accents, swap ñ for n, etc
|
||||||
|
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;&";
|
||||||
|
var to = "aaaaeeeeiiiioooouuuunc-------";
|
||||||
|
for (var i=0, l=from.length ; i<l ; i++) {
|
||||||
|
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
|
||||||
|
.replace(/\s+/g, '-') // collapse whitespace and replace by -
|
||||||
|
.replace(/-+/g, '-'); // collapse dashes
|
||||||
|
|
||||||
|
return str;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Function to generate static renderings of a particular post.
|
||||||
|
// Arguments: Callback function
|
||||||
|
// Returns: err
|
||||||
|
Photo.prototype.build = function (callback) {
|
||||||
|
var options = {
|
||||||
|
pretty: true,
|
||||||
|
photo: this
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('Rendering photo: '+this.get("title"));
|
||||||
|
var jadeOut = jade.renderFile('views/render-photo.jade', options);
|
||||||
|
|
||||||
|
console.log('Creating directory: '+ this.getDirectory());
|
||||||
|
|
||||||
|
self = this;
|
||||||
|
|
||||||
|
mkdirp(self.getDirectory(), function (err) {
|
||||||
|
if (err) console.log(err);
|
||||||
|
for (var size in config.imageSizes) {
|
||||||
|
console.log('Generating '+size+' for '+self.get("slug"));
|
||||||
|
(function(size){
|
||||||
|
gm(photo.path)
|
||||||
|
.autoOrient()
|
||||||
|
.resize(config.imageSizes[size], config.imageSizes[size])
|
||||||
|
.write(path.join(self.getDirectory(),size+'.'+self.get("extension")),
|
||||||
|
function(err) {
|
||||||
|
if (err) {
|
||||||
|
console.log(self.get("slug")+' resize failed.');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log(self.get("slug")+' '+size+' generated.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
})(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeFile(self.getIndexPath(), jadeOut, 'utf-8', function (err) {
|
||||||
|
self.set('lastBuildDate', new Date());
|
||||||
|
self.save(self.get('lastBuildDate'), function (err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Independent Functions
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Function to find the most recent photo generation.
|
||||||
|
// Returns a date object
|
||||||
|
Photo.getLastBuildDate = function (callback) {
|
||||||
|
db.collection("photos").findOne({}, {lastBuildDate: 1}, {sort: {lastBuildDate: -1}},
|
||||||
|
function(err, doc) {
|
||||||
|
callback(err, doc.lastBuildDate);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Function to find the most recent photo upload date.
|
||||||
|
// Returns a date object
|
||||||
|
Photo.getLastUploadDate = function (callback) {
|
||||||
|
db.collection("photos").findOne({}, {lastUploadDate: 1}, {sort: {lastUploadDate: -1}},
|
||||||
|
function (err, doc) {
|
||||||
|
callback(err, doc.lastUploadDate);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Get a specific photo by it's UUID
|
||||||
|
// Inputs: UUID as String
|
||||||
|
// Returns: Photo object
|
||||||
|
Photo.getByUUID = function (uuid, callback) {
|
||||||
|
db.collection("posts").findOne({uuid: uuid}, function(err, doc) {
|
||||||
|
callback(err, new Photo(doc));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get a count of current photos
|
||||||
|
// Inputs: Callback function
|
||||||
|
// Returns: Count of photos as integer
|
||||||
|
Photo.countPhotos = function (callback) {
|
||||||
|
db.collection("photos").find({}).count(function (err, count) {
|
||||||
|
if (err) console.log(err);
|
||||||
|
callback(err, count);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to get a list of posts of a certain count and starting at an offset
|
||||||
|
// Count and start are optional, when start is specified count must be specified.
|
||||||
|
// Returns a list of post objects
|
||||||
|
Photos.getPhotos = function (count, start, callback) {
|
||||||
|
if (typeof callback === undefined) {
|
||||||
|
if (typeof start === undefined) {
|
||||||
|
callback = count;
|
||||||
|
count = undefined;
|
||||||
|
start = undefined;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
callback = start;
|
||||||
|
start = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var options = {}
|
||||||
|
if (start) options.skip=start;
|
||||||
|
if (count) options.limit=count;
|
||||||
|
options.sort = [['photoDate', 'desc'],['title', 'asc']];
|
||||||
|
|
||||||
|
db.collection("photos").find({},options).toArray( function(err, docs) {
|
||||||
|
if (err) console.log(err);
|
||||||
|
photos = [];
|
||||||
|
for (i=0; i<docs.length; i++) {
|
||||||
|
photos.push(new Photo(docs[i]));
|
||||||
|
}
|
||||||
|
callback(null, photos);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export the Post object for external use.
|
||||||
|
module.exports = Photo;
|
16
schemas.js
16
schemas.js
|
@ -30,6 +30,22 @@ schemas = {
|
||||||
slug: null,
|
slug: null,
|
||||||
description: null,
|
description: null,
|
||||||
createdDate: new Date()
|
createdDate: new Date()
|
||||||
|
},
|
||||||
|
photo: {
|
||||||
|
uuid: uuid.v1(),
|
||||||
|
title: null,
|
||||||
|
slug: null,
|
||||||
|
markdown: null,
|
||||||
|
createdDate: new Date(),
|
||||||
|
photoDate: new Date(),
|
||||||
|
updatedDate: new Date(),
|
||||||
|
deleted: false,
|
||||||
|
published: false,
|
||||||
|
category: null,
|
||||||
|
tags: [],
|
||||||
|
metadata: {},
|
||||||
|
lastGenerateDate: new Date("Mon Jan 1 1900 00:00:00 GMT-0500"),
|
||||||
|
lastUploadDate: new Date("Mon Jan 1 1900 00:00:00 GMT-0500")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue