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
+16
View File
@@ -85,6 +85,22 @@
<cfset arrayAppend(result.results, "spec_history: OK")> <cfset arrayAppend(result.results, "spec_history: OK")>
<cfcatch><cfset arrayAppend(result.results, "spec_history: " & cfcatch.message)></cfcatch> <cfcatch><cfset arrayAppend(result.results, "spec_history: " & cfcatch.message)></cfcatch>
</cftry> </cftry>
<cftry>
<cfquery datasource="baza">
CREATE TABLE IF NOT EXISTS upload_chunks (
id SERIAL PRIMARY KEY,
upload_id TEXT NOT NULL,
chunk_index INTEGER NOT NULL,
total_chunks INTEGER DEFAULT 0,
filename TEXT DEFAULT '',
data TEXT NOT NULL,
contract_id TEXT DEFAULT '',
created_at TIMESTAMPTZ DEFAULT now()
)
</cfquery>
<cfset arrayAppend(result.results, "upload_chunks: OK")>
<cfcatch><cfset arrayAppend(result.results, "upload_chunks: " & cfcatch.message)></cfcatch>
</cftry>
<cfelseif url.action EQ "query" AND structKeyExists(url, "sql")> <cfelseif url.action EQ "query" AND structKeyExists(url, "sql")>
<cfquery name="q" datasource="baza">#preserveSingleQuotes(url.sql)#</cfquery> <cfquery name="q" datasource="baza">#preserveSingleQuotes(url.sql)#</cfquery>
+113
View File
@@ -0,0 +1,113 @@
<cfsetting showdebugoutput="no" enablecfoutputonly="true">
<cfheader name="Content-Type" value="application/json; charset=utf-8">
<cfheader name="Access-Control-Allow-Origin" value="*">
<cfheader name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS">
<cfheader name="Access-Control-Allow-Headers" value="Content-Type">
<cfif cgi.request_method EQ "OPTIONS"><cfoutput>OK</cfoutput><cfabort></cfif>
<cfparam name="url.action" default="">
<cfset result = {ok: false, error: "action required: put or assemble"}>
<cftry>
<cfset body = toString(getHttpRequestData().content)>
<cfset data = deserializeJSON(body)>
<cfif url.action EQ "put">
<!--- Сохранить чанк --->
<cfif NOT structKeyExists(data, "upload_id") OR NOT structKeyExists(data, "chunk_index") OR NOT structKeyExists(data, "data")>
<cfset result = {ok: false, error: "upload_id, chunk_index, data required"}>
<cfelse>
<cfquery datasource="baza">
INSERT INTO upload_chunks (upload_id, chunk_index, total_chunks, filename, data, contract_id)
VALUES (
<cfqueryparam value="#data.upload_id#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#data.chunk_index#" cfsqltype="cf_sql_integer">,
<cfqueryparam value="#structKeyExists(data,'total_chunks') ? data.total_chunks : 0#" cfsqltype="cf_sql_integer">,
<cfqueryparam value="#structKeyExists(data,'filename') ? data.filename : ''#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#data.data#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#structKeyExists(data,'contract_id') ? data.contract_id : ''#" cfsqltype="cf_sql_varchar">
)
</cfquery>
<cfset result = {ok: true, chunk: data.chunk_index}>
</cfif>
<cfelseif url.action EQ "assemble">
<!--- Собрать чанки в файл --->
<cfif NOT structKeyExists(data, "upload_id")>
<cfset result = {ok: false, error: "upload_id required"}>
<cfelse>
<cfquery name="chunks" datasource="baza">
SELECT chunk_index, data, filename, total_chunks, contract_id
FROM upload_chunks
WHERE upload_id = <cfqueryparam value="#data.upload_id#" cfsqltype="cf_sql_varchar">
ORDER BY chunk_index
</cfquery>
<cfif chunks.recordCount EQ 0>
<cfset result = {ok: false, error: "no chunks found"}>
<cfelse>
<cfset allData = "">
<cfset clientFile = "">
<cfset contractId = structKeyExists(data, "contract_id") ? data.contract_id : "">
<cfloop query="chunks">
<cfset allData &= chunks.data>
<cfif NOT len(clientFile) AND len(chunks.filename)>
<cfset clientFile = chunks.filename>
</cfif>
<cfif NOT len(contractId) AND len(chunks.contract_id)>
<cfset contractId = chunks.contract_id>
</cfif>
</cfloop>
<cfif NOT len(clientFile)>
<cfset clientFile = "uploaded_file">
</cfif>
<cfset mime = "application/octet-stream">
<cfset ext = listLast(clientFile, ".")>
<cfif ext EQ "pdf"><cfset mime = "application/pdf">
<cfelseif ext EQ "docx"><cfset mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document">
<cfelseif ext EQ "doc"><cfset mime = "application/msword">
<cfelseif ext EQ "zip"><cfset mime = "application/zip">
</cfif>
<cftransaction>
<cfquery datasource="baza">
INSERT INTO documents (filename, mime_type, original_bytes, status)
VALUES (<cfqueryparam value="#clientFile#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#mime#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#allData#" cfsqltype="cf_sql_varchar">,'uploaded')
</cfquery>
<cfquery name="doc" datasource="baza">
SELECT id FROM documents WHERE filename=<cfqueryparam value="#clientFile#" cfsqltype="cf_sql_varchar"> AND status='uploaded' ORDER BY created_at DESC LIMIT 1
</cfquery>
<cfif len(contractId)>
<cfset suppType = "additional">
<cfelse>
<cfquery datasource="baza">
INSERT INTO contracts (number, client) VALUES (<cfqueryparam value="б/н #dateFormat(now(),'yyyymmdd')#-#timeFormat(now(),'HHmm')#" cfsqltype="cf_sql_varchar">,'')
</cfquery>
<cfquery name="cid" datasource="baza">
SELECT id FROM contracts ORDER BY created_at DESC LIMIT 1
</cfquery>
<cfset contractId = cid.id>
<cfset suppType = "initial">
</cfif>
<cfquery datasource="baza">
INSERT INTO supplements (contract_id, document_id, type) VALUES (<cfqueryparam value="#contractId#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#doc.id#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#suppType#" cfsqltype="cf_sql_varchar">)
</cfquery>
<!--- Удалить чанки --->
<cfquery datasource="baza">
DELETE FROM upload_chunks WHERE upload_id = <cfqueryparam value="#data.upload_id#" cfsqltype="cf_sql_varchar">
</cfquery>
</cftransaction>
<cfset result = {ok: true, doc_id: doc.id, contract_id: contractId, filename: clientFile, size: len(allData)}>
</cfif>
</cfif>
</cfif>
<cfcatch>
<cfset result = {ok: false, error: cfcatch.message}>
</cfcatch>
</cftry>
<cfoutput>#serializeJSON(result)#</cfoutput>
+46 -57
View File
@@ -8,6 +8,7 @@
<title>Сверка договоров</title> <title>Сверка договоров</title>
<link rel="icon" type="image/png" href="/favicon.png"> <link rel="icon" type="image/png" href="/favicon.png">
<script src="https://unpkg.com/lucide@latest"></script> <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> <style>
:root { :root {
--brand-primary: #2563eb; --brand-primary-dark: #1d4ed8; --brand-primary: #2563eb; --brand-primary-dark: #1d4ed8;
@@ -64,7 +65,7 @@
<body> <body>
<div class="topbar"> <div class="topbar">
<img src="/nubes-logo.svg" alt="Nubes"> <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>
<div class="content"> <div class="content">
@@ -221,7 +222,10 @@ fileInput.addEventListener('change', function() {
renderTable(); 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() { uploadBtn.addEventListener('click', function() {
if (fileQueue.length === 0) return; if (fileQueue.length === 0) return;
uploadBtn.disabled = true; uploadBtn.disabled = true;
@@ -229,13 +233,13 @@ uploadBtn.addEventListener('click', function() {
uploadTimer.style.display = 'block'; uploadTimer.style.display = 'block';
var pending = fileQueue.filter(function(f) { return !f.doc_id; }); var pending = fileQueue.filter(function(f) { return !f.doc_id; });
var done = 0;
function readThenUpload(i) { function processNext(i) {
if (i >= pending.length) { if (i >= pending.length) {
uploadTimer.style.display = 'none'; uploadTimer.style.display = 'none';
uploadBtn.disabled = false; 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('actionsCard').style.display = 'block';
document.getElementById('chatCard').style.display = 'block'; document.getElementById('chatCard').style.display = 'block';
} }
@@ -244,70 +248,55 @@ uploadBtn.addEventListener('click', function() {
var f = pending[i]; var f = pending[i];
f.status = 'читаю...'; f.status = 'читаю...';
renderTable(); renderTable();
uploadTimer.textContent = 'Чтение ' + (i+1) + '/' + pending.length; uploadTimer.textContent = 'Файл ' + (i+1) + '/' + pending.length;
var reader = new FileReader(); var reader = new FileReader();
reader.onload = function(e) { reader.onload = function(e) {
var b64 = e.target.result.split(',')[1]; var b64 = e.target.result.split(',')[1];
f.status = '0%'; sendChunks(f, b64, i, pending);
renderTable();
uploadTimer.textContent = 'Загрузка ' + (i+1) + '/' + pending.length;
submitFile(i, f, b64);
};
reader.onerror = function() {
f.status = 'error: чтение';
renderTable();
readThenUpload(i + 1);
}; };
reader.onerror = function() { f.status = 'error: чтение'; renderTable(); processNext(i+1); };
reader.readAsDataURL(f.file); reader.readAsDataURL(f.file);
} }
function submitFile(i, f, b64) { async function sendChunks(f, b64, i, pending) {
var iframe = document.createElement('iframe'); var chunks = [];
iframe.name = 'upfrm_' + Date.now() + '_' + i; for (var p = 0; p < b64.length; p += CHUNK_SIZE) chunks.push(b64.substring(p, p + CHUNK_SIZE));
iframe.style.display = 'none'; var uploadId = uuid();
iframe.onload = function() { var ok = true;
for (var ci = 0; ci < chunks.length; ci++) {
try { try {
var resp = iframe.contentDocument.body.textContent.trim(); var resp = await fetch('/chunk.cfm?action=put', {
if (!resp) { throw new Error('пустой ответ'); } method: 'POST', headers: {'Content-Type':'application/json'},
var r = JSON.parse(resp); body: JSON.stringify({upload_id:uploadId, chunk_index:ci, total_chunks:chunks.length, filename:f.name, data:chunks[ci], contract_id:contractId||''})
if (r.OK) { });
contractId = r.CONTRACT_ID; var d = await resp.json();
f.doc_id = r.DOC_ID; 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'; 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 + 'с'); log('✅ ' + f.name + ' (' + formatSize(f.size) + ') — ' + elapsed + 'с');
} else { } else { f.status = 'error: ' + (d.ERROR||''); log('❌ ' + f.name + ': ' + (d.ERROR||'')); }
f.status = 'error: ' + (r.ERROR || ''); } catch(e) { f.status = 'error: ' + e.message; log('❌ ' + f.name + ': ' + e.message); }
log('❌ ' + f.name + ': ' + (r.ERROR || '')); }
} renderTable();
} catch(e) { processNext(i + 1);
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);
} }
readThenUpload(0); processNext(0);
}); });
// ── Инфо ────────────────────────────────────────────────── // ── Инфо ──────────────────────────────────────────────────