feat(dashcam): async import with polling status

- Import now returns immediately with {status: 'started'}
- Backend runs copy in a daemon thread, tracks job state in _import_job
- New GET /api/dash/import/status endpoint for polling
- UI polls every 3s, shows progress bar until done
- Page reload mid-import resumes polling automatically
- Prevents double-import with 'busy' guard

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
Mathieu Moran
2026-08-10 01:12:00 -04:00
co-authored by Copilot
parent 7d124b0fa0
commit 71f8ce2993
3 changed files with 115 additions and 63 deletions
+69 -48
View File
@@ -17,10 +17,12 @@ const filesRefreshBtn = document.getElementById('dash-files-refresh-btn');
const selectAllBtn = document.getElementById('dash-select-all-btn');
const importSelectedBtn = document.getElementById('dash-import-selected-btn');
const deleteAllBtn = document.getElementById('dash-delete-all-btn');
let importInProgress = false;
let importStartTime = null;
let importTotal = 0;
let importEtaInterval = null;
let importPollInterval = null;
function showImportProgress(msg, total) {
importTotal = total || 0;
@@ -42,6 +44,8 @@ function showImportProgress(msg, total) {
function showImportResult(msg) {
clearInterval(importEtaInterval);
clearInterval(importPollInterval);
importPollInterval = null;
importStatusBar.style.display = 'none';
importResult.textContent = msg;
importResultBar.style.display = 'block';
@@ -134,6 +138,7 @@ function renderDashcamFiles(summary) {
actionsTd.className = 'dash-actions-td';
const actionsDiv = document.createElement('div');
actionsDiv.className = 'data-list-actions';
const importButton = document.createElement('button');
importButton.type = 'button';
importButton.className = 'btn btn-sm btn-outline-primary btn-icon-mobile';
@@ -190,34 +195,62 @@ function deleteDashcamFile(path) {
});
}
function describeImportResult(summary) {
let text = `Selected ${summary.found} video(s), copied ${summary.copied}, skipped ${summary.skipped} (already imported).`;
if (summary.errors && summary.errors.length) {
text += ` Errors: ${summary.errors.join('; ')}`;
function describeImportResult(job) {
let text = `Copied ${job.copied}, skipped ${job.skipped} (already imported).`;
if (job.errors && job.errors.length) {
text += ` Errors: ${job.errors.join('; ')}`;
}
return text;
}
function importDashcamFiles(paths) {
showImportProgress(`Importing ${paths.length} video(s)…`, paths.length);
function pollImportStatus() {
fetch('/api/dash/import/status')
.then(r => r.json())
.then(job => {
if (!job || job.status === 'none' || job.status === 'running') return;
clearInterval(importPollInterval);
importPollInterval = null;
setImportBusy(false);
if (job.status === 'ok') {
showImportResult(describeImportResult(job));
refreshDashcamFiles();
} else {
showImportResult(job.message || `Import failed: ${job.status}`);
}
})
.catch(() => {}); // ignore transient network errors while polling
}
function startImport(paths) {
const label = paths ? `Importing ${paths.length} video(s)…` : 'Importing all videos… this can take a while.';
showImportProgress(label, paths ? paths.length : 0);
setImportBusy(true);
return fetchJson('/api/dash/import', {
const body = paths ? JSON.stringify({ paths }) : undefined;
fetch('/api/dash/import', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ paths }),
}).then(summary => {
showImportResult(describeImportResult(summary));
paths.forEach(path => {
const checkbox = Array.from(
fileListBody.querySelectorAll('[data-select-path]')
).find(candidate => candidate.dataset.selectPath === path);
if (checkbox) checkbox.checked = false;
headers: body ? { 'Content-Type': 'application/json' } : {},
body,
})
.then(r => r.json())
.then(data => {
if (data.status === 'busy') {
showImportResult(data.message || 'Import already in progress.');
setImportBusy(false);
return;
}
if (data.status !== 'started') {
showImportResult(data.message || `Import error: ${data.status}`);
setImportBusy(false);
return;
}
clearInterval(importPollInterval);
importPollInterval = setInterval(pollImportStatus, 3000);
})
.catch(err => {
showImportResult(`Import request failed: ${err}`);
setImportBusy(false);
});
return summary;
}).catch(err => {
showImportResult(`Selected import failed: ${err.message}`);
return null;
}).finally(() => setImportBusy(false));
}
function describeDevice(device) {
@@ -245,37 +278,13 @@ function scanForDashcam() {
});
}
function importDashcam() {
showImportProgress('Importing all videos… this can take a while.', 0);
setImportBusy(true);
fetch('/api/dash/import', { method: 'POST' })
.then(response => response.json())
.then(summary => {
if (summary.status !== 'ok') {
showImportResult(summary.message || ('Import failed: ' + summary.status));
return;
}
let text = `Found ${summary.found} video(s), copied ${summary.copied}, skipped ${summary.skipped}.`;
if (summary.errors && summary.errors.length > 0) {
text += ` Errors: ${summary.errors.join('; ')}`;
}
showImportResult(text);
refreshDashcamFiles();
})
.catch(err => {
showImportResult('Error while importing: ' + err);
})
.finally(() => setImportBusy(false));
}
scanBtn.addEventListener('click', scanForDashcam);
importBtn.addEventListener('click', importDashcam);
importBtn.addEventListener('click', () => startImport(null));
filesRefreshBtn.addEventListener('click', refreshDashcamFiles);
fileListBody.addEventListener('click', event => {
const importButton = event.target.closest('[data-import-path]');
if (importButton) {
importDashcamFiles([importButton.dataset.importPath]);
startImport([importButton.dataset.importPath]);
return;
}
@@ -295,7 +304,7 @@ fileListBody.addEventListener('change', event => {
importSelectedBtn.addEventListener('click', () => {
const paths = selectedPaths();
if (paths.length) {
importDashcamFiles(paths);
startImport(paths);
}
});
selectAllBtn.addEventListener('click', () => {
@@ -322,6 +331,18 @@ deleteAllBtn.addEventListener('click', () => {
});
});
// Check if an import is already running when page loads (e.g. after refresh mid-import)
fetch('/api/dash/import/status')
.then(r => r.json())
.then(job => {
if (job && job.status === 'running') {
showImportProgress('Import in progress…', 0);
setImportBusy(true);
importPollInterval = setInterval(pollImportStatus, 3000);
}
})
.catch(() => {});
// Auto-scan once when the page loads.
scanForDashcam();
refreshDashcamFiles();