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
+19 -28
View File
@@ -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"])