v2.0.5: интеграция с convert-service (HTTP вместо subprocess libreoffice)
Deploy contracts-flask / validate (push) Successful in 0s
Deploy contracts-flask / validate (push) Successful in 0s
This commit is contained in:
@@ -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")
|
||||||
|
|||||||
+16
-25
@@ -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:
|
|
||||||
with open(os.path.join(tmpdir, docx_files[0]), "rb") as out:
|
|
||||||
return send_file(
|
return send_file(
|
||||||
io.BytesIO(out.read()),
|
io.BytesIO(resp.content),
|
||||||
mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||||
)
|
)
|
||||||
return jsonify(ok=False, error="conversion produced no output"), 500
|
data = resp.json()
|
||||||
finally:
|
return jsonify(ok=False, error=data.get("error", "conversion failed")), 500
|
||||||
if doc_path and os.path.exists(doc_path):
|
except httpx.TimeoutException:
|
||||||
os.unlink(doc_path)
|
return jsonify(ok=False, error="conversion timeout"), 500
|
||||||
if tmpdir and os.path.exists(tmpdir):
|
except Exception as e:
|
||||||
for x in os.listdir(tmpdir):
|
return jsonify(ok=False, error=str(e)), 500
|
||||||
os.unlink(os.path.join(tmpdir, x))
|
|
||||||
os.rmdir(tmpdir)
|
|
||||||
|
|
||||||
|
|
||||||
@upload_bp.route("/unzip-upload", methods=["POST"])
|
@upload_bp.route("/unzip-upload", methods=["POST"])
|
||||||
|
|||||||
Reference in New Issue
Block a user