25 lines
885 B
Python
Executable File
25 lines
885 B
Python
Executable File
#!/usr/bin/env python3
|
|
"""Конвертер .doc → .docx через libreoffice. Принимает POST с файлом, возвращает docx."""
|
|
import subprocess, tempfile, os, sys
|
|
|
|
data = sys.stdin.buffer.read()
|
|
with tempfile.NamedTemporaryFile(suffix=".doc", delete=False) as f:
|
|
f.write(data)
|
|
doc_path = f.name
|
|
|
|
tmpdir = tempfile.mkdtemp()
|
|
try:
|
|
subprocess.run(["libreoffice", "--headless", "--convert-to", "docx", "--outdir", tmpdir, doc_path],
|
|
timeout=30, capture_output=True)
|
|
docx = [x for x in os.listdir(tmpdir) if x.endswith(".docx")]
|
|
if docx:
|
|
with open(os.path.join(tmpdir, docx[0]), "rb") as f:
|
|
sys.stdout.buffer.write(f.read())
|
|
else:
|
|
sys.stdout.buffer.write(b"")
|
|
finally:
|
|
os.unlink(doc_path)
|
|
for x in os.listdir(tmpdir):
|
|
os.unlink(os.path.join(tmpdir, x))
|
|
os.rmdir(tmpdir)
|