crunch-node/photo.js

279 lines
8.2 KiB
JavaScript

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;