полный лог _upload_json: 12 шагов, v1.25
This commit is contained in:
+63
-45
@@ -134,56 +134,74 @@ class ContractsApp:
|
|||||||
|
|
||||||
def _upload_json(self):
|
def _upload_json(self):
|
||||||
"""Принять файл как base64. Сохранить строкой (без декодирования — быстро)."""
|
"""Принять файл как base64. Сохранить строкой (без декодирования — быстро)."""
|
||||||
data = request.get_json(silent=True)
|
_log("upload_entry", "start")
|
||||||
_log("upload_entry", f"data={'yes' if data else 'no'}")
|
|
||||||
if not data:
|
|
||||||
return jsonify({"error": "Нет JSON"}), 400
|
|
||||||
|
|
||||||
filename = data.get("filename", "")
|
|
||||||
b64 = data.get("data", "")
|
|
||||||
if not filename or not b64:
|
|
||||||
return jsonify({"error": "Нет filename или data"}), 400
|
|
||||||
|
|
||||||
mime = mimeutil.guess_mime(filename) or "application/octet-stream"
|
|
||||||
cid = data.get("cid")
|
|
||||||
|
|
||||||
conn, err = db.connect()
|
|
||||||
if err:
|
|
||||||
return jsonify({"error": f"БД: {err}"}), 500
|
|
||||||
try:
|
try:
|
||||||
cur = conn.cursor()
|
data = request.get_json(silent=True)
|
||||||
if cid:
|
_log("upload_json_parsed", f"data={'yes' if data else 'no'} len={len(request.data) if request.data else 0}")
|
||||||
contract_id = cid
|
if not data:
|
||||||
else:
|
return jsonify({"error": "Нет JSON"}), 400
|
||||||
cur.execute(
|
|
||||||
"INSERT INTO contracts (number, client) VALUES (%s, %s) RETURNING id",
|
filename = data.get("filename", "")
|
||||||
("б/н " + __import__("datetime").datetime.now().strftime("%Y%m%d-%H%M"), ""),
|
b64 = data.get("data", "")
|
||||||
)
|
_log("upload_fields", f"file={filename} b64_len={len(b64) if b64 else 0} cid={data.get('cid')}")
|
||||||
contract_id = cur.fetchone()[0]
|
if not filename or not b64:
|
||||||
|
return jsonify({"error": "Нет filename или data"}), 400
|
||||||
|
|
||||||
|
mime = mimeutil.guess_mime(filename) or "application/octet-stream"
|
||||||
|
cid = data.get("cid")
|
||||||
|
|
||||||
|
conn, err = db.connect()
|
||||||
|
_log("upload_connect", f"err={err}")
|
||||||
|
if err:
|
||||||
|
return jsonify({"error": f"БД: {err}"}), 500
|
||||||
|
try:
|
||||||
|
cur = conn.cursor()
|
||||||
|
if cid:
|
||||||
|
contract_id = cid
|
||||||
|
_log("upload_contract", f"reuse cid={cid}")
|
||||||
|
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]
|
||||||
|
_log("upload_contract", f"new cid={contract_id}")
|
||||||
|
|
||||||
# Сохранить base64 как текст (быстро)
|
|
||||||
cur.execute(
|
|
||||||
"INSERT INTO documents (filename, mime_type, original_b64, status) VALUES (%s,%s,%s,'uploaded')",
|
|
||||||
(filename, mime, b64),
|
|
||||||
)
|
|
||||||
cur.execute("SELECT id FROM documents WHERE filename=%s AND status='uploaded' ORDER BY created_at DESC LIMIT 1", (filename,))
|
|
||||||
row = cur.fetchone()
|
|
||||||
doc_id = row[0] if row else None
|
|
||||||
if doc_id:
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO supplements (contract_id, document_id, type) VALUES (%s,%s,'initial')",
|
"INSERT INTO documents (filename, mime_type, original_b64, status) VALUES (%s,%s,%s,'uploaded')",
|
||||||
(str(contract_id), str(doc_id)),
|
(filename, mime, b64),
|
||||||
)
|
)
|
||||||
conn.commit()
|
_log("upload_doc_insert", "ok")
|
||||||
cur.close()
|
|
||||||
|
cur.execute("SELECT id FROM documents WHERE filename=%s AND status='uploaded' ORDER BY created_at DESC LIMIT 1", (filename,))
|
||||||
|
row = cur.fetchone()
|
||||||
|
doc_id = row[0] if row else None
|
||||||
|
_log("upload_doc_select", f"doc_id={doc_id}")
|
||||||
|
|
||||||
|
if doc_id:
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO supplements (contract_id, document_id, type) VALUES (%s,%s,'initial')",
|
||||||
|
(str(contract_id), str(doc_id)),
|
||||||
|
)
|
||||||
|
_log("upload_suppl", "ok")
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
_log("upload_commit", "ok")
|
||||||
|
cur.close()
|
||||||
|
except Exception as e:
|
||||||
|
_log("upload_error", str(e))
|
||||||
|
try: conn.rollback()
|
||||||
|
except: pass
|
||||||
|
raise e
|
||||||
|
finally:
|
||||||
|
db.put_conn(conn)
|
||||||
|
_log("upload_putconn", "ok")
|
||||||
|
|
||||||
|
_log("upload_done", f"cid={contract_id} doc={doc_id}")
|
||||||
|
return jsonify({"contract_id": str(contract_id), "doc_id": str(doc_id) if doc_id else None})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
try: conn.rollback()
|
_log("upload_outer_error", str(e))
|
||||||
except: pass
|
return jsonify({"error": f"Крах: {e}"}), 500
|
||||||
raise e
|
|
||||||
finally:
|
|
||||||
db.put_conn(conn)
|
|
||||||
|
|
||||||
return jsonify({"contract_id": str(contract_id), "doc_id": str(doc_id) if doc_id else None})
|
|
||||||
|
|
||||||
# ── Чанковая загрузка ──────────────────────────────────────
|
# ── Чанковая загрузка ──────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user