by-date generator supports nested albums + custom album title format

pull/47/head
Romain Prieto 8 years ago
parent 942f673670
commit 72fd69bb79

@ -1,31 +1,42 @@
var _ = require('lodash');
var path = require('path');
var moment = require('moment');
var Album = require('./album');
// for now, a single level of albums by month named "{year}-{month}"
// eventually could support nested albums e.g. "{year}/{month}"
// it could be an option like "format: yyyy/mm"
// creates nested albums based on the media date, e.g. "{year}/{month}"
// opts = {format}, where format is a valid <moment> format
// e.g. "YYYY-MM" or "YYYY/MMMM" for nested albums
exports.albums = function(collection, opts) {
opts = _.defaults(opts, {
grouping: 'YYYY-MMMM'
});
var groups = {};
// put all files in the right albums
collection.files.forEach(function(file) {
var groupName = exports.format(file.date);
if (!groups.hasOwnProperty(groupName)) {
groups[groupName] = [];
}
groups[groupName].push(file);
var groupName = moment(file.date).format(opts.grouping);
createAlbumHierarchy(groups, groupName);
groups[groupName].files.push(file);
});
// only return top-level albums
var topLevel = _.keys(groups).filter(function(dir) {
return path.dirname(dir) === '.';
});
var albums = _.map(groups, function(val, key) {
return new Album({
title: key,
files: groups[key]
});
})
return albums;
return _.values(_.pick(groups, topLevel));
};
exports.format = function(date) {
var year = date.getFullYear().toString();
var month = new String(date.getMonth() + 1);
if (month.length === 1) month = '0' + month;
return year + '-' + month;
};
function createAlbumHierarchy(albumsByFullDate, dateSegment) {
if (!albumsByFullDate.hasOwnProperty(dateSegment)) {
// create parent albums first
var parentDate = path.dirname(dateSegment);
if (parentDate !== '.') {
createAlbumHierarchy(albumsByFullDate, parentDate);
}
// then create album if it doesn't exist
var lastDateSegment = path.basename(dateSegment);
albumsByFullDate[dateSegment] = new Album({title: lastDateSegment});
// then attach to parent
if (parentDate !== '.') {
albumsByFullDate[parentDate].albums.push(albumsByFullDate[dateSegment]);
}
}
}

@ -8,6 +8,7 @@ var files = require('../utils/files');
var template = require('./template');
var Album = require('./album');
var byFolder = require('./by-folder');
var byDate = require('./by-date');
var DIR_PUBLIC = path.join(__dirname, '..', '..', 'public');
var DIR_TEMPLATES = path.join(__dirname, '..', '..', 'templates');

@ -155,7 +155,6 @@ describe('Album', function() {
});
a.finalize();
should(a.previews).have.length(4);
console.log('-----\n', a.previews)
for (var i = 0; i < 4; ++i) {
should(a.previews[i].urls.thumb).not.eql('public/missing.png');
}

@ -5,38 +5,66 @@ var fixtures = require('../fixtures');
describe('ByDate', function() {
describe('date format', function() {
it('formats a date as YYYY-MM', function() {
should(bydate.format(fixtures.date('2016-06-13T16:43:19'))).eql('2016-06')
});
it('formats based on the local timezone', function() {
// TODO: why doesn't 23:59:59 work? Seems to be converted
should(bydate.format(fixtures.date('1999-01-01T00:00:00'))).eql('1999-01')
should(bydate.format(fixtures.date('1999-12-31T00:00:00'))).eql('1999-12')
});
});
it('creates albums by date', function () {
it('creates top-level albums grouped by month', function () {
// create files from different dates
var june1 = fixtures.photo({path: 'some/IMG_000001.jpg', date: fixtures.date('2016-06-01')});
var june2 = fixtures.photo({path: 'folders/IMG_000003.jpg', date: fixtures.date('2016-06-10')});
var july1 = fixtures.photo({path: 'random/IMG_000002.jpg', date: fixtures.date('2016-07-23')});
var july2 = fixtures.video({path: 'and/subfolders/IMG_000004.mp4', date: fixtures.date('2016-07-18')});
var a_2016_06 = fixtures.photo({date: fixtures.date('2016-06-01')});
var b_2016_06 = fixtures.photo({date: fixtures.date('2016-06-10')});
var c_2016_07 = fixtures.photo({date: fixtures.date('2016-07-23')});
var d_2016_07 = fixtures.video({date: fixtures.date('2016-07-18')});
// group them per month
var collection = { files: [june1, june2, july1, july2] };
var albums = bydate.albums(collection, {});
var collection = { files: [a_2016_06, b_2016_06, c_2016_07, d_2016_07] };
var albums = bydate.albums(collection, {
grouping: 'YYYY-MM'
});
// assert on the result
should(albums).eql([
new Album({
'title': '2016-06',
files: [june1, june2]
files: [a_2016_06, b_2016_06]
}),
new Album({
title: '2016-07',
files: [july1, july2]
files: [c_2016_07, d_2016_07]
})
]);
});
it('creates albums using a date hierarchy', function () {
// create files from different dates
var a_2015_06 = fixtures.photo({date: fixtures.date('2015-06-01')});
var b_2015_06 = fixtures.photo({date: fixtures.date('2015-06-10')});
var c_2016_07 = fixtures.photo({date: fixtures.date('2016-07-23')});
var d_2016_08 = fixtures.video({date: fixtures.date('2016-08-18')});
// group them per year, and nested month
var collection = { files: [a_2015_06, b_2015_06, c_2016_07, d_2016_08] };
var albums = bydate.albums(collection, {
grouping: 'YYYY/MM'
});
// assert on the result
should(albums).eql([
new Album({
'title': '2015',
files: [],
albums: [
new Album({
title: '06',
files: [a_2015_06, b_2015_06]
})
]
}),
new Album({
title: '2016',
files: [],
albums: [
new Album({
title: '07',
files: [c_2016_07]
}),
new Album({
title: '08',
files: [d_2016_08]
})
]
})
]);
});

@ -34,7 +34,6 @@ describe('ByFolder', function() {
// group them per folder
var collection = {files: [photo1, photo2]};
var albums = byfolder.albums(collection, {});
console.log(albums);
// assert on the result
should(albums).eql([
new Album({

Loading…
Cancel
Save