← back to Butlr
lib/orphan-recordings.js
33 lines
// Find MP3s in data/recordings/ with no matching call_id in calls.json.
// Pure function — no Express, no req/res — so unit-testable.
const fs = require('fs');
const path = require('path');
const data = require('./data');
const transcribe = require('./transcribe');
function scanOrphans({ dir = transcribe.RECORDINGS_DIR, knownIds = null } = {}) {
if (!fs.existsSync(dir)) return [];
const ids = knownIds || new Set(data.listCallsUnscoped().map(c => c.id));
const files = fs.readdirSync(dir).filter(f => f.endsWith('.mp3'));
const orphans = [];
for (const f of files) {
const base = f.replace(/\.mp3$/, '');
if (ids.has(base)) continue;
const fp = path.join(dir, f);
let st;
try { st = fs.statSync(fp); } catch { continue; }
orphans.push({
id: base,
filename: f,
size_bytes: st.size,
size_kb: (st.size / 1024).toFixed(1),
mtime: st.mtime.toISOString().replace('T', ' ').slice(0, 19),
});
}
orphans.sort((a, b) => b.mtime.localeCompare(a.mtime));
return orphans;
}
module.exports = { scanOrphans };