From 2525ad039f1fc358223a8dd558f62a1dd68a533e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Wed, 17 Jun 2026 23:00:04 +0400 Subject: [PATCH] =?UTF-8?q?=D1=87=D0=B0=D0=BD=D0=BA=D0=B8=20=D0=B2=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BC=D1=8F=D1=82=D0=B8=20(=D0=BD=D0=B5=20=D0=91?= =?UTF-8?q?=D0=94),=2030KB=20+=2060s=20timeout,=20v1.43?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/app.py | 83 ++++++++++---------------------------- site/templates/upload.html | 4 +- 2 files changed, 24 insertions(+), 63 deletions(-) diff --git a/site/app.py b/site/app.py index a488a28..8ce80f4 100644 --- a/site/app.py +++ b/site/app.py @@ -20,9 +20,12 @@ import redis_worker load_dotenv() import time as _time +import re as _re # Логи в памяти (быстро, не тормозит ответ) _log_memory = [] +# Хранилище чанков в памяти +_chunks_mem = {} def _log(step, detail=""): """Писать лог в память (мгновенно).""" @@ -190,10 +193,10 @@ class ContractsApp: _log("upload_outer_error", str(e)) return jsonify({"error": f"Крах: {e}"}), 500 - # ── Чанковая загрузка base64 (10KB) ─────────────────────── + # ── Чанковая загрузка base64 (30KB) ─────────────────────── def _chunk_json(self): - """Принять чанк base64. На последнем — собрать и сохранить.""" + """Принять чанк base64 (в памяти). На последнем — в Redis.""" data = request.get_json(silent=True) or {} upload_id = data.get("upload_id", "") filename = data.get("filename", "") @@ -205,68 +208,26 @@ class ContractsApp: if not upload_id or not b64_piece: return jsonify({"error": "Нет upload_id или data"}), 400 - # Храним в той же таблице chunks (chunk_data = base64 кусок как текст) - db.execute( - "INSERT INTO chunks (upload_id, chunk_index, chunk_data, filename, mime, total_chunks, cid) " - "VALUES (%s,%s,%s,%s,%s,%s,%s) ON CONFLICT (upload_id, chunk_index) DO NOTHING", - (upload_id, chunk_index, b64_piece.encode(), filename, "", total_chunks, cid), - ) + if upload_id not in _chunks_mem: + _chunks_mem[upload_id] = {"chunks": [None]*total_chunks, "filename": filename, "cid": cid, "received": 0} - count_res, _ = db.query("SELECT COUNT(*) FROM chunks WHERE upload_id=%s", (upload_id,)) - received = count_res["rows"][0][0] if count_res else 0 + store = _chunks_mem[upload_id] + if store["chunks"][chunk_index] is None: + store["chunks"][chunk_index] = b64_piece + store["received"] += 1 - if received == total_chunks: - try: - rows_res, _ = db.query( - "SELECT chunk_data FROM chunks WHERE upload_id=%s ORDER BY chunk_index", - (upload_id,), - ) - full_b64 = b"".join(r[0] for r in rows_res["rows"]).decode() + if store["received"] == total_chunks: + full_b64 = "".join(store["chunks"]) + fname = store["filename"] + fcid = store["cid"] + mime = mimeutil.guess_mime(fname) or "application/octet-stream" + import threading + task = {"filename": fname, "b64": full_b64, "mime": mime, "cid": fcid or ""} + threading.Thread(target=redis_client.push, args=(task,), daemon=True).start() + del _chunks_mem[upload_id] + return jsonify({"contract_id": fcid or "pending"}) - # Получить метаданные - meta, _ = db.query_one("SELECT filename, cid FROM chunks WHERE upload_id=%s LIMIT 1", (upload_id,)) - fname = meta["filename"] if meta else filename - fcid = meta["cid"] if meta else cid - - conn, err = db.connect() - if err: - return jsonify({"error": f"БД: {err}"}), 500 - try: - cur = conn.cursor() - if fcid: - contract_id = fcid - else: - cur.execute( - "INSERT INTO contracts (number, client) VALUES (%s, %s) RETURNING id", - ("б/н " + __import__("datetime").datetime.now().strftime("%Y%m%d-%H%M"), ""), - ) - contract_id = cur.fetchone()[0] - - mime = mimeutil.guess_mime(fname) or "application/octet-stream" - cur.execute( - "INSERT INTO documents (filename, mime_type, original_b64, status) VALUES (%s,%s,%s,'uploaded')", - (fname, mime, full_b64), - ) - cur.execute("SELECT id FROM documents WHERE filename=%s AND status='uploaded' ORDER BY created_at DESC LIMIT 1", (fname,)) - row = cur.fetchone() - doc_id = row[0] if row else None - if doc_id: - cur.execute( - "INSERT INTO supplements (contract_id, document_id, type) VALUES (%s,%s,'initial')", - (str(contract_id), str(doc_id)), - ) - conn.commit() - cur.close() - finally: - db.put_conn(conn) - except Exception as e: - return jsonify({"error": f"Сборка: {e}"}), 500 - finally: - db.execute("DELETE FROM chunks WHERE upload_id=%s", (upload_id,)) - - return jsonify({"contract_id": str(contract_id)}) - - return jsonify({"chunk": chunk_index, "received": received, "total": total_chunks}) + return jsonify({"chunk": chunk_index, "received": store["received"], "total": total_chunks}) # ── Старая чанковая загрузка ─────────────────────────────── diff --git a/site/templates/upload.html b/site/templates/upload.html index 131621d..556a34c 100644 --- a/site/templates/upload.html +++ b/site/templates/upload.html @@ -59,7 +59,7 @@
Nubes - Сверка договоров v1.42 + Сверка договоров v1.43
@@ -173,7 +173,7 @@ var xhr = new XMLHttpRequest(); xhr.open('POST', '/chunk_json'); xhr.setRequestHeader('Content-Type', 'application/json'); - xhr.timeout = 15000; + xhr.timeout = 60000; xhr.onload = function() { if (xhr.status === 200) { try {