|
|
|
@ -1,4 +1,3 @@
|
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
const path = require('path')
|
|
|
|
|
const Album = require('./album')
|
|
|
|
|
|
|
|
|
@ -6,25 +5,31 @@ exports.createAlbums = function (collection, mapper, opts) {
|
|
|
|
|
// returns a top-level album for the home page
|
|
|
|
|
// under which all files are grouped into sub-albums
|
|
|
|
|
// and finalised recursively (calculate stats, etc...)
|
|
|
|
|
var home = new Album('Home')
|
|
|
|
|
home.albums = group(collection, mapper)
|
|
|
|
|
const home = group(collection, mapper)
|
|
|
|
|
home.finalize(opts)
|
|
|
|
|
return home
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function group (collection, mapper) {
|
|
|
|
|
var groups = {}
|
|
|
|
|
// this hashtable will contain all albums, with the full path as key
|
|
|
|
|
// e.g. groups['holidays/tokyo']
|
|
|
|
|
var groups = {
|
|
|
|
|
// we use path.basename() to parse the hierarchy
|
|
|
|
|
// so the home album is indexed as '.' to make it easy
|
|
|
|
|
// the value of '.' is local to this function, and doesn't appear anywhere else
|
|
|
|
|
'.': new Album('Home')
|
|
|
|
|
}
|
|
|
|
|
// put all files in the right albums
|
|
|
|
|
collection.forEach(function (media) {
|
|
|
|
|
var groupName = mapper(media)
|
|
|
|
|
if (!groupName) {
|
|
|
|
|
groupName = '.'
|
|
|
|
|
}
|
|
|
|
|
createAlbumHierarchy(groups, groupName)
|
|
|
|
|
groups[groupName].files.push(media)
|
|
|
|
|
})
|
|
|
|
|
// only return top-level albums
|
|
|
|
|
var topLevel = _.keys(groups).filter(function (dir) {
|
|
|
|
|
return path.dirname(dir) === '.'
|
|
|
|
|
})
|
|
|
|
|
return _.values(_.pick(groups, topLevel))
|
|
|
|
|
// return the top-level album
|
|
|
|
|
return groups['.']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createAlbumHierarchy (allGroupNames, segment) {
|
|
|
|
@ -35,10 +40,10 @@ function createAlbumHierarchy (allGroupNames, segment) {
|
|
|
|
|
createAlbumHierarchy(allGroupNames, parent)
|
|
|
|
|
}
|
|
|
|
|
// then create album if it doesn't exist
|
|
|
|
|
// and attach it to its parent
|
|
|
|
|
var lastSegment = path.basename(segment)
|
|
|
|
|
allGroupNames[segment] = new Album({title: lastSegment})
|
|
|
|
|
// then attach to parent
|
|
|
|
|
if (parent !== '.') {
|
|
|
|
|
if (!allGroupNames.hasOwnProperty(segment)) {
|
|
|
|
|
allGroupNames[segment] = new Album({title: lastSegment})
|
|
|
|
|
allGroupNames[parent].albums.push(allGroupNames[segment])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|