From 5d8bcd61ea9b77bab45e98c301f08badb360f256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Thu, 16 Jul 2026 10:18:17 +0400 Subject: [PATCH] =?UTF-8?q?v2.0.5:=20=D0=B8=D0=BD=D1=82=D0=B5=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D1=81=20convert-service=20(HTTP=20?= =?UTF-8?q?=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20subprocess=20libreoffice?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/config.py | 1 + site/routes/upload_bp.py | 47 ++++++++++++++++------------------------ 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/site/config.py b/site/config.py index 4a249cd..de1712e 100644 --- a/site/config.py +++ b/site/config.py @@ -9,3 +9,4 @@ LLM_MODEL = os.getenv("LLM_MODEL", "gpt-oss-120b") MAX_CONTENT_LENGTH = 200 * 1024 * 1024 # 200 MB 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") diff --git a/site/routes/upload_bp.py b/site/routes/upload_bp.py index 6e954d5..03dad1d 100644 --- a/site/routes/upload_bp.py +++ b/site/routes/upload_bp.py @@ -1,9 +1,10 @@ """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 services.parse import parse_file from db import documents -from config import MAX_CONTENT_LENGTH +import config upload_bp = Blueprint("upload", __name__) @@ -70,38 +71,28 @@ def upload(): @upload_bp.route("/convert-doc", methods=["POST"]) def convert_doc(): - """.doc → .docx через libreoffice (без сохранения на диск).""" + """.doc → .docx через внешний libreoffice-сервис (HTTP).""" f = request.files.get("files") if not f: return jsonify(ok=False, error="no file"), 400 - data = f.read() - doc_path = None - tmpdir = None try: - with tempfile.NamedTemporaryFile(suffix=".doc", delete=False) as tmp: - tmp.write(data) - doc_path = tmp.name - tmpdir = tempfile.mkdtemp() - subprocess.run( - ["libreoffice", "--headless", "--convert-to", "docx", "--outdir", tmpdir, doc_path], - timeout=30, capture_output=True, + resp = httpx.post( + config.CONVERT_SERVICE_URL + "/convert", + files={"file": (f.filename, f.read(), f.content_type or "application/msword")}, + timeout=120, ) - 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 out: - return send_file( - io.BytesIO(out.read()), - mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document", - ) - return jsonify(ok=False, error="conversion produced no output"), 500 - finally: - if doc_path and os.path.exists(doc_path): - os.unlink(doc_path) - if tmpdir and os.path.exists(tmpdir): - for x in os.listdir(tmpdir): - os.unlink(os.path.join(tmpdir, x)) - os.rmdir(tmpdir) + if resp.status_code == 200: + return send_file( + io.BytesIO(resp.content), + mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ) + data = resp.json() + return jsonify(ok=False, error=data.get("error", "conversion failed")), 500 + except httpx.TimeoutException: + return jsonify(ok=False, error="conversion timeout"), 500 + except Exception as e: + return jsonify(ok=False, error=str(e)), 500 @upload_bp.route("/unzip-upload", methods=["POST"])