diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 73341a6fa..656d52ccd 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -7,10 +7,6 @@ WORKDIR /app ENV NODE_OPTIONS="--max-old-space-size=4096" ENV VITE_IS_DOCKER=true -# 文件大小限制(MB),通过构建参数传入 -ARG MAX_FILE_SIZE_MB=50 -ENV VITE_MAX_FILE_SIZE_MB=${MAX_FILE_SIZE_MB} - # 复制依赖文件 COPY package*.json ./ COPY packages/xlsx-0.20.2.tgz ./packages/xlsx-0.20.2.tgz @@ -34,11 +30,14 @@ COPY --from=build-stage /app/dist /usr/share/nginx/html # 复制nginx配置模板文件 COPY nginx.conf /etc/nginx/templates/default.conf.template +# 复制启动脚本 +COPY docker-entrypoint.sh /docker-entrypoint.sh +RUN chmod +x /docker-entrypoint.sh + # 设置默认环境变量(MB) ENV MAX_FILE_SIZE_MB=50 # 暴露端口 EXPOSE 80 -# 启动时将 MAX_FILE_SIZE_MB 转换为带单位的 MAX_FILE_SIZE,然后替换到 nginx 配置 -CMD ["/bin/sh", "-c", "export MAX_FILE_SIZE=${MAX_FILE_SIZE_MB}M && envsubst '${MAX_FILE_SIZE}' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"] \ No newline at end of file +ENTRYPOINT ["/docker-entrypoint.sh"] \ No newline at end of file diff --git a/frontend/docker-entrypoint.sh b/frontend/docker-entrypoint.sh new file mode 100644 index 000000000..8cd6c7598 --- /dev/null +++ b/frontend/docker-entrypoint.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +# 生成运行时配置文件,注入环境变量到前端 +cat > /usr/share/nginx/html/config.js << EOF +window.__RUNTIME_CONFIG__ = { + MAX_FILE_SIZE_MB: ${MAX_FILE_SIZE_MB:-50} +}; +EOF + +# 处理 nginx 配置 +export MAX_FILE_SIZE=${MAX_FILE_SIZE_MB}M +envsubst '${MAX_FILE_SIZE}' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf + +# 启动 nginx +exec nginx -g 'daemon off;' diff --git a/frontend/index.html b/frontend/index.html index e2ab8ef2b..f5538cebd 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -15,6 +15,7 @@ +
diff --git a/frontend/public/config.js b/frontend/public/config.js new file mode 100644 index 000000000..2861e2413 --- /dev/null +++ b/frontend/public/config.js @@ -0,0 +1,4 @@ +// 运行时配置(本地开发默认值,Docker 环境会被 entrypoint 脚本覆盖) +window.__RUNTIME_CONFIG__ = { + MAX_FILE_SIZE_MB: 50 +}; diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index db9389bc8..c6a36560f 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -1,7 +1,19 @@ import { MessagePlugin } from "tdesign-vue-next"; -// 从环境变量获取最大文件大小(MB),默认30MB -const MAX_FILE_SIZE_MB = Number(import.meta.env.VITE_MAX_FILE_SIZE_MB) || 50; +// 声明全局运行时配置类型 +declare global { + interface Window { + __RUNTIME_CONFIG__?: { + MAX_FILE_SIZE_MB?: number; + }; + } +} + +// 从运行时配置获取最大文件大小(MB),支持 Docker 环境动态配置 +// 优先级:运行时配置 > 构建时环境变量 > 默认值 50MB +const MAX_FILE_SIZE_MB = window.__RUNTIME_CONFIG__?.MAX_FILE_SIZE_MB + || Number(import.meta.env.VITE_MAX_FILE_SIZE_MB) + || 50; const MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024; export function generateRandomString(length: number) {