forked from EduCraft/curriculum-project-hub
fix(filelib): 上传上限抬到 50MiB,并把它与 body limit 的串联写清
7.8MB 文件上传报 413:那是 Fastify 在 body 解析阶段拒的,不是 HUB_FILELIB_MAX_FILE_BYTES。上传把内容放在 JSON body 里、二进制过 base64 体积涨 4/3,所以有效上限是 min(该值, HUB_HTTP_BODY_LIMIT_BYTES × 3/4)。 原先 body limit 是 1MiB,10MiB 的文件上限根本不可达。 .env.example:body limit 1MiB → 70MiB,新增 HUB_FILELIB_MAX_FILE_BYTES=50MiB。 注意 body limit 同时是 ADR-0022 requestBodySize 维度的平台 ceiling,抬高它 对所有端点生效。 resolveMaxFileBytes 拆成 parseMaxFileBytes(纯解析)+ resolveMaxFileBytes(读 env):原先带默认参数,显式传 undefined 会回落到读 env,"没传值"与"读环境变量" 分不开,vitest 加载 .env 后测试会读到真实配置。
This commit is contained in:
@@ -9,26 +9,26 @@ import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
FILE_CONTENT_MAX_BYTES_DEFAULT,
|
||||
defaultCommitMessage,
|
||||
resolveMaxFileBytes,
|
||||
parseMaxFileBytes,
|
||||
} from "../../src/database/filelib/fileService.js";
|
||||
|
||||
describe("resolveMaxFileBytes", () => {
|
||||
describe("parseMaxFileBytes", () => {
|
||||
it("缺省 10MiB", () => {
|
||||
expect(FILE_CONTENT_MAX_BYTES_DEFAULT).toBe(10 * 1024 * 1024);
|
||||
expect(resolveMaxFileBytes(undefined)).toBe(FILE_CONTENT_MAX_BYTES_DEFAULT);
|
||||
expect(resolveMaxFileBytes("")).toBe(FILE_CONTENT_MAX_BYTES_DEFAULT);
|
||||
expect(resolveMaxFileBytes(" ")).toBe(FILE_CONTENT_MAX_BYTES_DEFAULT);
|
||||
expect(parseMaxFileBytes(undefined)).toBe(FILE_CONTENT_MAX_BYTES_DEFAULT);
|
||||
expect(parseMaxFileBytes("")).toBe(FILE_CONTENT_MAX_BYTES_DEFAULT);
|
||||
expect(parseMaxFileBytes(" ")).toBe(FILE_CONTENT_MAX_BYTES_DEFAULT);
|
||||
});
|
||||
|
||||
it("合法正整数生效(可上调也可下调)", () => {
|
||||
expect(resolveMaxFileBytes("1048576")).toBe(1024 * 1024);
|
||||
expect(resolveMaxFileBytes(" 52428800 ")).toBe(50 * 1024 * 1024);
|
||||
expect(resolveMaxFileBytes("1")).toBe(1);
|
||||
expect(parseMaxFileBytes("1048576")).toBe(1024 * 1024);
|
||||
expect(parseMaxFileBytes(" 52428800 ")).toBe(50 * 1024 * 1024);
|
||||
expect(parseMaxFileBytes("1")).toBe(1);
|
||||
});
|
||||
|
||||
it("非法值一律回退缺省,不产生 0 或负数上限", () => {
|
||||
for (const bad of ["0", "-1", "abc", "1.5", "NaN", "Infinity", "1e999", "10MB"]) {
|
||||
expect(resolveMaxFileBytes(bad)).toBe(FILE_CONTENT_MAX_BYTES_DEFAULT);
|
||||
expect(parseMaxFileBytes(bad)).toBe(FILE_CONTENT_MAX_BYTES_DEFAULT);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user