Fix #70: files in the root of the source folder get added to the root gallery (index.html)

Before, they were added to an album called “.” which didn’t look good,
and the corresponding “.html” file (no base name) caused issues on web servers
pull/67/merge
Romain 7 years ago
parent ac67a08da9
commit d24c5110cc

@ -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])
}
}

@ -31,6 +31,24 @@ describe('hierarchy', function () {
})
})
describe('empty mappers', function () {
const emptyMappers = ['', '.', null]
emptyMappers.forEach(value => {
it(`adds any photos mapped to <${value}> to the root gallery`, function () {
const media = [
fixtures.photo({path: 'IMG_000001.jpg'}),
fixtures.photo({path: 'IMG_000002.jpg'})
]
const mapper = media => value
const home = hierarchy.createAlbums(media, mapper)
should(home.albums.length).eql(0)
should(home.files.length).eql(2)
should(home.files[0].filename).eql('IMG_000001.jpg')
should(home.files[1].filename).eql('IMG_000002.jpg')
})
})
})
describe('nested albums', function () {
it('can group media into a single folder', function () {
const media = [

Loading…
Cancel
Save