mirror of https://github.com/thumbsup/thumbsup
Use <standard> for code style, which removed lots of unused variables/requires
parent
e71e5d4207
commit
59b9bdbbd2
@ -1,141 +1,141 @@
|
|||||||
var _ = require('lodash');
|
var _ = require('lodash')
|
||||||
var path = require('path');
|
var path = require('path')
|
||||||
var url = require('url');
|
var url = require('url')
|
||||||
var index = 0;
|
var index = 0
|
||||||
|
|
||||||
// number of images to show in the album preview grid
|
// number of images to show in the album preview grid
|
||||||
var PREVIEW_COUNT = 10;
|
var PREVIEW_COUNT = 10
|
||||||
|
|
||||||
var SORT_ALBUMS_BY = {
|
var SORT_ALBUMS_BY = {
|
||||||
'title': function(album) { return album.title; },
|
'title': function (album) { return album.title },
|
||||||
'start-date': function(album) { return album.stats.fromDate; },
|
'start-date': function (album) { return album.stats.fromDate },
|
||||||
'end-date': function(album) { return album.stats.toDate; }
|
'end-date': function (album) { return album.stats.toDate }
|
||||||
};
|
}
|
||||||
|
|
||||||
var SORT_MEDIA_BY = {
|
var SORT_MEDIA_BY = {
|
||||||
'filename': function(file) { return file.filename; },
|
'filename': function (file) { return file.filename },
|
||||||
'date': function(file) { return file.date; }
|
'date': function (file) { return file.date }
|
||||||
};
|
}
|
||||||
|
|
||||||
var PREVIEW_MISSING = {
|
var PREVIEW_MISSING = {
|
||||||
urls: {
|
urls: {
|
||||||
thumbnail: 'public/missing.png'
|
thumbnail: 'public/missing.png'
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
function Album(opts) {
|
function Album (opts) {
|
||||||
if (typeof opts === 'string') opts = { title: opts };
|
if (typeof opts === 'string') opts = { title: opts }
|
||||||
this.id = opts.id || ++index;
|
this.id = opts.id || ++index
|
||||||
this.title = opts.title || ('Album ' + this.id);
|
this.title = opts.title || ('Album ' + this.id)
|
||||||
this.basename = sanitise(this.title);
|
this.basename = sanitise(this.title)
|
||||||
this.files = opts.files || [];
|
this.files = opts.files || []
|
||||||
this.albums = opts.albums || [];
|
this.albums = opts.albums || []
|
||||||
this.depth = 0;
|
this.depth = 0
|
||||||
this.home = false;
|
this.home = false
|
||||||
this.stats = null;
|
this.stats = null
|
||||||
this.previews = null;
|
this.previews = null
|
||||||
this.allFiles = [];
|
this.allFiles = []
|
||||||
}
|
}
|
||||||
|
|
||||||
Album.prototype.finalize = function(options, parent) {
|
Album.prototype.finalize = function (options, parent) {
|
||||||
options = options || {};
|
options = options || {}
|
||||||
var albumsOutputFolder = options.albumsOutputFolder || '.';
|
var albumsOutputFolder = options.albumsOutputFolder || '.'
|
||||||
// calculate final file paths and URLs
|
// calculate final file paths and URLs
|
||||||
if (parent == null) {
|
if (parent == null) {
|
||||||
this.path = options.index;
|
this.path = options.index
|
||||||
this.url = options.index;
|
this.url = options.index
|
||||||
this.depth = 0;
|
this.depth = 0
|
||||||
} else {
|
} else {
|
||||||
if (parent.depth > 0) {
|
if (parent.depth > 0) {
|
||||||
this.basename = parent.basename + '-' + this.basename;
|
this.basename = parent.basename + '-' + this.basename
|
||||||
}
|
}
|
||||||
this.path = path.join(albumsOutputFolder, this.basename + '.html');
|
this.path = path.join(albumsOutputFolder, this.basename + '.html')
|
||||||
this.url = url.resolve(albumsOutputFolder + '/', this.basename + '.html');
|
this.url = url.resolve(albumsOutputFolder + '/', this.basename + '.html')
|
||||||
this.depth = parent.depth + 1;
|
this.depth = parent.depth + 1
|
||||||
}
|
}
|
||||||
// then finalize all nested albums (which uses the parent basename)
|
// then finalize all nested albums (which uses the parent basename)
|
||||||
for (var i = 0; i < this.albums.length; ++i) {
|
for (var i = 0; i < this.albums.length; ++i) {
|
||||||
this.albums[i].finalize(options, this);
|
this.albums[i].finalize(options, this)
|
||||||
}
|
}
|
||||||
// perform stats & other calculations
|
// perform stats & other calculations
|
||||||
// once the nested albums have been finalized too
|
// once the nested albums have been finalized too
|
||||||
this.home = this.depth === 0;
|
this.home = this.depth === 0
|
||||||
this.calculateStats();
|
this.calculateStats()
|
||||||
this.calculateSummary();
|
this.calculateSummary()
|
||||||
this.sort(options);
|
this.sort(options)
|
||||||
this.pickPreviews();
|
this.pickPreviews()
|
||||||
this.aggregateAllFiles();
|
this.aggregateAllFiles()
|
||||||
};
|
}
|
||||||
|
|
||||||
Album.prototype.calculateStats = function() {
|
Album.prototype.calculateStats = function () {
|
||||||
// nested albums
|
// nested albums
|
||||||
var nestedPhotos = _.map(this.albums, 'stats.photos');
|
var nestedPhotos = _.map(this.albums, 'stats.photos')
|
||||||
var nestedVideos = _.map(this.albums, 'stats.videos');
|
var nestedVideos = _.map(this.albums, 'stats.videos')
|
||||||
var nestedFromDates = _.map(this.albums, 'stats.fromDate');
|
var nestedFromDates = _.map(this.albums, 'stats.fromDate')
|
||||||
var nestedToDates = _.map(this.albums, 'stats.toDate');
|
var nestedToDates = _.map(this.albums, 'stats.toDate')
|
||||||
// current level
|
// current level
|
||||||
var currentPhotos = _.filter(this.files, {isVideo: false}).length;
|
var currentPhotos = _.filter(this.files, {isVideo: false}).length
|
||||||
var currentVideos = _.filter(this.files, {isVideo: true}).length;
|
var currentVideos = _.filter(this.files, {isVideo: true}).length
|
||||||
var currentFromDate = _.map(this.files, 'date');
|
var currentFromDate = _.map(this.files, 'date')
|
||||||
var currentToDate = _.map(this.files, 'date');
|
var currentToDate = _.map(this.files, 'date')
|
||||||
// aggregate all stats
|
// aggregate all stats
|
||||||
this.stats = {
|
this.stats = {
|
||||||
albums: this.albums.length,
|
albums: this.albums.length,
|
||||||
photos: _.sum(_.compact(_.concat(nestedPhotos, currentPhotos))) || 0,
|
photos: _.sum(_.compact(_.concat(nestedPhotos, currentPhotos))) || 0,
|
||||||
videos: _.sum(_.compact(_.concat(nestedVideos, currentVideos))) || 0,
|
videos: _.sum(_.compact(_.concat(nestedVideos, currentVideos))) || 0,
|
||||||
fromDate: _.min(_.compact(_.concat(nestedFromDates, currentFromDate))),
|
fromDate: _.min(_.compact(_.concat(nestedFromDates, currentFromDate))),
|
||||||
toDate: _.max(_.compact(_.concat(nestedToDates, currentToDate)))
|
toDate: _.max(_.compact(_.concat(nestedToDates, currentToDate)))
|
||||||
};
|
}
|
||||||
this.stats.total = this.stats.photos + this.stats.videos;
|
this.stats.total = this.stats.photos + this.stats.videos
|
||||||
}
|
}
|
||||||
|
|
||||||
Album.prototype.calculateSummary = function() {
|
Album.prototype.calculateSummary = function () {
|
||||||
var items = [
|
var items = [
|
||||||
itemCount(this.stats.albums, 'album'),
|
itemCount(this.stats.albums, 'album'),
|
||||||
itemCount(this.stats.photos, 'photo'),
|
itemCount(this.stats.photos, 'photo'),
|
||||||
itemCount(this.stats.videos, 'video')
|
itemCount(this.stats.videos, 'video')
|
||||||
];
|
]
|
||||||
this.summary = _.compact(items).join(', ');
|
this.summary = _.compact(items).join(', ')
|
||||||
};
|
}
|
||||||
|
|
||||||
Album.prototype.sort = function(options) {
|
Album.prototype.sort = function (options) {
|
||||||
this.files = _.orderBy(this.files, SORT_MEDIA_BY[options.sortMediaBy], options.sortMediaDirection);
|
this.files = _.orderBy(this.files, SORT_MEDIA_BY[options.sortMediaBy], options.sortMediaDirection)
|
||||||
this.albums = _.orderBy(this.albums, SORT_ALBUMS_BY[options.sortAlbumsBy], options.sortAlbumsDirection);
|
this.albums = _.orderBy(this.albums, SORT_ALBUMS_BY[options.sortAlbumsBy], options.sortAlbumsDirection)
|
||||||
};
|
}
|
||||||
|
|
||||||
Album.prototype.pickPreviews = function() {
|
Album.prototype.pickPreviews = function () {
|
||||||
// also consider previews from nested albums
|
// also consider previews from nested albums
|
||||||
var nestedPicks = _.flatten(_.map(this.albums, 'previews')).filter(function(file) {
|
var nestedPicks = _.flatten(_.map(this.albums, 'previews')).filter(function (file) {
|
||||||
return file !== PREVIEW_MISSING;
|
return file !== PREVIEW_MISSING
|
||||||
});
|
})
|
||||||
// then pick the top ones
|
// then pick the top ones
|
||||||
var potentialPicks = _.concat(this.files, nestedPicks);
|
var potentialPicks = _.concat(this.files, nestedPicks)
|
||||||
this.previews = potentialPicks.slice(0, PREVIEW_COUNT);
|
this.previews = potentialPicks.slice(0, PREVIEW_COUNT)
|
||||||
// and fill the gap with a placeholder
|
// and fill the gap with a placeholder
|
||||||
var missing = PREVIEW_COUNT - this.previews.length;
|
var missing = PREVIEW_COUNT - this.previews.length
|
||||||
for (var i = 0; i < missing; ++i) {
|
for (var i = 0; i < missing; ++i) {
|
||||||
this.previews.push(PREVIEW_MISSING);
|
this.previews.push(PREVIEW_MISSING)
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
Album.prototype.aggregateAllFiles = function() {
|
Album.prototype.aggregateAllFiles = function () {
|
||||||
var nestedFiles = _.flatten(_.map(this.albums, 'allFiles'))
|
var nestedFiles = _.flatten(_.map(this.albums, 'allFiles'))
|
||||||
this.allFiles = _.concat(nestedFiles, this.files);
|
this.allFiles = _.concat(nestedFiles, this.files)
|
||||||
};
|
}
|
||||||
|
|
||||||
function sanitise(filename) {
|
function sanitise (filename) {
|
||||||
return filename.replace(/[^a-z0-9-_]/ig, '');
|
return filename.replace(/[^a-z0-9-_]/ig, '')
|
||||||
}
|
}
|
||||||
|
|
||||||
function itemCount(count, type) {
|
function itemCount (count, type) {
|
||||||
if (count === 0) return '';
|
if (count === 0) return ''
|
||||||
var plural = (count > 1) ? 's' : '';
|
var plural = (count > 1) ? 's' : ''
|
||||||
return '' + count + ' ' + type + plural;
|
return '' + count + ' ' + type + plural
|
||||||
}
|
}
|
||||||
|
|
||||||
// for testing purposes
|
// for testing purposes
|
||||||
Album.resetIds = function() {
|
Album.resetIds = function () {
|
||||||
index = 0;
|
index = 0
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = Album;
|
module.exports = Album
|
||||||
|
@ -1,42 +1,42 @@
|
|||||||
var _ = require('lodash');
|
var _ = require('lodash')
|
||||||
var path = require('path');
|
var path = require('path')
|
||||||
var moment = require('moment');
|
var moment = require('moment')
|
||||||
var Album = require('./album');
|
var Album = require('./album')
|
||||||
|
|
||||||
// creates nested albums based on the media date, e.g. "{year}/{month}"
|
// creates nested albums based on the media date, e.g. "{year}/{month}"
|
||||||
// opts = {format}, where format is a valid <moment> format
|
// opts = {format}, where format is a valid <moment> format
|
||||||
// e.g. "YYYY-MM" or "YYYY/MMMM" for nested albums
|
// e.g. "YYYY-MM" or "YYYY/MMMM" for nested albums
|
||||||
exports.albums = function(collection, opts) {
|
exports.albums = function (collection, opts) {
|
||||||
opts = _.defaults(opts, {
|
opts = _.defaults(opts, {
|
||||||
grouping: opts.albumsDateFormat || 'YYYY-MMMM'
|
grouping: opts.albumsDateFormat || 'YYYY-MMMM'
|
||||||
});
|
})
|
||||||
var groups = {};
|
var groups = {}
|
||||||
// put all files in the right albums
|
// put all files in the right albums
|
||||||
collection.forEach(function(media) {
|
collection.forEach(function (media) {
|
||||||
var groupName = moment(media.date).format(opts.grouping);
|
var groupName = moment(media.date).format(opts.grouping)
|
||||||
createAlbumHierarchy(groups, groupName);
|
createAlbumHierarchy(groups, groupName)
|
||||||
groups[groupName].files.push(media);
|
groups[groupName].files.push(media)
|
||||||
});
|
})
|
||||||
// only return top-level albums
|
// only return top-level albums
|
||||||
var topLevel = _.keys(groups).filter(function(dir) {
|
var topLevel = _.keys(groups).filter(function (dir) {
|
||||||
return path.dirname(dir) === '.';
|
return path.dirname(dir) === '.'
|
||||||
});
|
})
|
||||||
return _.values(_.pick(groups, topLevel));
|
return _.values(_.pick(groups, topLevel))
|
||||||
};
|
}
|
||||||
|
|
||||||
function createAlbumHierarchy(albumsByFullDate, dateSegment) {
|
function createAlbumHierarchy (albumsByFullDate, dateSegment) {
|
||||||
if (!albumsByFullDate.hasOwnProperty(dateSegment)) {
|
if (!albumsByFullDate.hasOwnProperty(dateSegment)) {
|
||||||
// create parent albums first
|
// create parent albums first
|
||||||
var parentDate = path.dirname(dateSegment);
|
var parentDate = path.dirname(dateSegment)
|
||||||
if (parentDate !== '.') {
|
if (parentDate !== '.') {
|
||||||
createAlbumHierarchy(albumsByFullDate, parentDate);
|
createAlbumHierarchy(albumsByFullDate, parentDate)
|
||||||
}
|
}
|
||||||
// then create album if it doesn't exist
|
// then create album if it doesn't exist
|
||||||
var lastDateSegment = path.basename(dateSegment);
|
var lastDateSegment = path.basename(dateSegment)
|
||||||
albumsByFullDate[dateSegment] = new Album({title: lastDateSegment});
|
albumsByFullDate[dateSegment] = new Album({title: lastDateSegment})
|
||||||
// then attach to parent
|
// then attach to parent
|
||||||
if (parentDate !== '.') {
|
if (parentDate !== '.') {
|
||||||
albumsByFullDate[parentDate].albums.push(albumsByFullDate[dateSegment]);
|
albumsByFullDate[parentDate].albums.push(albumsByFullDate[dateSegment])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,38 +1,38 @@
|
|||||||
var _ = require('lodash');
|
var _ = require('lodash')
|
||||||
var path = require('path');
|
var path = require('path')
|
||||||
var Album = require('./album');
|
var Album = require('./album')
|
||||||
|
|
||||||
// for now only 1 level of folders,
|
// for now only 1 level of folders,
|
||||||
// e.g. an album might be called "holidays/newyork" or "holidays/tokyo"n
|
// e.g. an album might be called "holidays/newyork" or "holidays/tokyo"n
|
||||||
// eventually we could return nested albums as an option
|
// eventually we could return nested albums as an option
|
||||||
exports.albums = function(collection, opts) {
|
exports.albums = function (collection, opts) {
|
||||||
var albumsByFullPath = {};
|
var albumsByFullPath = {}
|
||||||
// put all files in the right album
|
// put all files in the right album
|
||||||
collection.forEach(function(media) {
|
collection.forEach(function (media) {
|
||||||
var fullDir = path.dirname(media.file.path);
|
var fullDir = path.dirname(media.file.path)
|
||||||
createAlbumHierarchy(albumsByFullPath, fullDir);
|
createAlbumHierarchy(albumsByFullPath, fullDir)
|
||||||
albumsByFullPath[fullDir].files.push(media);
|
albumsByFullPath[fullDir].files.push(media)
|
||||||
});
|
})
|
||||||
// only return top-level albums
|
// only return top-level albums
|
||||||
var topLevel = _.keys(albumsByFullPath).filter(function(dir) {
|
var topLevel = _.keys(albumsByFullPath).filter(function (dir) {
|
||||||
return path.dirname(dir) === '.';
|
return path.dirname(dir) === '.'
|
||||||
});
|
})
|
||||||
return _.values(_.pick(albumsByFullPath, topLevel));
|
return _.values(_.pick(albumsByFullPath, topLevel))
|
||||||
};
|
}
|
||||||
|
|
||||||
function createAlbumHierarchy(albumsByFullPath, fullDir) {
|
function createAlbumHierarchy (albumsByFullPath, fullDir) {
|
||||||
if (!albumsByFullPath.hasOwnProperty(fullDir)) {
|
if (!albumsByFullPath.hasOwnProperty(fullDir)) {
|
||||||
// create parent albums first
|
// create parent albums first
|
||||||
var parentDir = path.dirname(fullDir);
|
var parentDir = path.dirname(fullDir)
|
||||||
if (parentDir !== '.') {
|
if (parentDir !== '.') {
|
||||||
createAlbumHierarchy(albumsByFullPath, parentDir);
|
createAlbumHierarchy(albumsByFullPath, parentDir)
|
||||||
}
|
}
|
||||||
// then create album if it doesn't exist
|
// then create album if it doesn't exist
|
||||||
var dirname = path.basename(fullDir);
|
var dirname = path.basename(fullDir)
|
||||||
albumsByFullPath[fullDir] = new Album({title: dirname});
|
albumsByFullPath[fullDir] = new Album({title: dirname})
|
||||||
// then attach to parent
|
// then attach to parent
|
||||||
if (parentDir !== '.') {
|
if (parentDir !== '.') {
|
||||||
albumsByFullPath[parentDir].albums.push(albumsByFullPath[fullDir]);
|
albumsByFullPath[parentDir].albums.push(albumsByFullPath[fullDir])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue