mirror of https://github.com/thumbsup/thumbsup
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
8 years ago
|
const path = require('path')
|
||
7 years ago
|
const Album = require('../model/album')
|
||
8 years ago
|
|
||
8 years ago
|
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...)
|
||
7 years ago
|
const home = group(collection, mapper)
|
||
8 years ago
|
home.finalize(opts)
|
||
|
return home
|
||
|
}
|
||
8 years ago
|
|
||
|
function group (collection, mapper) {
|
||
7 years ago
|
// 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')
|
||
|
}
|
||
8 years ago
|
// put all files in the right albums
|
||
7 years ago
|
collection.forEach(function (file) {
|
||
|
var groupName = mapper(file)
|
||
7 years ago
|
if (!groupName) {
|
||
|
groupName = '.'
|
||
|
}
|
||
8 years ago
|
createAlbumHierarchy(groups, groupName)
|
||
7 years ago
|
groups[groupName].files.push(file)
|
||
8 years ago
|
})
|
||
7 years ago
|
// return the top-level album
|
||
|
return groups['.']
|
||
8 years ago
|
}
|
||
|
|
||
|
function createAlbumHierarchy (allGroupNames, segment) {
|
||
|
if (!allGroupNames.hasOwnProperty(segment)) {
|
||
|
// create parent albums first
|
||
|
var parent = path.dirname(segment)
|
||
|
if (parent !== '.') {
|
||
|
createAlbumHierarchy(allGroupNames, parent)
|
||
|
}
|
||
|
// then create album if it doesn't exist
|
||
7 years ago
|
// and attach it to its parent
|
||
8 years ago
|
var lastSegment = path.basename(segment)
|
||
7 years ago
|
allGroupNames[segment] = new Album({title: lastSegment})
|
||
|
allGroupNames[parent].albums.push(allGroupNames[segment])
|
||
8 years ago
|
}
|
||
|
}
|