v1.0.158: fix r.OK→r.ok (Python lowercase) + add /convert-doc endpoint

This commit is contained in:
2026-06-23 17:00:49 +04:00
parent ddc0a8b42d
commit a788e9e23d
2 changed files with 44 additions and 4 deletions
+41 -1
View File
@@ -3,7 +3,7 @@
from http.server import HTTPServer, BaseHTTPRequestHandler from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn from socketserver import ThreadingMixIn
from urllib.parse import urlparse, parse_qs from urllib.parse import urlparse, parse_qs
import json, re import json, re, os
# ── DB auto-seed ────────────────────────────────────────────────────────── # ── DB auto-seed ──────────────────────────────────────────────────────────
from db import prompts as db_prompts from db import prompts as db_prompts
@@ -54,6 +54,8 @@ class Handler(BaseHTTPRequestHandler):
self._handle_unzip() self._handle_unzip()
elif self.path == "/llm-ops": elif self.path == "/llm-ops":
self._handle_llm_ops() self._handle_llm_ops()
elif self.path == "/convert-doc":
self._handle_convert_doc()
else: else:
self.send_error(404) self.send_error(404)
@@ -136,6 +138,44 @@ class Handler(BaseHTTPRequestHandler):
except Exception as e: except Exception as e:
self._json({"ok": False, "error": str(e)}, 500) self._json({"ok": False, "error": str(e)}, 500)
# ── /convert-doc ──────────────────────────────────────────────────────
def _handle_convert_doc(self):
"""DOC → DOCX через LibreOffice."""
import subprocess, tempfile
length = int(self.headers.get("Content-Length", 0))
data = self.rfile.read(length)
with tempfile.NamedTemporaryFile(suffix=".doc", delete=False) as f:
f.write(data)
doc_path = f.name
tmpdir = tempfile.mkdtemp()
try:
subprocess.run(
["libreoffice", "--headless", "--convert-to", "docx", "--outdir", tmpdir, doc_path],
timeout=30, capture_output=True,
)
docx_files = [x for x in os.listdir(tmpdir) if x.endswith(".docx")]
if docx_files:
with open(os.path.join(tmpdir, docx_files[0]), "rb") as f:
result = f.read()
self.send_response(200)
self.send_header("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
self.send_header("Content-Length", str(len(result)))
self._send_cors()
self.end_headers()
self.wfile.write(result)
else:
self.send_error(500, "conversion produced no output")
except Exception as e:
self.send_error(500, str(e))
finally:
os.unlink(doc_path)
for x in os.listdir(tmpdir):
os.unlink(os.path.join(tmpdir, x))
os.rmdir(tmpdir)
# ── /llm-ops (debug) ────────────────────────────────────────────────── # ── /llm-ops (debug) ──────────────────────────────────────────────────
def _handle_llm_ops(self): def _handle_llm_ops(self):
+3 -3
View File
@@ -75,7 +75,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">Сверка договоров — LLM AI-driven Event Sourcing <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.0.157 — Lucee</span></span> <span class="title">Сверка договоров — LLM AI-driven Event Sourcing <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.0.158 — Lucee</span></span>
</div> </div>
<div class="content"> <div class="content">
@@ -435,8 +435,8 @@ function uploadFile(file, onProgress) {
xhr.onload = function() { xhr.onload = function() {
try { try {
var r = JSON.parse(xhr.responseText); var r = JSON.parse(xhr.responseText);
if (r.OK) resolve(r); if (r.ok) resolve(r);
else reject(new Error(r.ERROR || 'Неизвестная ошибка')); else reject(new Error(r.error || 'Неизвестная ошибка'));
} catch(e) { reject(new Error('Некорректный ответ')); } } catch(e) { reject(new Error('Некорректный ответ')); }
}; };
xhr.onerror = function() { reject(new Error('Сеть')); }; xhr.onerror = function() { reject(new Error('Сеть')); };