v0.0.30: .doc поддержка — accept + liberta API вместо LibreOffice
Deploy drhider / validate (push) Waiting to run

This commit is contained in:
2026-07-16 11:28:16 +04:00
parent c102d54887
commit 87a6df45c5
3 changed files with 26 additions and 32 deletions
+24 -30
View File
@@ -149,14 +149,14 @@ def pdf_to_markdown(content: bytes) -> str:
# ═══════════════════════════════════════════════════════════════════════════
# DOC → Markdown (через LibreOffice)
# DOC → Markdown (через сервис liberta)
# ═══════════════════════════════════════════════════════════════════════════
def doc_to_markdown(content: bytes) -> str:
"""Конвертировать бинарный .doc → .docx (LibreOffice) → Markdown.
"""Конвертировать бинарный .doc → .docx (сервис liberta) → Markdown.
Использует soffice --headless --convert-to docx, затем прогоняет
через штатный docx_to_markdown() с полным форматированием и таблицами.
Отправляет .doc на HTTP-сервис конвертации, получает .docx,
затем прогоняет через штатный docx_to_markdown().
Args:
content: Бинарное содержимое .doc файла
@@ -164,33 +164,27 @@ def doc_to_markdown(content: bytes) -> str:
Returns:
Строка в формате Markdown (или сообщение об ошибке)
"""
tmpdir = tempfile.mkdtemp()
doc_path = os.path.join(tmpdir, "input.doc")
import httpx
url = os.environ.get(
"CONVERT_SERVICE_URL",
"https://liberta.containerk8s.dev.nubes.ru/convert"
)
try:
with open(doc_path, 'wb') as f:
f.write(content)
result = subprocess.run(
["soffice", "--headless", "--convert-to", "docx",
"--outdir", tmpdir, doc_path],
timeout=60, capture_output=True
)
docx_path = os.path.join(tmpdir, "input.docx")
if os.path.exists(docx_path):
with open(docx_path, 'rb') as f:
return docx_to_markdown(f.read())
stderr = result.stderr.decode('utf-8', errors='replace').strip()
log.warning("LibreOffice conversion failed: %s", stderr)
return f"[DOC — LibreOffice conversion failed: {stderr}]"
except FileNotFoundError:
return "[DOC — LibreOffice not installed. Install: apt install libreoffice-writer]"
except subprocess.TimeoutExpired:
return "[DOC — conversion timed out (>60s)]"
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
with httpx.Client(timeout=120) as client:
resp = client.post(
url,
files={"file": ("input.doc", content, "application/msword")}
)
if resp.status_code == 200:
return docx_to_markdown(resp.content)
err = resp.json().get("error", f"HTTP {resp.status_code}")
log.warning("liberta conversion failed: %s", err)
return f"[DOC — conversion failed: {err}]"
except httpx.TimeoutException:
return "[DOC — conversion timed out (>120s)]"
except Exception as e:
log.warning("liberta error: %s", e)
return f"[DOC — conversion error: {e}]"
# ═══════════════════════════════════════════════════════════════════════════
+1 -1
View File
@@ -20,7 +20,7 @@ if _sys_path_root not in sys.path:
sys.path.insert(0, _sys_path_root)
# Версия приложения (меняется при изменениях)
VERSION = "0.0.29"
VERSION = "0.0.30"
def create_app():
+1 -1
View File
@@ -101,7 +101,7 @@
CSV с таблицей замен скачивается отдельно.
</p>
<div class="file-input-wrap">
<input type="file" id="fileInput" multiple accept=".docx,.pdf,.txt,.zip">
<input type="file" id="fileInput" multiple accept=".doc,.docx,.pdf,.txt,.zip">
</div>
<div class="table-wrap">
<table>