-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
647 lines (568 loc) · 23.4 KB
/
Copy pathserver.js
File metadata and controls
647 lines (568 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
'use strict';
const express = require('express');
const path = require('path');
const os = require('os');
const fs = require('fs');
const fsp = fs.promises;
const crypto = require('crypto');
const { createJob, getJob, deleteJob, jobs: jobsMap } = require('./lib/jobStore');
const { runScan } = require('./lib/scanner');
const { buildPlan, executePlan, undoRun } = require('./lib/mover');
const { knownCategories } = require('./lib/categorize');
const { testConnection, DEFAULT_URL, DEFAULT_MODEL, DEFAULT_PROVIDER, PROVIDERS } = require('./lib/llm');
const { listDrives } = require('./lib/drives');
const { learnRule, getAllRules, deleteRule } = require('./lib/learnedRules');
const { sharpAvailable } = require('./lib/perceptualHash');
const persistence = require('./lib/persistence');
const { snapshotTreesToFile, streamSnapshot } = require('./lib/treeSnapshot');
const { logJob } = require('./lib/jobLog');
const { readTemps } = require('./lib/thermal');
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
const THUMBNAIL_EXTS = new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp']);
// ---------- Folder browsing ----------
app.get('/api/browse', async (req, res) => {
const dir = req.query.dir || os.homedir();
try {
const resolved = path.resolve(dir);
const stat = await fsp.stat(resolved);
if (!stat.isDirectory()) return res.status(400).json({ error: 'Not a directory' });
const entries = await fsp.readdir(resolved, { withFileTypes: true });
const dirs = entries
.filter((e) => e.isDirectory() && !e.name.startsWith('.'))
.map((e) => ({ name: e.name, path: path.join(resolved, e.name) }))
.sort((a, b) => a.name.localeCompare(b.name));
const parent = path.dirname(resolved);
res.json({
current: resolved,
parent: parent === resolved ? null : parent,
entries: dirs,
});
} catch (err) {
res.status(400).json({ error: err.message });
}
});
app.get('/api/drives', async (req, res) => {
try {
const drives = await listDrives();
res.json({ drives, homeDir: os.homedir() });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// ---------- Capabilities / LLM test ----------
app.get('/api/capabilities', (req, res) => {
res.json({
perceptualHashing: sharpAvailable,
// Local LLM backends the user can choose from, with their default URLs.
providers: Object.entries(PROVIDERS).map(([id, p]) => ({ id, defaultUrl: p.defaultUrl })),
defaultProvider: DEFAULT_PROVIDER,
defaultModel: DEFAULT_MODEL,
});
});
// Live CPU/GPU temperatures (best-effort; { available:false } if this machine can't be read).
app.get('/api/thermal', async (req, res) => {
try {
res.json(await readTemps());
} catch (err) {
res.json({ available: false, error: err.message });
}
});
app.post('/api/llm/test', async (req, res) => {
const { provider, url, model } = req.body || {};
const result = await testConnection({ provider: provider || DEFAULT_PROVIDER, url: url || DEFAULT_URL, model: model || DEFAULT_MODEL });
res.json(result);
});
// ---------- Categories & learned rules ----------
app.get('/api/categories', (req, res) => {
res.json({ categories: knownCategories() });
});
app.get('/api/rules', async (req, res) => {
res.json({ rules: await getAllRules() });
});
app.delete('/api/rules/:ext', async (req, res) => {
await deleteRule(req.params.ext);
res.json({ ok: true });
});
// ---------- Scan lifecycle ----------
app.get('/api/scan', (req, res) => {
const RESUMABLE = new Set(['done', 'scanning', 'moving', 'pending']);
const list = [...jobsMap.values()]
.filter((j) => RESUMABLE.has(j.status))
.map((j) => ({ id: j.id, status: j.status, sources: j.sources, destination: j.destination, fileCount: j.files.size, createdAt: j.createdAt }));
res.json({ jobs: list });
});
app.post('/api/scan', async (req, res) => {
const {
sources, destination, useLLM, llmProvider, llmUrl, llmModel, aiExtractStrays,
ignoreNodeModules, ignoreJunkFolders, detectProjects, detectThemedFolders,
organizeByDate, organizeByMusicTags, findSimilarImages, thermal,
} = req.body || {};
const clamp = (v, def, lo, hi) => {
const n = Number(v);
return Number.isFinite(n) ? Math.min(hi, Math.max(lo, n)) : def;
};
let thermalCfg = { enabled: false };
if (thermal && thermal.enabled) {
const maxTempC = clamp(thermal.maxTempC, 85, 45, 110);
let resumeTempC = clamp(thermal.resumeTempC, maxTempC - 10, 30, 105);
if (resumeTempC >= maxTempC) resumeTempC = maxTempC - 5; // keep hysteresis
thermalCfg = {
enabled: true,
maxTempC,
resumeTempC,
pollMs: Math.max(2000, clamp(thermal.pollSeconds, 5, 2, 120) * 1000),
maxWaitMs: Math.max(30000, clamp(thermal.maxWaitMinutes, 10, 1, 120) * 60000),
};
}
if (!Array.isArray(sources) || sources.length === 0) {
return res.status(400).json({ error: 'At least one source folder is required.' });
}
if (!destination) {
return res.status(400).json({ error: 'A destination folder is required.' });
}
const resolvedSources = sources.map((s) => path.resolve(s));
const resolvedDest = path.resolve(destination);
for (const src of resolvedSources) {
if (resolvedDest === src || resolvedDest.startsWith(src + path.sep)) {
return res.status(400).json({ error: `Destination cannot be inside source folder: ${src}` });
}
}
for (const p of [...resolvedSources, resolvedDest]) {
try {
await fsp.access(p);
} catch {
return res.status(400).json({ error: `Folder does not exist or is not accessible: ${p}` });
}
}
const job = createJob({
id: crypto.randomUUID(),
status: 'pending',
sources: resolvedSources,
destination: resolvedDest,
useLLM: !!useLLM,
llmProvider: llmProvider || DEFAULT_PROVIDER,
llmUrl: llmUrl || DEFAULT_URL,
llmModel: llmModel || DEFAULT_MODEL,
aiExtractStrays: aiExtractStrays !== false, // default ON when the LLM is enabled
thermal: thermalCfg,
temps: null,
cooling: null,
ignoreNodeModules: !!ignoreNodeModules,
ignoreJunkFolders: !!ignoreJunkFolders,
detectProjects: detectProjects !== false, // default ON - this is the safety-critical one
detectThemedFolders: detectThemedFolders !== false, // default ON - keeps human-organized folders together
organizeByDate: !!organizeByDate,
organizeByMusicTags: !!organizeByMusicTags,
findSimilarImages: !!findSimilarImages && sharpAvailable,
progress: { phase: 'pending', filesFound: 0, filesProcessed: 0 },
files: new Map(),
duplicateGroups: new Map(),
similarGroups: [],
projects: [],
themedFolders: [],
error: null,
createdAt: Date.now(),
report: null,
});
runScan(job).then(() => persistence.saveJobSnapshot(job));
res.json({ jobId: job.id });
});
app.get('/api/scan/:jobId/status', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
res.json({
status: job.status,
progress: job.progress,
error: job.error,
log: (job.log || []).slice(-200),
temps: job.temps || null,
cooling: job.cooling || null,
runId: job.runId || null,
// Once a move finishes, hand the report straight back through the poll so the
// frontend can render the report view without a second round-trip.
report: job.status === 'completed' ? job.report : null,
treeBeforeStats: job.treeBeforeStats || null,
treeAfterStats: job.treeAfterStats || null,
});
});
function serializeJob(job) {
const files = [...job.files.values()].map((f) => ({
id: f.id,
name: f.name,
ext: f.ext,
absPath: f.absPath,
sourceRoot: f.sourceRoot,
relPath: f.relPath,
size: f.size,
mtime: f.mtime,
category: f.category,
subPath: f.subPath,
excluded: f.excluded,
duplicateGroupId: f.duplicateGroupId,
similarGroupId: f.similarGroupId,
themedFolderId: f.themedFolderId || null,
hasThumbnail: THUMBNAIL_EXTS.has(f.ext),
}));
const duplicateGroups = [...job.duplicateGroups.values()].map((g) => ({
id: g.id,
size: g.size,
fileIds: g.fileIds,
resolution: g.resolution,
}));
return {
id: job.id,
status: job.status,
sources: job.sources,
destination: job.destination,
progress: job.progress,
files,
duplicateGroups,
similarGroups: job.similarGroups || [],
projects: job.projects || [],
themedFolders: job.themedFolders || [],
ignoreNodeModules: job.ignoreNodeModules,
ignoreJunkFolders: job.ignoreJunkFolders,
detectProjects: job.detectProjects,
detectThemedFolders: job.detectThemedFolders,
organizeByDate: job.organizeByDate,
organizeByMusicTags: job.organizeByMusicTags,
findSimilarImages: job.findSimilarImages,
ignoredNodeModulesDirs: job.ignoredNodeModulesDirs || [],
ignoredJunkDirs: job.ignoredJunkDirs || [],
ignoredJunkFiles: job.ignoredJunkFiles || [],
report: job.report,
};
}
app.get('/api/scan/:jobId', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
res.json(serializeJob(job));
});
// ---------- Thumbnails ----------
app.get('/api/scan/:jobId/files/:fileId/thumbnail', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).end();
const file = job.files.get(req.params.fileId);
if (!file || !THUMBNAIL_EXTS.has(file.ext)) return res.status(404).end();
res.sendFile(file.absPath, (err) => {
if (err && !res.headersSent) res.status(404).end();
});
});
// ---------- CRUD on scanned files ----------
app.put('/api/scan/:jobId/files/:fileId', async (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
const file = job.files.get(req.params.fileId);
if (!file) return res.status(404).json({ error: 'File not found' });
const { category, excluded } = req.body || {};
if (typeof category === 'string' && category.trim()) {
file.category = category.trim().toLowerCase().replace(/\s+/g, '_');
if (file.ext) await learnRule(file.ext, file.category); // remember this choice for next time
}
if (typeof excluded === 'boolean') {
file.excluded = excluded;
}
persistence.saveJobSnapshot(job);
res.json({ ok: true, file });
});
app.delete('/api/scan/:jobId/files/:fileId', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
const file = job.files.get(req.params.fileId);
if (!file) return res.status(404).json({ error: 'File not found' });
file.excluded = true;
persistence.saveJobSnapshot(job);
res.json({ ok: true });
});
app.post('/api/scan/:jobId/files/bulk-category', async (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
const { fileIds, category } = req.body || {};
if (!Array.isArray(fileIds) || !category) return res.status(400).json({ error: 'fileIds and category required' });
const normalized = category.trim().toLowerCase().replace(/\s+/g, '_');
let updated = 0;
for (const id of fileIds) {
const file = job.files.get(id);
if (file) {
file.category = normalized;
if (file.ext) await learnRule(file.ext, normalized);
updated += 1;
}
}
persistence.saveJobSnapshot(job);
res.json({ ok: true, updated });
});
app.post('/api/scan/:jobId/files/bulk-exclude', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
const { fileIds, excluded } = req.body || {};
if (!Array.isArray(fileIds)) return res.status(400).json({ error: 'fileIds required' });
let updated = 0;
for (const id of fileIds) {
const file = job.files.get(id);
if (file) {
file.excluded = !!excluded;
updated += 1;
}
}
persistence.saveJobSnapshot(job);
res.json({ ok: true, updated });
});
// ---------- Detected project folders ----------
app.put('/api/scan/:jobId/projects/:projectId', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
const project = (job.projects || []).find((p) => p.id === req.params.projectId);
if (!project) return res.status(404).json({ error: 'Project not found' });
const { excluded } = req.body || {};
if (typeof excluded === 'boolean') project.excluded = excluded;
persistence.saveJobSnapshot(job);
res.json({ ok: true, project });
});
// ---------- Detected themed folders (e.g. "2026 Birthday Photos") ----------
app.put('/api/scan/:jobId/themed-folders/:folderId', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
const folder = (job.themedFolders || []).find((f) => f.id === req.params.folderId);
if (!folder) return res.status(404).json({ error: 'Themed folder not found' });
const { excluded } = req.body || {};
if (typeof excluded === 'boolean') folder.excluded = excluded;
persistence.saveJobSnapshot(job);
res.json({ ok: true, folder });
});
// ---------- Duplicate resolution ----------
app.put('/api/scan/:jobId/duplicates/:groupId', (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
const group = job.duplicateGroups.get(req.params.groupId);
if (!group) return res.status(404).json({ error: 'Duplicate group not found' });
const { type, keepId } = req.body || {};
if (type === 'merge') {
if (!group.fileIds.includes(keepId)) {
return res.status(400).json({ error: 'keepId must be one of this group\'s files' });
}
group.resolution = { type: 'merge', keepId };
} else if (type === 'keep_all') {
group.resolution = { type: 'keep_all' };
} else {
return res.status(400).json({ error: 'type must be "merge" or "keep_all"' });
}
persistence.saveJobSnapshot(job);
res.json({ ok: true, group });
});
// ---------- Confirm & execute ----------
/**
* Runs the actual move in the background so the frontend can poll for live
* progress and a log. Fully journaled and crash-resumable: the resolved plan is
* written before any file is touched, each op's outcome is journaled as it
* completes, and the undo manifest is written the moment the moves finish.
*/
async function runMove(job, runId) {
try {
job.progress = { phase: 'planning', moved: 0, total: 0 };
logJob(job, 'Building the move plan…');
const plan = await buildPlan(job);
await persistence.savePlan(runId, {
...plan, jobId: job.id, sources: job.sources, destination: job.destination, createdAt: Date.now(),
});
job.progress = { phase: 'snapshotting_before', moved: 0, total: plan.ops.length };
logJob(job, `Plan ready: ${plan.ops.length} operation(s). Snapshotting source structure…`);
persistence.saveJobSnapshot(job);
let treeBeforeStats = null;
try {
treeBeforeStats = await snapshotTreesToFile(job.sources, persistence.treeBeforePath(runId));
} catch (snapErr) {
logJob(job, `Before-snapshot failed (continuing): ${snapErr.message}`, 'warn');
}
job.progress.phase = 'moving';
logJob(job, 'Moving files…');
const report = await executePlan(plan, {
appendJournal: (entry) => persistence.appendJournalEntry(runId, entry),
onProgress: (processed, total) => {
job.progress.moved = processed;
job.progress.total = total;
if (processed === total || processed % 25 === 0) {
logJob(job, `${processed}/${total} operation(s) done…`);
persistence.saveJobSnapshot(job);
}
},
});
job.report = report;
// Persist the undo manifest IMMEDIATELY (rollback depends only on this).
job.progress.phase = 'finalizing';
let manifest = await persistence.saveManifest(runId, { job, report, treeBeforeStats, treeAfterStats: null });
await persistence.deleteJobSnapshot(job.id);
// After-snapshot is inspection-only and therefore best-effort.
let treeAfterStats = null;
try {
job.progress.phase = 'snapshotting_after';
treeAfterStats = await snapshotTreesToFile([job.destination], persistence.treeAfterPath(runId));
manifest = await persistence.saveManifest(runId, { job, report, treeBeforeStats, treeAfterStats });
} catch (snapErr) {
logJob(job, `After-snapshot failed (run still recorded and reversible): ${snapErr.message}`, 'warn');
}
job.treeBeforeStats = treeBeforeStats;
job.treeAfterStats = treeAfterStats;
job.runId = manifest.id;
job.status = 'completed';
job.progress.phase = 'done';
const deletedTotal = report.deleted.length + report.deletedDirs.length + report.deletedFiles.length;
logJob(job, `Done. Moved ${report.moved.length}, deleted ${deletedTotal}, errors ${report.errors.length}.`);
} catch (err) {
job.status = 'error';
job.error = err.message;
logJob(job, `Move failed: ${err.message}`, 'error');
persistence.saveJobSnapshot(job);
}
}
app.post('/api/scan/:jobId/confirm', async (req, res) => {
const job = getJob(req.params.jobId);
if (!job) return res.status(404).json({ error: 'Job not found' });
if (job.status !== 'done') return res.status(400).json({ error: 'Job is not ready to confirm yet' });
job.status = 'moving';
job.progress = { phase: 'planning', moved: 0, total: 0 };
const runId = await persistence.createRun();
job.runId = runId;
logJob(job, 'Confirm received.');
persistence.saveJobSnapshot(job);
// Respond immediately; the client polls /status for progress + log and the final report.
res.json({ ok: true, runId, started: true });
runMove(job, runId);
});
app.delete('/api/scan/:jobId', async (req, res) => {
deleteJob(req.params.jobId);
await persistence.deleteJobSnapshot(req.params.jobId);
res.json({ ok: true });
});
// ---------- Run history & undo ----------
app.get('/api/runs', async (req, res) => {
try {
res.json({ runs: await persistence.listRuns() });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get('/api/runs/:runId', async (req, res) => {
try {
res.json(await persistence.getRun(req.params.runId));
} catch (err) {
res.status(404).json({ error: 'Run not found' });
}
});
async function serveSnapshot(indexFile, res) {
try {
const served = await streamSnapshot(indexFile, res);
if (!served && !res.headersSent) res.status(404).json({ error: 'Snapshot not found' });
} catch (err) {
if (!res.headersSent) res.status(500).json({ error: err.message });
else res.end();
}
}
// Reconstructs the (possibly chunked) snapshot into one logical JSON tree, streamed
// chunk-by-chunk so the server never buffers a giant file in memory.
app.get('/api/runs/:runId/tree/before', (req, res) => {
serveSnapshot(persistence.treeBeforePath(req.params.runId), res);
});
app.get('/api/runs/:runId/tree/after', (req, res) => {
serveSnapshot(persistence.treeAfterPath(req.params.runId), res);
});
app.post('/api/runs/:runId/undo', async (req, res) => {
try {
const manifest = await persistence.getRun(req.params.runId);
if (manifest.undoneAt) {
return res.status(400).json({ error: 'This run was already undone.' });
}
const result = await undoRun(manifest);
// A run only counts as fully undone when every recorded move was reversed.
// Otherwise we leave it retryable (undoneAt stays null) and report the truth.
const fullyUndone = result.errors.length === 0 && result.notRestorable.length === 0;
await persistence.markRunUndone(req.params.runId, result, fullyUndone);
res.json({ ok: true, result, fullyUndone });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// ---------- Startup: resume anything left unfinished by a crash/power loss ----------
/**
* Finishes any move that was interrupted mid-flight. A run with a plan but no
* manifest is incomplete: we replay only the ops the journal hasn't marked done
* (execution is idempotent for the rest), then write the manifest so the run
* becomes fully recorded and reversible - exactly as if it had never stopped.
*/
async function resumeIncompleteRuns() {
let incomplete = [];
try {
incomplete = await persistence.listIncompleteRuns();
} catch {
return;
}
for (const runId of incomplete) {
const plan = await persistence.loadPlan(runId);
if (!plan || !Array.isArray(plan.ops)) continue;
try {
const journal = await persistence.loadJournal(runId);
const done = new Set([...journal.entries()].filter(([, s]) => s === 'done').map(([i]) => i));
console.log(`Resuming interrupted move ${runId}: ${done.size}/${plan.ops.length} op(s) already applied.`);
const job = jobsMap.get(plan.jobId);
if (job) { job.status = 'moving'; job.runId = runId; job.progress = { phase: 'moving', moved: done.size, total: plan.ops.length }; logJob(job, 'Resuming interrupted move after restart…', 'warn'); }
const report = await executePlan(plan, {
journalCompleted: done,
appendJournal: (entry) => persistence.appendJournalEntry(runId, entry),
onProgress: (processed, total) => {
if (job) { job.progress.moved = processed; job.progress.total = total; }
},
});
const pseudoJob = { id: plan.jobId, sources: plan.sources || [], destination: plan.destination };
let treeAfterStats = null;
await persistence.saveManifest(runId, { job: pseudoJob, report, treeBeforeStats: null, treeAfterStats: null });
try {
treeAfterStats = await snapshotTreesToFile([plan.destination], persistence.treeAfterPath(runId));
await persistence.saveManifest(runId, { job: pseudoJob, report, treeBeforeStats: null, treeAfterStats });
} catch { /* inspection-only */ }
if (job) {
job.report = report;
job.treeAfterStats = treeAfterStats;
job.status = 'completed';
job.progress.phase = 'done';
logJob(job, `Resumed move finished. Moved ${report.moved.length}, errors ${report.errors.length}.`);
await persistence.deleteJobSnapshot(job.id);
}
console.log(`Resumed move ${runId} completed: moved ${report.moved.length}, errors ${report.errors.length}.`);
} catch (err) {
console.error(`Failed to resume move ${runId}:`, err.message);
}
}
}
async function bootstrap() {
const savedJobs = await persistence.loadAllJobSnapshots();
for (const job of savedJobs) {
jobsMap.set(job.id, job);
}
for (const job of savedJobs) {
if (job.status === 'scanning' || job.status === 'pending') {
// A scan is read-only, so re-running it from scratch is always safe.
logJob(job, 'Resuming interrupted scan after restart.', 'warn');
job.status = 'scanning';
runScan(job).then(() => persistence.saveJobSnapshot(job)).catch(() => {});
} else if (job.status === 'moving') {
// If no plan hit disk, nothing was moved yet - make it re-confirmable.
if (!job.runId || !(await persistence.planExists(job.runId))) {
job.status = 'done';
logJob(job, 'Interrupted before any file moved - ready to confirm again.', 'warn');
persistence.saveJobSnapshot(job);
}
// Otherwise resumeIncompleteRuns() below finishes it.
}
}
if (savedJobs.length > 0) {
console.log(`Loaded ${savedJobs.length} in-progress job(s) from the last session.`);
}
const PORT = process.env.PORT || 4173;
app.listen(PORT, () => {
console.log(`Folder Organizer running at http://localhost:${PORT}`);
resumeIncompleteRuns().catch((err) => console.error('Resume sweep failed:', err.message));
});
}
bootstrap();