2016-01-23 23:45:49 +00:00
|
|
|
var moment = require("moment");
|
|
|
|
var _ = require("lodash");
|
|
|
|
var fs = require('fs');
|
|
|
|
var mkdirp = require('mkdirp');
|
|
|
|
var jade = require('jade');
|
2016-01-25 03:05:38 +00:00
|
|
|
var marked = require('marked');
|
2016-01-23 23:45:49 +00:00
|
|
|
var path = require('path');
|
|
|
|
var schemas = require('./schemas.js');
|
|
|
|
var config = require("./config.js").config;
|
2016-01-25 03:05:38 +00:00
|
|
|
var sharp = require('sharp');
|
2016-01-23 23:45:49 +00:00
|
|
|
|
|
|
|
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
|
2016-01-24 03:53:53 +00:00
|
|
|
Photo.prototype.tagPhoto = function(str) {
|
2016-01-23 23:45:49 +00:00
|
|
|
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 () {
|
2016-01-25 03:05:38 +00:00
|
|
|
return marked(this.get("markdown"));
|
2016-01-23 23:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Function to get the relative url of a photo, this sets the permalink structure
|
|
|
|
// Arguments: None
|
|
|
|
// Returns: URL as String
|
|
|
|
Photo.prototype.getURL = function () {
|
2016-01-25 03:05:38 +00:00
|
|
|
var url = "/photos/";
|
2016-01-23 23:45:49 +00:00
|
|
|
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) {
|
2016-01-25 03:05:38 +00:00
|
|
|
console.log('Rendering photo: '+this.get("title"));
|
|
|
|
var jadeOut = jade.renderFile('views/render-photo.jade', {
|
2016-01-23 23:45:49 +00:00
|
|
|
pretty: true,
|
|
|
|
photo: this
|
2016-01-25 03:05:38 +00:00
|
|
|
});
|
2016-01-23 23:45:49 +00:00
|
|
|
|
|
|
|
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"));
|
2016-01-25 03:05:38 +00:00
|
|
|
(function (size) {
|
|
|
|
var outFile = path.join(self.getDirectory(),size+'.'+self.get("extension"));
|
|
|
|
sharp(self.get('masterPath'))
|
|
|
|
.resize(config.imageSizes[size], config.imageSizes[size])
|
|
|
|
.max()
|
|
|
|
.quality(95)
|
|
|
|
.withoutEnlargement()
|
|
|
|
.toFile(outFile, function(err) {
|
|
|
|
if (!err) {
|
2016-01-23 23:45:49 +00:00
|
|
|
console.log(self.get("slug")+' '+size+' generated.');
|
2016-01-25 03:05:38 +00:00
|
|
|
} else {
|
|
|
|
console.log(self.get("slug")+' resize failed.');
|
2016-01-23 23:45:49 +00:00
|
|
|
}
|
2016-01-25 03:05:38 +00:00
|
|
|
});
|
2016-01-23 23:45:49 +00:00
|
|
|
})(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) {
|
2016-01-24 03:53:53 +00:00
|
|
|
db.collection("photos").findOne({uuid: uuid}, function(err, doc) {
|
2016-01-23 23:45:49 +00:00
|
|
|
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
|
2016-01-24 03:53:53 +00:00
|
|
|
Photo.getPhotos = function (count, start, callback) {
|
2016-01-23 23:45:49 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-24 03:53:53 +00:00
|
|
|
// Function to find photos that need to be built
|
|
|
|
// Inputs: Callback function
|
|
|
|
// Returns: list of Photo objects
|
|
|
|
Photo.getNeedsBuild = function (callback) {
|
|
|
|
db.collection("photos").find({
|
|
|
|
$where: "(this.lastBuildDate < this.updatedDate) && this.published"
|
|
|
|
}).toArray(
|
|
|
|
function (err, docs) {
|
|
|
|
if (err) console.log(err);
|
|
|
|
photos = [];
|
|
|
|
if (docs) {
|
|
|
|
for (i=0; i<docs.length; i++) {
|
|
|
|
photos.push(new Photo(docs[i]));
|
|
|
|
}
|
|
|
|
callback(null, photos);
|
|
|
|
} else {
|
|
|
|
callback('No photos need to be built.', null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to find photos that need to be uploaded
|
|
|
|
// Inputs: Callback function
|
|
|
|
// Returns: List of Photo objects
|
|
|
|
Photo.getNeedsUpload = function (callback) {
|
|
|
|
db.collection("photos").find({
|
|
|
|
$where: '(this.lastUploadDate < this.updatedDate) && \
|
|
|
|
(this.lastBuildDate >= this.updatedDate) && \
|
|
|
|
this.published'
|
|
|
|
}).toArray( function (err, docs) {
|
|
|
|
if (err) console.log(err);
|
|
|
|
photos = [];
|
|
|
|
if (docs) {
|
|
|
|
for (i=0; i<docs.length; i++) {
|
|
|
|
photos.push(new Photo(docs[i]));
|
|
|
|
}
|
|
|
|
callback(null, photos);
|
|
|
|
} else {
|
|
|
|
callback("No photos require upload.", null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export the Photo object for external use.
|
2016-01-23 23:45:49 +00:00
|
|
|
module.exports = Photo;
|