Skip to content

Commit

Permalink
feat: support multiple scan modes for partial gallery generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rprieto authored and ebardsley committed May 25, 2023
1 parent 2491aa9 commit 8653579
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 152 deletions.
6 changes: 6 additions & 0 deletions src/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const OPTIONS = {
// ------------------------------------
// Input options
// ------------------------------------
'scan-mode': {
group: 'Input options:',
description: 'How files are indexed',
choices: ['full', 'partial', 'incremental'],
'default': 'full'
},
'include-photos': {
group: 'Input options:',
description: 'Include photos in the gallery',
Expand Down
32 changes: 24 additions & 8 deletions src/components/index/delta.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
/* eslint-disable no-prototype-builtins */
const _ = require('lodash')
const GlobPattern = require('./pattern')

/*
Calculate the difference between files on disk and already indexed
- databaseMap = hashmap of {path, timestamp}
- diskMap = hashmap of {path, timestamp}
*/
exports.calculate = (databaseMap, diskMap) => {
exports.calculate = (databaseMap, diskMap, { scanMode = 'full', include, exclude }) => {
const delta = {
unchanged: [],
added: [],
modified: [],
deleted: []
deleted: [],
skipped: []
}
// TODO: the glob pattern should be passed in
// It should be identical to the one used by the Glob object that scans the disk
// For now, partial scans only uses the include/exclude filter
// If we pass it it, other filters would apply as well (e.g. photo/video/raw...)
const pattern = new GlobPattern({ include, exclude, extensions: [] })
_.each(databaseMap, (dbTime, dbPath) => {
if (diskMap.hasOwnProperty(dbPath)) {
const modified = Math.abs(dbTime - diskMap[dbPath]) > 1000
if (modified) {
delta.modified.push(dbPath)
const shouldProcessDBEntry = (scanMode === 'full') ? true : pattern.match(dbPath)
if (shouldProcessDBEntry) {
if (diskMap.hasOwnProperty(dbPath)) {
const modified = Math.abs(dbTime - diskMap[dbPath]) > 1000
if (modified) {
delta.modified.push(dbPath)
} else {
delta.unchanged.push(dbPath)
}
} else {
delta.unchanged.push(dbPath)
if (scanMode === 'incremental') {
delta.skipped.push(dbPath)
} else {
delta.deleted.push(dbPath)
}
}
} else {
delta.deleted.push(dbPath)
delta.skipped.push(dbPath)
}
})
_.each(diskMap, (diskTime, diskPath) => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ class Index {
if (err) return console.error('error', err)

// calculate the difference: which files have been added, modified, etc
const deltaFiles = delta.calculate(databaseMap, diskMap)
const deltaFiles = delta.calculate(databaseMap, diskMap, options)
emitter.emit('stats', {
unchanged: deltaFiles.unchanged.length,
added: deltaFiles.added.length,
modified: deltaFiles.modified.length,
deleted: deltaFiles.deleted.length,
skipped: deltaFiles.skipped.length,
total: Object.keys(diskMap).length
})

Expand Down
10 changes: 6 additions & 4 deletions src/components/index/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const micromatch = require('micromatch')

class GlobPattern {
constructor ({ include, exclude, extensions }) {
this.includeList = include
this.excludeList = exclude
this.includeList = (include && include.length > 0) ? include : ['**/**']
this.excludeList = exclude || []
this.includeFolders = _.uniq(_.flatMap(this.includeList, this.subFolders))
this.directoryExcludeList = exclude.concat(['**/@eaDir/**', '#recycle/**'])
this.directoryExcludeList = this.excludeList.concat(['**/@eaDir/**', '#recycle/**'])
this.extensions = extPattern(extensions)
}

Expand Down Expand Up @@ -43,7 +43,9 @@ class GlobPattern {
}

function extPattern (extensions) {
if (extensions.length === 1) {
if (extensions.length === 0) {
return '**/*'
} else if (extensions.length === 1) {
return '**/*.' + extensions[0]
} else {
return '**/*.{' + extensions.join(',') + '}'
Expand Down
Loading

0 comments on commit 8653579

Please sign in to comment.