Files
core/ui/static/ingest.js
T

80 lines
2.7 KiB
JavaScript
Raw Normal View History

2026-05-07 13:24:30 -04:00
async function uploadScan() {
const fileInput = document.getElementById('scanFile');
const adapterSelect = document.getElementById('adapterSelect');
const resultDiv = document.getElementById('ingestResult');
2026-04-02 10:57:36 -04:00
2026-05-07 13:24:30 -04:00
const file = fileInput.files[0];
const adapterName = adapterSelect.value;
2026-04-02 10:57:36 -04:00
2026-05-07 13:24:30 -04:00
if (!file) {
showResult("Please select a file to upload.", false);
return;
}
2026-04-02 10:57:36 -04:00
2026-05-07 13:24:30 -04:00
if (!adapterName) {
showResult("Please select an adapter.", false);
return;
2026-04-02 10:57:36 -04:00
}
2026-05-07 13:24:30 -04:00
// Show processing state
showResult("Processing...", true, true);
2026-04-02 10:57:36 -04:00
try {
let response;
2026-05-07 13:24:30 -04:00
// Route appropriately based on file extension
2026-04-02 10:57:36 -04:00
if (file.name.toLowerCase().endsWith('.json')) {
const rawText = await file.text();
response = await fetch(`/api/ingest/${encodeURIComponent(adapterName)}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: rawText
});
2026-05-07 13:24:30 -04:00
} else {
2026-04-02 10:57:36 -04:00
let formData = new FormData();
formData.append('file', file);
2026-05-07 13:24:30 -04:00
formData.append('adapter_name', adapterName); // Pass adapter name for CSVs
2026-04-02 10:57:36 -04:00
response = await fetch('/api/ingest/csv', { method: 'POST', body: formData });
}
if (!response.ok) {
2026-05-07 13:24:30 -04:00
// Auto-redirect to builder if the adapter doesn't match the file structure
2026-04-02 10:57:36 -04:00
if (response.status === 404) {
2026-05-07 13:24:30 -04:00
showResult("Format not recognized. Redirecting to Adapter Builder...", false);
2026-04-02 10:57:36 -04:00
setTimeout(() => {
window.location.href = `/admin/adapters/new?filename=${encodeURIComponent(file.name)}`;
}, 1200);
} else {
const errText = await response.text();
throw new Error(errText);
}
} else {
2026-05-07 13:24:30 -04:00
showResult("Yeehaw! Tickets corralled successfully.", true);
setTimeout(() => window.location.href = "/dashboard", 1000);
2026-04-02 10:57:36 -04:00
}
} catch (err) {
2026-05-07 13:24:30 -04:00
showResult("Stampede! Error: " + err.message, false);
2026-04-02 10:57:36 -04:00
}
}
2026-05-07 13:24:30 -04:00
// Helper function to handle status messages nicely
function showResult(msg, isSuccess, isInfo = false) {
const div = document.getElementById('ingestResult');
div.style.display = 'block';
div.innerText = msg;
if (isInfo) {
div.style.backgroundColor = '#e0f2fe';
div.style.color = '#0369a1';
div.style.border = '1px solid #bae6fd';
} else if (isSuccess) {
div.style.backgroundColor = '#dcfce7';
div.style.color = '#166534';
div.style.border = '1px solid #bbf7d0';
} else {
div.style.backgroundColor = '#fee2e2';
div.style.color = '#991b1b';
div.style.border = '1px solid #fecaca';
}
}