# 后端构建脚本
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/eclipse-temurin:17-jre

ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8
ENV TZ=Asia/Shanghai
ENV JAVA_OPTS=""
ENV EASYFLOW_JAR_PATH=/app/artifacts/easyflow.jar
ENV EASYFLOW_CONFIG_PATH=file:/app/application.yml
ENV EASYFLOW_LOG_FILE=/app/logs/app.log
ENV EASYFLOW_JAR_RESTART_GRACE_SECONDS=30
ENV NPM_CONFIG_REGISTRY=https://registry.npmjs.org
ENV PIP_INDEX_URL=https://pypi.org/simple
ENV PYTHONPATH=/opt/easyflow/python-packages
ENV NODE_PATH=/app/node_modules

WORKDIR /app

ARG DEBIAN_FRONTEND=noninteractive

RUN useradd --system --create-home easyflow && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        gnupg && \
    mkdir -p /etc/apt/keyrings && \
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key -o /tmp/nodesource.gpg.key && \
    gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg /tmp/nodesource.gpg.key && \
    chmod 644 /etc/apt/keyrings/nodesource.gpg && \
    printf "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_24.x nodistro main\n" > /etc/apt/sources.list.d/nodesource.list && \
    rm -f /tmp/nodesource.gpg.key && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        coreutils \
        diffutils \
        file \
        findutils \
        fontconfig \
        fonts-liberation2 \
        fonts-noto-cjk \
        gawk \
        grep \
        gzip \
        inotify-tools \
        jq \
        libdigest-sha-perl \
        libreoffice-calc \
        libreoffice-impress \
        libreoffice-writer \
        nodejs \
        pandoc \
        poppler-utils \
        procps \
        python3 \
        python3-pip \
        python3-venv \
        qpdf \
        ripgrep \
        sed \
        tar \
        tree \
        unzip \
        util-linux \
        zip \
        tini && \
    rm -rf /var/lib/apt/lists/*

RUN mkdir -p /etc/pip "${PYTHONPATH}" /opt/easyflow/node-runtime && \
    ln -sf /usr/bin/python3 /usr/local/bin/python && \
    ln -sf /usr/bin/pip3 /usr/local/bin/pip && \
    npm config set registry "${NPM_CONFIG_REGISTRY}" && \
    printf "registry=%s\n" "${NPM_CONFIG_REGISTRY}" > /etc/npmrc && \
    npm install -g pnpm@10.17.1 && \
    pnpm config set registry "${NPM_CONFIG_REGISTRY}" && \
    printf "[global]\nindex-url = %s\n" "${PIP_INDEX_URL}" > /etc/pip.conf

RUN python3 -m pip install --no-cache-dir --target "${PYTHONPATH}" \
        python-docx==1.2.0 \
        python-pptx==1.0.2 \
        openpyxl==3.1.5 \
        xlsxwriter==3.2.9 \
        lxml==6.1.1 \
        defusedxml==0.7.1 \
        pillow==12.3.0 \
        pypdf==6.16.1 \
        pdfplumber==0.11.10 \
        pdf2image==1.17.0 \
        reportlab==5.0.0 \
        numpy==2.5.2 \
        pandas==3.0.5 \
        matplotlib==3.11.1 \
        seaborn==0.13.2 \
        pyyaml==6.0.3 \
        jsonschema==4.26.0 \
        jinja2==3.1.6 \
        beautifulsoup4==4.15.0 \
        pydantic==2.13.4 \
        python-dateutil==2.9.0.post0 \
        tabulate==0.10.0 \
        markdown==3.10.3 \
        charset-normalizer==3.5.1 \
        tenacity==9.1.4 && \
    PYTHONPATH="${PYTHONPATH}" python3 -c "import bs4, defusedxml, docx, jsonschema, lxml, matplotlib, numpy, openpyxl, pandas, pdfplumber, PIL, pptx, pydantic, pypdf, reportlab, seaborn, yaml"

RUN npm install --prefix /opt/easyflow/node-runtime --omit=dev --no-audit --no-fund --save-exact \
        docx@9.7.1 \
        pptxgenjs@4.0.1 \
        sharp@0.35.3 \
        pdf-lib@1.17.1 \
        pdfjs-dist@6.2.108 \
        zod@4.4.3 \
        ajv@8.20.0 \
        yaml@2.9.0 \
        csv-parse@7.0.2 \
        csv-stringify@6.8.3 \
        fast-xml-parser@5.10.1 \
        marked@18.0.9 \
        sanitize-html@2.17.7 \
        cheerio@1.2.0 \
        dayjs@1.11.21 \
        handlebars@4.7.9 \
        jszip@3.10.1 && \
    ln -s /opt/easyflow/node-runtime/node_modules /app/node_modules && \
    node -e "for (const name of ['docx','pptxgenjs','sharp','pdf-lib','pdfjs-dist/package.json','zod','ajv','yaml','csv-parse','csv-stringify','fast-xml-parser','marked','sanitize-html','cheerio','dayjs','handlebars','jszip']) require.resolve(name)" && \
    npm cache clean --force

RUN fc-cache -f && \
    mkdir -p /app/logs /app/artifacts /app/data && \
    chown -R easyflow:easyflow /app

COPY docker-soffice-wrapper.sh /usr/local/bin/soffice
COPY docker-entrypoint.sh /usr/local/bin/easyflow-entrypoint.sh
RUN chmod 755 /usr/local/bin/soffice /usr/local/bin/easyflow-entrypoint.sh

VOLUME ["/app/logs", "/app/data"]
EXPOSE 8111

ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/easyflow-entrypoint.sh"]
