Files
shpm/worker/index.js
T
nmemmertandClaude Sonnet 4.6 f0e237ab2b feat: filter sidebar, ingest progress, starred photos
- Left filter sidebar: All Photos / Favorites / Date by Year / Tags / Collections
  with counts, See more/less toggles, and blue active-row highlight
- SSE ingest badge in header: pulsing dot + filename + indexed count during scan;
  auto-refreshes the library when a scan finishes
- Starred/Favorites: starred column on photos, star button in lightbox toolbar,
  Favorites row in sidebar, ?starred=1 filter on /api/photos
- GET /api/library/stats endpoint (totals, years, tags, collections)
- GET /api/ingest/stream SSE endpoint (polls ingest_status table every 1.5s)
- Worker writes ingest_status (active, current file, scanned count) to SQLite

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-08-17 17:04:01 -04:00

70 lines
1.9 KiB
JavaScript

import 'dotenv/config';
import chokidar from 'chokidar';
import path from 'path';
import { openDb } from './db.js';
import { ingestFile, isSupported } from './ingest.js';
const PHOTO_DIR = process.env.PHOTO_DIR ?? './photos';
const DB_PATH = process.env.DB_PATH ?? './data/library.db';
const POSTER_DIR = process.env.POSTER_DIR ?? './data/posters';
const db = openDb(DB_PATH);
console.log(`[worker] watching ${path.resolve(PHOTO_DIR)}`);
console.log(`[worker] db at ${path.resolve(DB_PATH)} (${db.count()} 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();
try {
await ingestFile(fp, db, POSTER_DIR);
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;
}
const watcher = chokidar.watch(PHOTO_DIR, {
persistent: true,
ignoreInitial: false,
awaitWriteFinish: { stabilityThreshold: 2000, pollInterval: 100 },
ignored: /(^|[/\\])\../,
});
watcher.on('add', (fp) => {
if (!isSupported(fp)) return;
queue.push(path.resolve(fp));
drain();
});
watcher.on('unlink', (fp) => {
db.removePhoto(path.resolve(fp));
console.log(`[worker] removed ${path.basename(fp)}`);
});
watcher.on('error', (err) => console.error('[worker] watcher error:', err));
process.on('SIGINT', () => {
console.log('\n[worker] shutting down');
db.setIngestStatus({ active: false, current: null, scanned, errors });
watcher.close();
process.exit(0);
});