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 socketserver import ThreadingMixIn
from urllib.parse import urlparse, parse_qs
import json, re
import json, re, os
# ── DB auto-seed ──────────────────────────────────────────────────────────
from db import prompts as db_prompts
@@ -54,6 +54,8 @@ class Handler(BaseHTTPRequestHandler):
self._handle_unzip()
elif self.path == "/llm-ops":
self._handle_llm_ops()
elif self.path == "/convert-doc":
self._handle_convert_doc()
else:
self.send_error(404)
@@ -136,6 +138,44 @@ class Handler(BaseHTTPRequestHandler):
except Exception as e:
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) ──────────────────────────────────────────────────
def _handle_llm_ops(self):