From d24c5110cce56995b2f65b8a9f249ebcf0edaa8b Mon Sep 17 00:00:00 2001 From: Romain Date: Sat, 22 Jul 2017 21:25:43 +1000 Subject: [PATCH] Fix #70: files in the root of the source folder get added to the root gallery (index.html) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/model/hierarchy.js | 29 +++++++++++++++++------------ test/model/hierarchy.spec.js | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/model/hierarchy.js b/src/model/hierarchy.js index 48f860a..c95eab9 100644 --- a/src/model/hierarchy.js +++ b/src/model/hierarchy.js @@ -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]) } } diff --git a/test/model/hierarchy.spec.js b/test/model/hierarchy.spec.js index ad5f9cc..43d57f5 100644 --- a/test/model/hierarchy.spec.js +++ b/test/model/hierarchy.spec.js @@ -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 = [