v2.0.5: интеграция с convert-service (HTTP вместо subprocess libreoffice)
Deploy contracts-flask / validate (push) Successful in 0s

This commit is contained in:
2026-07-16 10:18:17 +04:00
parent 6334ca2e3e
commit 5d8bcd61ea
2 changed files with 20 additions and 28 deletions
+1
View File
@@ -9,3 +9,4 @@ LLM_MODEL = os.getenv("LLM_MODEL", "gpt-oss-120b")
MAX_CONTENT_LENGTH = 200 * 1024 * 1024 # 200 MB MAX_CONTENT_LENGTH = 200 * 1024 * 1024 # 200 MB
API_KEY = os.getenv("API_KEY", "") API_KEY = os.getenv("API_KEY", "")
CONVERT_SERVICE_URL = os.getenv("CONVERT_SERVICE_URL", "http://containerk8s.df36c8af-1a95-4623-b551-0d37b731ccca.svc.cluster.local:5000")
+19 -28
View File
@@ -1,9 +1,10 @@
"""Upload blueprint — загрузка, конвертация, распаковка.""" """Upload blueprint — загрузка, конвертация, распаковка."""
import io, os, base64, hashlib, zipfile, tempfile, subprocess import io, os, base64, hashlib, zipfile
import httpx
from flask import Blueprint, request, jsonify, send_file from flask import Blueprint, request, jsonify, send_file
from services.parse import parse_file from services.parse import parse_file
from db import documents from db import documents
from config import MAX_CONTENT_LENGTH import config
upload_bp = Blueprint("upload", __name__) upload_bp = Blueprint("upload", __name__)
@@ -70,38 +71,28 @@ def upload():
@upload_bp.route("/convert-doc", methods=["POST"]) @upload_bp.route("/convert-doc", methods=["POST"])
def convert_doc(): def convert_doc():
""".doc → .docx через libreoffice (без сохранения на диск).""" """.doc → .docx через внешний libreoffice-сервис (HTTP)."""
f = request.files.get("files") f = request.files.get("files")
if not f: if not f:
return jsonify(ok=False, error="no file"), 400 return jsonify(ok=False, error="no file"), 400
data = f.read()
doc_path = None
tmpdir = None
try: try:
with tempfile.NamedTemporaryFile(suffix=".doc", delete=False) as tmp: resp = httpx.post(
tmp.write(data) config.CONVERT_SERVICE_URL + "/convert",
doc_path = tmp.name files={"file": (f.filename, f.read(), f.content_type or "application/msword")},
tmpdir = tempfile.mkdtemp() timeout=120,
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 resp.status_code == 200:
if docx_files: return send_file(
with open(os.path.join(tmpdir, docx_files[0]), "rb") as out: io.BytesIO(resp.content),
return send_file( mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
io.BytesIO(out.read()), )
mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document", data = resp.json()
) return jsonify(ok=False, error=data.get("error", "conversion failed")), 500
return jsonify(ok=False, error="conversion produced no output"), 500 except httpx.TimeoutException:
finally: return jsonify(ok=False, error="conversion timeout"), 500
if doc_path and os.path.exists(doc_path): except Exception as e:
os.unlink(doc_path) return jsonify(ok=False, error=str(e)), 500
if tmpdir and os.path.exists(tmpdir):
for x in os.listdir(tmpdir):
os.unlink(os.path.join(tmpdir, x))
os.rmdir(tmpdir)
@upload_bp.route("/unzip-upload", methods=["POST"]) @upload_bp.route("/unzip-upload", methods=["POST"])