v1.0.36: чанки 8KB — обход H2 бага

This commit is contained in:
2026-06-18 21:41:29 +04:00
parent b2b148c1b7
commit a23ea0f049
3 changed files with 175 additions and 57 deletions
+46 -57
View File
@@ -8,6 +8,7 @@
<title>Сверка договоров</title>
<link rel="icon" type="image/png" href="/favicon.png">
<script src="https://unpkg.com/lucide@latest"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<style>
:root {
--brand-primary: #2563eb; --brand-primary-dark: #1d4ed8;
@@ -64,7 +65,7 @@
<body>
<div class="topbar">
<img src="/nubes-logo.svg" alt="Nubes">
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.0.35 — Lucee</span></span>
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.0.36 — Lucee</span></span>
</div>
<div class="content">
@@ -221,7 +222,10 @@ fileInput.addEventListener('change', function() {
renderTable();
});
// ── Загрузка через iframe (base64 в скрытом поле) ────────
// ── Загрузка чанками (8KB) ────────────────────────────────
var CHUNK_SIZE = 8000;
function uuid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(c){var r=Math.random()*16|0,v=c=='x'?r:(r&3|8);return v.toString(16);}); }
uploadBtn.addEventListener('click', function() {
if (fileQueue.length === 0) return;
uploadBtn.disabled = true;
@@ -229,13 +233,13 @@ uploadBtn.addEventListener('click', function() {
uploadTimer.style.display = 'block';
var pending = fileQueue.filter(function(f) { return !f.doc_id; });
var done = 0;
function readThenUpload(i) {
function processNext(i) {
if (i >= pending.length) {
uploadTimer.style.display = 'none';
uploadBtn.disabled = false;
if (contractId && done === pending.length) {
var hasErrors = fileQueue.some(function(f) { return f.status && f.status.indexOf('error') === 0; });
if (contractId && !hasErrors) {
document.getElementById('actionsCard').style.display = 'block';
document.getElementById('chatCard').style.display = 'block';
}
@@ -244,70 +248,55 @@ uploadBtn.addEventListener('click', function() {
var f = pending[i];
f.status = 'читаю...';
renderTable();
uploadTimer.textContent = 'Чтение ' + (i+1) + '/' + pending.length;
uploadTimer.textContent = 'Файл ' + (i+1) + '/' + pending.length;
var reader = new FileReader();
reader.onload = function(e) {
var b64 = e.target.result.split(',')[1];
f.status = '0%';
renderTable();
uploadTimer.textContent = 'Загрузка ' + (i+1) + '/' + pending.length;
submitFile(i, f, b64);
};
reader.onerror = function() {
f.status = 'error: чтение';
renderTable();
readThenUpload(i + 1);
sendChunks(f, b64, i, pending);
};
reader.onerror = function() { f.status = 'error: чтение'; renderTable(); processNext(i+1); };
reader.readAsDataURL(f.file);
}
function submitFile(i, f, b64) {
var iframe = document.createElement('iframe');
iframe.name = 'upfrm_' + Date.now() + '_' + i;
iframe.style.display = 'none';
iframe.onload = function() {
async function sendChunks(f, b64, i, pending) {
var chunks = [];
for (var p = 0; p < b64.length; p += CHUNK_SIZE) chunks.push(b64.substring(p, p + CHUNK_SIZE));
var uploadId = uuid();
var ok = true;
for (var ci = 0; ci < chunks.length; ci++) {
try {
var resp = iframe.contentDocument.body.textContent.trim();
if (!resp) { throw new Error('пустой ответ'); }
var r = JSON.parse(resp);
if (r.OK) {
contractId = r.CONTRACT_ID;
f.doc_id = r.DOC_ID;
var resp = await fetch('/chunk.cfm?action=put', {
method: 'POST', headers: {'Content-Type':'application/json'},
body: JSON.stringify({upload_id:uploadId, chunk_index:ci, total_chunks:chunks.length, filename:f.name, data:chunks[ci], contract_id:contractId||''})
});
var d = await resp.json();
if (!d.OK) { ok = false; log('❌ ' + f.name + ' чанк ' + ci + ': ' + d.ERROR); break; }
f.status = Math.round((ci+1)/chunks.length*100) + '%';
renderTable();
uploadTimer.textContent = f.name + ' ' + f.status;
} catch(e) { ok = false; f.status = 'error: ' + e.message; renderTable(); break; }
}
if (ok) {
try {
var resp = await fetch('/chunk.cfm?action=assemble', {
method: 'POST', headers: {'Content-Type':'application/json'},
body: JSON.stringify({upload_id:uploadId})
});
var d = await resp.json();
if (d.OK) {
contractId = d.CONTRACT_ID || contractId;
f.doc_id = d.DOC_ID;
f.status = 'uploaded';
done++;
var elapsed = ((Date.now() - uploadStartTime) / 1000).toFixed(1);
var elapsed = ((Date.now()-uploadStartTime)/1000).toFixed(1);
log('✅ ' + f.name + ' (' + formatSize(f.size) + ') — ' + elapsed + 'с');
} else {
f.status = 'error: ' + (r.ERROR || '');
log('❌ ' + f.name + ': ' + (r.ERROR || ''));
}
} catch(e) {
f.status = 'error: ' + e.message;
log('❌ ' + f.name + ': ' + e.message);
}
renderTable();
document.body.removeChild(iframe);
readThenUpload(i + 1);
};
document.body.appendChild(iframe);
// Форма с скрытыми полями, target = iframe
var form = document.createElement('form');
form.method = 'POST';
form.action = '/upload.cfm';
form.target = iframe.name;
form.style.display = 'none';
form.innerHTML =
'<input type="hidden" name="filename" value="' + f.name.replace(/"/g,'&quot;') + '">' +
'<input type="hidden" name="data" value="' + b64.replace(/"/g,'&quot;') + '">' +
(contractId ? '<input type="hidden" name="contract_id" value="' + contractId + '">' : '');
document.body.appendChild(form);
form.submit();
setTimeout(function() { document.body.removeChild(form); }, 100);
} else { f.status = 'error: ' + (d.ERROR||''); log('❌ ' + f.name + ': ' + (d.ERROR||'')); }
} catch(e) { f.status = 'error: ' + e.message; log('❌ ' + f.name + ': ' + e.message); }
}
renderTable();
processNext(i + 1);
}
readThenUpload(0);
processNext(0);
});
// ── Инфо ──────────────────────────────────────────────────