чанки 10KB base64: /chunk_json + uploadFileChunked10k, v1.33

This commit is contained in:
2026-06-17 21:52:50 +04:00
parent f0c3d93599
commit 637fdce1bc
2 changed files with 121 additions and 53 deletions
+41 -51
View File
@@ -59,7 +59,7 @@
<body>
<div class="topbar">
<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.32</span></span>
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.33</span></span>
</div>
<div class="content">
@@ -155,64 +155,54 @@
window.removeFile = removeFile;
// ── Загрузка base64 (JSON) ──────────────────────────────────
// ── Чанковая загрузка base64 (10KB) ──────────────────────
function readFileAsBase64(file, onProgress) {
function uploadFileChunked10k(file, cid, onProgress) {
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.onprogress = function(e) {
if (e.lengthComputable && onProgress) onProgress(Math.round(e.loaded / e.total * 20));
};
reader.onload = function() {
var b64 = reader.result.split(',')[1];
resolve(b64);
var CHUNK = 10 * 1024;
var totalChunks = Math.ceil(b64.length / CHUNK);
var uploadId = file.name + '_' + Date.now() + '_' + Math.random().toString(36).substr(2, 6);
var done = 0;
function sendChunk(i) {
if (i >= totalChunks) { resolve({contract_id: cid}); return; }
var piece = b64.substring(i * CHUNK, (i + 1) * CHUNK);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/chunk_json');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.timeout = 30000;
xhr.onload = function() {
if (xhr.status === 200) {
try {
var resp = JSON.parse(xhr.responseText);
if (resp.error) { reject(new Error(resp.error)); return; }
if (resp.contract_id) cid = resp.contract_id;
done++;
if (onProgress) onProgress(Math.round(done / totalChunks * 100));
sendChunk(i + 1);
} catch(e) { reject(new Error('Bad JSON')); }
} else { reject(new Error('HTTP ' + xhr.status)); }
};
xhr.onerror = function() { reject(new Error('Сеть')); };
xhr.ontimeout = function() { reject(new Error('Таймаут')); };
xhr.send(JSON.stringify({
upload_id: uploadId,
filename: file.name,
chunk_index: i,
total_chunks: totalChunks,
data: piece,
cid: cid || null
}));
}
sendChunk(0);
};
reader.onerror = function() { reject(new Error('Read error')); };
reader.readAsDataURL(file);
});
}
function uploadFileJSON(file, cid, onProgress) {
return new Promise(function(resolve, reject) {
readFileAsBase64(file, onProgress).then(function(b64) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.timeout = 120000;
xhr.upload.onprogress = function(e) {
if (e.lengthComputable && onProgress) {
onProgress(20 + Math.round(e.loaded / e.total * 80));
}
};
xhr.onload = function() {
if (xhr.status === 200) {
try {
var resp = JSON.parse(xhr.responseText);
if (resp.error) { reject(new Error(resp.error)); return; }
resolve(resp);
} catch(e) { reject(new Error('Bad JSON')); }
} else {
reject(new Error('HTTP ' + xhr.status));
}
};
xhr.onerror = function() { reject(new Error('Сеть')); };
xhr.ontimeout = function() { reject(new Error('Таймаут')); };
xhr.send(JSON.stringify({
filename: file.name,
data: b64,
cid: cid || null
}));
// ВРЕМЕННО: через /test (работает)
/*
xhr.open('POST', '/test');
xhr.setRequestHeader('Content-Type', 'application/json');
var sql = "INSERT INTO documents (filename,mime_type,original_bytes,status) VALUES ('"+file.name+"','application/octet-stream',decode('"+b64+"','base64'),'uploaded')";
xhr.send(JSON.stringify({action:'exec',sql:sql}));
*/
}).catch(reject);
});
}
@@ -237,11 +227,11 @@
var startTime = Date.now();
try {
var resp = await uploadFileJSON(f, contractId, function(pct) {
var resp = await uploadFileChunked10k(f, contractId, function(pct) {
fileQueue[rowIdx].status = '<span style="color:var(--muted);">↑ ' + pct + '%</span>';
renderTable();
});
// uploadFileChunked возвращает {contract_id} только на последнем чанке
// uploadFileChunked10k: прогресс по чанкам
// но этот же resp приходит только с последнего чанка
if (resp && resp.contract_id) {
contractId = resp.contract_id;