Commit f57a50d9 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

fix

parent b3f8b0b7
Loading
Loading
Loading
Loading
Loading
+27 −39
Original line number Diff line number Diff line
@@ -135,22 +135,17 @@ def refresh_docx_fields(input_path: str, image: str = "docx-field-refresh") -> s
    if not input_path.exists() or input_path.suffix.lower() != ".docx":
        raise FileNotFoundError(f"Invalid DOCX path: {input_path}")

    with tempfile.TemporaryDirectory() as tmpdir:
        tmpdir_path = Path(tmpdir)
        temp_input = tmpdir_path / input_path.name
        shutil.copy2(input_path, temp_input)
    
    # Create a temporary home directory for LibreOffice to prevent creating files in /root
        lo_home = tmpdir_path / "lo_home"
    lo_home = "lo_home"
    lo_home.mkdir(mode=0o755, exist_ok=True)

    # Run LibreOffice container on this specific file
    # Get the relative path of the copied file within the temp directory
        file_path_in_container = f"/data/{temp_input.relative_to(tmpdir_path)}"
    
    cmd = [
        "docker", "run", "--rm",
            "-v", f"{tmpdir_path}:/data",
        "-v", f"{input_path.parent}:/data",
        "-v", f"{lo_home}:/tmp/lo_home",  # Mount temp home directory
        "-u", f"{os.getuid()}:{os.getgid()}",  # <--- run as host user
        "-e", "HOME=/tmp/lo_home",  # Tell LibreOffice to use temp directory for its profile
@@ -162,19 +157,12 @@ def refresh_docx_fields(input_path: str, image: str = "docx-field-refresh") -> s
        "--convert-to", "docx",
        "--infilter=writer8",
        "--outdir", "/data",
            file_path_in_container,  # Use the path relative to /data in container
        f"/data/{input_path}",  # Use the path relative to /data in container
    ]

    print(f"Running command:\n{' '.join(cmd)}\n")
    subprocess.run(cmd, check=True)

        generated = tmpdir_path / f"{input_path.stem}.docx"
        if not generated.exists():
            raise RuntimeError("LibreOffice did not produce an output file.")

        # Overwrite the original file
        shutil.copy2(generated, input_path)

    print(f"✅ Refreshed DOCX updated in place: {input_path}")
    return str(input_path)