fix: Promise резолвился на прогрессе, contractId не ставился, v1.14

This commit is contained in:
2026-06-17 12:47:59 +04:00
parent 31fec1ca84
commit 1714abf766
+21 -43
View File
@@ -59,7 +59,7 @@
<body> <body>
<div class="topbar"> <div class="topbar">
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes"> <img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.13</span></span> <span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.14</span></span>
</div> </div>
<div class="content"> <div class="content">
@@ -155,9 +155,9 @@
window.removeFile = removeFile; window.removeFile = removeFile;
// ── Загрузка файла (XHR с прогрессом) ────────────────────── // ── Загрузка файла (XHR, возвращает Promise только при успехе/ошибке) ──
function uploadFile(file, cid) { function uploadFileXHR(file, cid, onProgress) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
var url = '/' + (cid ? '?cid=' + cid : ''); var url = '/' + (cid ? '?cid=' + cid : '');
@@ -165,9 +165,8 @@
xhr.timeout = 120000; xhr.timeout = 120000;
xhr.upload.onprogress = function(e) { xhr.upload.onprogress = function(e) {
if (e.lengthComputable) { if (e.lengthComputable && onProgress) {
var pct = Math.round(e.loaded / e.total * 100); onProgress(Math.round(e.loaded / e.total * 100));
resolve({_progress: true, pct: pct});
} }
}; };
@@ -203,56 +202,35 @@
var f = newFiles[i]; var f = newFiles[i];
if (fileQueue.find(function(q) { return q.name === f.name && q.size === f.size && !q.isZipChild; })) continue; if (fileQueue.find(function(q) { return q.name === f.name && q.size === f.size && !q.isZipChild; })) continue;
// Добавить строку с прогрессом
var entry = { name: f.name, lastModified: f.lastModified, size: f.size, status: '<span style="color:var(--muted);">↑ 0%</span>' }; var entry = { name: f.name, lastModified: f.lastModified, size: f.size, status: '<span style="color:var(--muted);">↑ 0%</span>' };
fileQueue.push(entry); fileQueue.push(entry);
var rowIdx = fileQueue.length - 1; var rowIdx = fileQueue.length - 1;
renderTable(); renderTable();
var row = document.getElementById('row_' + rowIdx);
var startTime = Date.now(); var startTime = Date.now();
// Загружаем try {
var lastPct = 0; var resp = await uploadFileXHR(f, contractId, function(pct) {
var done = false; fileQueue[rowIdx].status = '<span style="color:var(--muted);">↑ ' + pct + '%</span>';
uploadFile(f, contractId).then(
function(res) {
if (done) return;
if (res._progress) {
var pct = res.pct;
if (pct !== lastPct) {
lastPct = pct;
fileQueue[rowIdx].status = '<span style="color:var(--muted);">↑ ' + pct + '%</span>';
renderTable();
}
return; // ждём следующий прогресс или завершение
}
// Завершено
done = true;
contractId = res.contract_id;
var elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
fileQueue[rowIdx].status = '<span class="status-ok">✓ ' + elapsed + 'с</span>';
fileQueue[rowIdx].uploaded = true;
renderTable(); renderTable();
});
contractId = resp.contract_id;
var elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
fileQueue[rowIdx].status = '<span class="status-ok">✓ ' + elapsed + 'с</span>';
fileQueue[rowIdx].uploaded = true;
renderTable();
// ZIP: показать содержимое if (f.type === 'application/zip' || f.name.toLowerCase().endsWith('.zip')) {
if (f.type === 'application/zip' || f.name.toLowerCase().endsWith('.zip')) { await showZipContents(f);
showZipContents(f);
}
},
function(err) {
if (done) return;
done = true;
fileQueue[rowIdx].status = '<span class="status-err">✗ ' + err.message + '</span>';
renderTable();
} }
); } catch(err) {
fileQueue[rowIdx].status = '<span class="status-err">✗ ' + err.message + '</span>';
renderTable();
}
} }
fileInput.value = ''; fileInput.value = '';
fileInput.disabled = false; fileInput.disabled = false;
parseBtn.disabled = false; parseBtn.disabled = (contractId === null);
parseBtn.innerHTML = '<i data-lucide="play" style="width:16px;height:16px;"></i> Парсинг'; parseBtn.innerHTML = '<i data-lucide="play" style="width:16px;height:16px;"></i> Парсинг';
lucide.createIcons(); lucide.createIcons();
}); });