# PDF & Pixel converter: Ghostscript, qpdf, LibreOffice and pdf2docx behind a
# tiny Node HTTP API. Licence: AGPL-3.0 (see LICENSE, NOTICE.md); the running
# converter serves this folder's source at /source.
# Local:  npm run docker:up  (compose service "converter", http://localhost:8081)
# Node 24 runs the TypeScript in src/ directly, so there is no build step and
# no npm install (the converter has no npm dependencies).
# Debian 13 (trixie): Python 3.13 for pdf2docx's pinned packages.
FROM node:24-trixie-slim

# LibreOffice without its desktop parts, plus fonts that stand in for the ones
# Office documents use: Liberation (Arial, Times New Roman, Courier New),
# Carlito (Calibri), Caladea (Cambria), DejaVu, and Noto for other languages.
RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      ghostscript qpdf python3 python3-venv \
      libreoffice-writer-nogui libreoffice-calc-nogui libreoffice-impress-nogui \
      fonts-liberation2 fonts-crosextra-carlito fonts-crosextra-caladea \
      fonts-dejavu-core fonts-noto-core \
 && rm -rf /var/lib/apt/lists/*

# PDF → Word: pdf2docx and its exact dependencies in their own Python.
COPY py/requirements.txt /tmp/requirements.txt
RUN python3 -m venv /opt/pdf2docx \
 && /opt/pdf2docx/bin/pip install --no-cache-dir -r /tmp/requirements.txt \
 && rm /tmp/requirements.txt

# On AWS Lambda the same image runs behind the Lambda Web Adapter, which turns
# Lambda calls into ordinary HTTP requests to port 8080 (it does nothing when
# the container runs anywhere else). https://github.com/awslabs/aws-lambda-web-adapter
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.9.1 /lambda-adapter /opt/extensions/lambda-adapter

WORKDIR /app
# Readable by the non-root user (files from the exFAT drive are owner-only).
COPY --chmod=755 package.json tsconfig.json Dockerfile LICENSE NOTICE.md ./
COPY --chmod=755 src ./src
COPY --chmod=755 py ./py

ENV NODE_ENV=production \
    PORT=8080 \
    TMPDIR=/tmp \
    PDF2DOCX_PYTHON=/opt/pdf2docx/bin/python \
    AWS_LWA_READINESS_CHECK_PATH=/health
USER node
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s \
  CMD node -e "fetch('http://127.0.0.1:8080/health').then(r => process.exit(r.ok ? 0 : 1), () => process.exit(1))"
CMD ["node", "--disable-warning=ExperimentalWarning", "src/main.ts"]
