Files
shpm/worker/index.js
T
nmemmertandClaude Sonnet 4.6 d18c855571 feat: settings panel + multi-folder scanning
Settings panel (⚙ in header):
- Watch Folders: add/remove paths, toggle enabled; worker polls every 30s
  and calls watcher.add/unwatch without restart
- Ingestion: thumb/preview size and quality, applied per-file at ingest time
- Duplicate Detection: configurable Hamming distance threshold
- Danger Zone: clear all dismissed duplicate pairs

Multi-folder scanning:
- watch_folders table replaces single PHOTO_DIR env var
- PHOTO_DIR env var seeds the first entry on startup
- settings table with defaults for all tunable values
- Worker reads settings from DB on each drain cycle so changes are live

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-08-18 07:58:44 -04:00

116 lines
3.2 KiB
JavaScript

import 'dotenv/config';
import chokidar from 'chokidar';
import path from 'path';
import fs from 'fs';
import { openDb } from './db.js';
import { ingestFile, isSupported } from './ingest.js';
const PHOTO_DIR = process.env.PHOTO_DIR ?? null;
const DB_PATH = process.env.DB_PATH ?? './data/library.db';
const POSTER_DIR = process.env.POSTER_DIR ?? './data/posters';
const db = openDb(DB_PATH);
// Seed PHOTO_DIR env var as the first watch folder (if set)
if (PHOTO_DIR) {
const resolved = path.resolve(PHOTO_DIR);
if (fs.existsSync(resolved)) db.addWatchFolder(resolved);
}
// Load all known filepaths into memory so the initial chokidar burst skips
// already-indexed files without touching the drain queue.
const indexed = new Set(
db.raw.prepare('SELECT filepath FROM photos').all().map(r => r.filepath)
);
console.log(`[worker] db at ${path.resolve(DB_PATH)} (${indexed.size} photos indexed)`);
const queue = [];
let busy = false;
let scanned = 0;
let errors = 0;
async function drain() {
if (busy) return;
busy = true;
db.setIngestStatus({ active: true, scanned, errors });
while (queue.length > 0) {
const fp = queue.shift();
const settings = db.getSettings();
try {
await ingestFile(fp, db, POSTER_DIR, settings);
indexed.add(fp);
scanned++;
db.setIngestStatus({ active: true, current: path.basename(fp), scanned, errors });
} catch (err) {
errors++;
console.error(`[worker] ${path.basename(fp)}: ${err.message}`);
db.setIngestStatus({ active: true, current: path.basename(fp), scanned, errors });
}
}
db.setIngestStatus({ active: false, current: null, scanned, errors });
busy = false;
}
// Single chokidar instance — paths are added/removed dynamically
const watcher = chokidar.watch([], {
persistent: true,
ignoreInitial: false,
awaitWriteFinish: { stabilityThreshold: 2000, pollInterval: 100 },
ignored: /(^|[/\\])\../,
});
watcher.on('add', (fp) => {
if (!isSupported(fp)) return;
const resolved = path.resolve(fp);
if (indexed.has(resolved)) return;
queue.push(resolved);
drain();
});
watcher.on('unlink', (fp) => {
const resolved = path.resolve(fp);
indexed.delete(resolved);
db.removePhoto(resolved);
console.log(`[worker] removed ${path.basename(fp)}`);
});
watcher.on('error', (err) => console.error('[worker] watcher error:', err));
// Sync watched paths against watch_folders table
const watching = new Set();
function syncWatchFolders() {
const folders = db.listWatchFolders().filter(f => f.enabled);
for (const { path: p } of folders) {
if (!watching.has(p)) {
console.log(`[worker] watching ${p}`);
watcher.add(p);
watching.add(p);
}
}
const enabledPaths = new Set(folders.map(f => f.path));
for (const p of watching) {
if (!enabledPaths.has(p)) {
console.log(`[worker] stopped watching ${p}`);
watcher.unwatch(p);
watching.delete(p);
}
}
}
// Initial sync then poll every 30s for UI-added folders
syncWatchFolders();
setInterval(syncWatchFolders, 30_000);
process.on('SIGINT', () => {
console.log('\n[worker] shutting down');
db.setIngestStatus({ active: false, current: null, scanned, errors });
watcher.close();
process.exit(0);
});