fix(office): sanitize LibreOffice failures + resolve binary via shutil.which
Test / Tests (Python 3.12) (push) Canceled after 0s
Test / Tests (Python 3.12) (push) Canceled after 0s
This commit is contained in:
@@ -13,6 +13,7 @@ soffice first-run lock hang).
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -33,17 +34,22 @@ def _run_office(src: Path, work: Path) -> bytes:
|
|||||||
Satisfies: spec render RICH_RENDER.
|
Satisfies: spec render RICH_RENDER.
|
||||||
"""
|
"""
|
||||||
work.mkdir(parents=True, exist_ok=True)
|
work.mkdir(parents=True, exist_ok=True)
|
||||||
|
binary = shutil.which("libreoffice") or "/usr/bin/libreoffice"
|
||||||
try:
|
try:
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
["/usr/bin/libreoffice", "--headless", "--convert-to", "pdf",
|
[binary, "--headless", "--convert-to", "pdf",
|
||||||
"--outdir", str(work), str(src)],
|
"--outdir", str(work), str(src)],
|
||||||
check=True, timeout=LIBREOFFICE_TIMEOUT,
|
check=True, timeout=LIBREOFFICE_TIMEOUT,
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
)
|
)
|
||||||
except FileNotFoundError as exc:
|
except FileNotFoundError as exc:
|
||||||
raise ValueError(
|
raise ValueError("LibreOffice is not installed. Install it to enable Rich mode.") from exc
|
||||||
"LibreOffice is not installed. Install it to enable Rich mode."
|
except subprocess.CalledProcessError as exc:
|
||||||
) from exc
|
# Read the captured stderr (already on exc) into a clean, actionable message
|
||||||
|
detail = exc.stderr.decode(errors="replace").strip() or f"exit status {exc.returncode}"
|
||||||
|
raise ValueError(f"LibreOffice could not render the document ({detail})") from exc
|
||||||
|
except subprocess.TimeoutExpired as exc:
|
||||||
|
raise ValueError("LibreOffice timed out rendering the document (600s)") from exc
|
||||||
# LibreOffice names its output after the ON-DISK input basename (e.g. the
|
# LibreOffice names its output after the ON-DISK input basename (e.g. the
|
||||||
# web job path job-abc123.docx -> job-abc123.pdf), NOT the original upload
|
# web job path job-abc123.docx -> job-abc123.pdf), NOT the original upload
|
||||||
# name, so locate the PDF there rather than by the source stem.
|
# name, so locate the PDF there rather than by the source stem.
|
||||||
|
|||||||
Reference in New Issue
Block a user