feat: 支持运行时动态配置文件大小限制

This commit is contained in:
wizardchen
2026-01-05 16:05:38 +08:00
committed by lyingbug
parent 1b0edccfc4
commit 20e7b36202
5 changed files with 39 additions and 8 deletions
+5 -6
View File
@@ -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;'"]
ENTRYPOINT ["/docker-entrypoint.sh"]
+15
View File
@@ -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;'
+1
View File
@@ -15,6 +15,7 @@
<link rel="apple-touch-icon" sizes="120x120" type="image/png" href="./public/favicon.ico"/>
</head>
<body>
<script src="/config.js"></script>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
+4
View File
@@ -0,0 +1,4 @@
// 运行时配置(本地开发默认值,Docker 环境会被 entrypoint 脚本覆盖)
window.__RUNTIME_CONFIG__ = {
MAX_FILE_SIZE_MB: 50
};
+14 -2
View File
@@ -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) {