2017-07-24 11:40:03 +00:00
|
|
|
/*
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
Represents an album, which is made of many photos and videos
|
|
|
|
This is a virtual grouping of files, independent of the location on disk
|
|
|
|
A single photo/video could exist in multiple albums
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
const _ = require('lodash')
|
|
|
|
const path = require('path')
|
|
|
|
const url = require('url')
|
2017-03-06 12:46:46 +00:00
|
|
|
var index = 0
|
2016-10-10 11:49:08 +00:00
|
|
|
|
|
|
|
// number of images to show in the album preview grid
|
2017-07-24 11:40:03 +00:00
|
|
|
const PREVIEW_COUNT = 10
|
2016-10-10 11:49:08 +00:00
|
|
|
|
2017-07-24 11:40:03 +00:00
|
|
|
const SORT_ALBUMS_BY = {
|
2017-03-06 12:46:46 +00:00
|
|
|
'title': function (album) { return album.title },
|
|
|
|
'start-date': function (album) { return album.stats.fromDate },
|
|
|
|
'end-date': function (album) { return album.stats.toDate }
|
|
|
|
}
|
2016-10-10 11:49:08 +00:00
|
|
|
|
2017-07-24 11:40:03 +00:00
|
|
|
const SORT_MEDIA_BY = {
|
|
|
|
'filename': function (file) { return file.filename },
|
|
|
|
'date': function (file) { return file.meta.date }
|
2017-03-06 12:46:46 +00:00
|
|
|
}
|
2016-10-10 11:49:08 +00:00
|
|
|
|
2017-07-24 11:40:03 +00:00
|
|
|
const PREVIEW_MISSING = {
|
2016-10-16 11:24:07 +00:00
|
|
|
urls: {
|
2017-03-06 11:16:36 +00:00
|
|
|
thumbnail: 'public/missing.png'
|
2016-10-16 11:24:07 +00:00
|
|
|
}
|
2017-03-06 12:46:46 +00:00
|
|
|
}
|
2016-10-16 11:24:07 +00:00
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
function Album (opts) {
|
|
|
|
if (typeof opts === 'string') opts = { title: opts }
|
|
|
|
this.id = opts.id || ++index
|
|
|
|
this.title = opts.title || ('Album ' + this.id)
|
|
|
|
this.basename = sanitise(this.title)
|
|
|
|
this.files = opts.files || []
|
|
|
|
this.albums = opts.albums || []
|
|
|
|
this.depth = 0
|
|
|
|
this.home = false
|
|
|
|
this.stats = null
|
|
|
|
this.previews = null
|
2016-10-10 11:49:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
Album.prototype.finalize = function (options, parent) {
|
|
|
|
options = options || {}
|
|
|
|
var albumsOutputFolder = options.albumsOutputFolder || '.'
|
2017-02-07 12:19:22 +00:00
|
|
|
// calculate final file paths and URLs
|
|
|
|
if (parent == null) {
|
2017-03-08 09:12:05 +00:00
|
|
|
this.path = options.index || 'index.html'
|
|
|
|
this.url = options.index || 'index.html'
|
2017-03-06 12:46:46 +00:00
|
|
|
this.depth = 0
|
2017-02-07 12:19:22 +00:00
|
|
|
} else {
|
|
|
|
if (parent.depth > 0) {
|
2017-03-06 12:46:46 +00:00
|
|
|
this.basename = parent.basename + '-' + this.basename
|
2017-02-07 12:19:22 +00:00
|
|
|
}
|
2017-03-06 12:46:46 +00:00
|
|
|
this.path = path.join(albumsOutputFolder, this.basename + '.html')
|
|
|
|
this.url = url.resolve(albumsOutputFolder + '/', this.basename + '.html')
|
|
|
|
this.depth = parent.depth + 1
|
2017-02-07 12:19:22 +00:00
|
|
|
}
|
|
|
|
// then finalize all nested albums (which uses the parent basename)
|
2016-10-10 11:49:08 +00:00
|
|
|
for (var i = 0; i < this.albums.length; ++i) {
|
2017-03-06 12:46:46 +00:00
|
|
|
this.albums[i].finalize(options, this)
|
2016-10-10 11:49:08 +00:00
|
|
|
}
|
2016-10-16 11:24:07 +00:00
|
|
|
// perform stats & other calculations
|
|
|
|
// once the nested albums have been finalized too
|
2017-03-06 12:46:46 +00:00
|
|
|
this.home = this.depth === 0
|
|
|
|
this.calculateStats()
|
|
|
|
this.calculateSummary()
|
|
|
|
this.sort(options)
|
|
|
|
this.pickPreviews()
|
|
|
|
}
|
2016-10-10 11:49:08 +00:00
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
Album.prototype.calculateStats = function () {
|
2016-10-10 11:49:08 +00:00
|
|
|
// nested albums
|
2017-03-06 12:46:46 +00:00
|
|
|
var nestedPhotos = _.map(this.albums, 'stats.photos')
|
|
|
|
var nestedVideos = _.map(this.albums, 'stats.videos')
|
|
|
|
var nestedFromDates = _.map(this.albums, 'stats.fromDate')
|
|
|
|
var nestedToDates = _.map(this.albums, 'stats.toDate')
|
2016-10-10 11:49:08 +00:00
|
|
|
// current level
|
2018-12-11 22:11:03 +00:00
|
|
|
var currentPhotos = _.filter(this.files, { type: 'image' }).length
|
|
|
|
var currentVideos = _.filter(this.files, { type: 'video' }).length
|
2017-07-24 11:40:03 +00:00
|
|
|
var currentFromDate = _.map(this.files, 'meta.date')
|
|
|
|
var currentToDate = _.map(this.files, 'meta.date')
|
2016-10-10 11:49:08 +00:00
|
|
|
// aggregate all stats
|
|
|
|
this.stats = {
|
2017-03-06 12:46:46 +00:00
|
|
|
albums: this.albums.length,
|
|
|
|
photos: _.sum(_.compact(_.concat(nestedPhotos, currentPhotos))) || 0,
|
|
|
|
videos: _.sum(_.compact(_.concat(nestedVideos, currentVideos))) || 0,
|
2016-10-10 11:49:08 +00:00
|
|
|
fromDate: _.min(_.compact(_.concat(nestedFromDates, currentFromDate))),
|
2017-03-06 12:46:46 +00:00
|
|
|
toDate: _.max(_.compact(_.concat(nestedToDates, currentToDate)))
|
|
|
|
}
|
|
|
|
this.stats.total = this.stats.photos + this.stats.videos
|
2016-10-10 11:49:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
Album.prototype.calculateSummary = function () {
|
2016-10-10 11:49:08 +00:00
|
|
|
var items = [
|
|
|
|
itemCount(this.stats.albums, 'album'),
|
|
|
|
itemCount(this.stats.photos, 'photo'),
|
|
|
|
itemCount(this.stats.videos, 'video')
|
2017-03-06 12:46:46 +00:00
|
|
|
]
|
|
|
|
this.summary = _.compact(items).join(', ')
|
|
|
|
}
|
2016-10-10 11:49:08 +00:00
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
Album.prototype.sort = function (options) {
|
|
|
|
this.files = _.orderBy(this.files, SORT_MEDIA_BY[options.sortMediaBy], options.sortMediaDirection)
|
|
|
|
this.albums = _.orderBy(this.albums, SORT_ALBUMS_BY[options.sortAlbumsBy], options.sortAlbumsDirection)
|
|
|
|
}
|
2016-10-10 11:49:08 +00:00
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
Album.prototype.pickPreviews = function () {
|
2016-10-16 11:24:07 +00:00
|
|
|
// also consider previews from nested albums
|
2017-03-06 12:46:46 +00:00
|
|
|
var nestedPicks = _.flatten(_.map(this.albums, 'previews')).filter(function (file) {
|
|
|
|
return file !== PREVIEW_MISSING
|
|
|
|
})
|
2016-11-17 01:34:58 +00:00
|
|
|
// then pick the top ones
|
2017-03-06 12:46:46 +00:00
|
|
|
var potentialPicks = _.concat(this.files, nestedPicks)
|
|
|
|
this.previews = potentialPicks.slice(0, PREVIEW_COUNT)
|
2016-10-16 11:24:07 +00:00
|
|
|
// and fill the gap with a placeholder
|
2017-03-06 12:46:46 +00:00
|
|
|
var missing = PREVIEW_COUNT - this.previews.length
|
2016-10-10 11:49:08 +00:00
|
|
|
for (var i = 0; i < missing; ++i) {
|
2017-03-06 12:46:46 +00:00
|
|
|
this.previews.push(PREVIEW_MISSING)
|
2016-10-10 11:49:08 +00:00
|
|
|
}
|
2017-03-06 12:46:46 +00:00
|
|
|
}
|
2016-10-10 11:49:08 +00:00
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
function sanitise (filename) {
|
|
|
|
return filename.replace(/[^a-z0-9-_]/ig, '')
|
2016-10-10 11:49:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
function itemCount (count, type) {
|
|
|
|
if (count === 0) return ''
|
|
|
|
var plural = (count > 1) ? 's' : ''
|
|
|
|
return '' + count + ' ' + type + plural
|
2016-10-10 11:49:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 00:35:27 +00:00
|
|
|
// for testing purposes
|
2017-03-06 12:46:46 +00:00
|
|
|
Album.resetIds = function () {
|
|
|
|
index = 0
|
|
|
|
}
|
2016-11-02 00:35:27 +00:00
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
module.exports = Album
|