/admin-api/projects/{projectCode}/uploads/commit

用户端和管理端表单项上限设为 1003,支持每批 500 个文件。
数量超限单独返回 UPLOAD_TOO_MANY_PARTS,补充排查日志。
This commit is contained in:
lxp
2026-10-27 13:44:36 +08:00
parent 01aa13935a
commit 27f5849657
7 changed files with 198 additions and 1 deletions
+3
View File
@@ -638,6 +638,8 @@ interface CommitInput {
单文件最多 **10 MiB**,一批文件合计最多 **50 MiB**;1 MiB = 1,048,576 字节。同名路径会更新已有文件。上传目录不自动解压 ZIP,超限时让用户减少文件或分批提交,每批使用上次返回的新 SHA。
multipart 整个请求最多 **52 MiB**,表单项上限为 **1003**(500 个 files、500 个 paths、3 个公共字段)。不要为每个文件额外添加其他表单项。表单项数量超限返回 413 `UPLOAD_TOO_MANY_PARTS`,大小超限返回 `UPLOAD_TOO_LARGE` 或 `FILE_TOO_LARGE`。
```ts
interface SelectedFile {
file: File;
@@ -1066,6 +1068,7 @@ async function watchImport(
| 409 CLEANUP_REPOSITORY_UNSAFE / CLEANUP_REPOSITORY_REPLACED | 残留仓库核验不通过,停止清理;任务中的同名 errorCode 也按此提示 |
| REMOTE_RESULT_UNKNOWN(状态码以响应为准) | 写入结果待核对,停止重复提交,展示 requestId |
| 413 FILE_TOO_LARGE / UPLOAD_TOO_LARGE / URDF_UPLOAD_TOO_LARGE | 检查对应上传类型限额,拆批或减少文件 |
| 413 UPLOAD_TOO_MANY_PARTS | 文件或表单项数量超限,减少文件并检查重复添加的字段 |
| 429 ADMIN_LOGIN_LIMITED | 提示 15 分钟后重试,不循环发登录请求 |
| 503 OSS_UNAVAILABLE | 暂停上传入口并提示当前环境未开放上传 |
| 503 PROJECT_LOCK_UNAVAILABLE / PROJECT_LOCK_INTERRUPTED | 暂停写入并刷新,必要时联系后端核对 |
+2
View File
@@ -139,6 +139,8 @@ Redis 只监听内网或回环地址,启用认证;跨不可信网络使用 T
JSON 上限为实际线上请求字节数,包含转义和结构开销;业务单文件/总内容上限继续保留。Content-Length 和 chunked 均流式核验,超限返回 413 REQUEST_TOO_LARGE。multipart 仍为单文件 10MB、请求 52MB,原有 UPLOAD_TOO_LARGE/FILE_TOO_LARGE 错误保留;URDF 直传 OSS 不占用这类 multipart 名额。文件名额不足为 429 FILE_BUSY,不排队;Fork 繁忙仍为 FORK_BUSY。
API 和管理端均显式配置 `server.tomcat.max-part-count=1003`,允许 500 个 files、500 个 paths 及 3 个公共字段。Tomcat 默认的 50 项无法承载业务允许的批量上传;数量超限会被 Spring 包装成上传大小异常,现区分为 413 `UPLOAD_TOO_MANY_PARTS` 并记录安全诊断日志。部署时若覆盖该配置,需要与业务批量文件数对齐。
Gitea 原始文件有界读取,contents JSON 上限按单文件 Base64 膨胀量加 2 MiB 元信息计算,其他元数据 8 MiB,错误响应 64 KiB。GET 超限返回 502 GITEA_RESPONSE_TOO_LARGE;写入响应无法完整确认仍为 REMOTE_RESULT_UNKNOWN,持久化阻断后续仓库写入,不自动重试。
### 依赖安全核对(2026-10-22)
+2
View File
@@ -866,6 +866,7 @@ entries 仅包含当前目录的直接子项,目录在前,名称升序。展
| 本次上传文件或提交内容总量 | 50 MiB,52428800 字节 |
| multipart 整个请求 | 52 MiB,包含传输开销 |
| 单批变更或上传文件数量 | 500 |
| multipart 表单项数量 | 1003,含 500 个 files、500 个 paths 和 3 个公共字段 |
大小按字节计算,文本使用 UTF-8 编码长度,不使用 JavaScript 字符串 length 计算文件体积。以上是当前默认配置,超限返回 413。后端调整业务上传大小时还需同步调整 multipart 配置。
@@ -1262,6 +1263,7 @@ if (result.status !== 'CHAT') {
| 409 | REMOTE_RESULT_UNKNOWN | 上一次仓库写入结果待核对,暂不能继续该项目的仓库写入 |
| 413 | FILE_TOO_LARGE | 文件读取、编辑或上传内容超限,提示大小限制 |
| 413 | UPLOAD_TOO_LARGE | 整体 multipart 请求超限 |
| 413 | UPLOAD_TOO_MANY_PARTS | multipart 文件或表单项数量超限,减少文件或重复字段 |
| 415 | UNSUPPORTED_MEDIA_TYPE | 检查 Content-Type,JSON 与 FormData 不要混用 |
| 500 | CAPTCHA_ERROR | 验证码生成失败,可重新获取并记录 requestId |
| 500 | PROJECT_INITIALIZATION_ERROR | 项目初始化或回写未完成,查看 data.projectCode 和工作区,请后端核对 |
@@ -1,5 +1,8 @@
server:
port: ${ADMIN_SERVER_PORT:8994}
tomcat:
# 每批 500 个文件 + 500 条 paths + expectedCommitSha、message、directory。
max-part-count: 1003
spring:
application:
name: vibemake-adminapi
@@ -1,5 +1,8 @@
server:
port: ${SERVER_PORT:${PORT:8993}}
tomcat:
# 每批 500 个文件 + 500 条 paths + expectedCommitSha、message、directory。
max-part-count: 1003
spring:
application:
name: vibemake-api
@@ -1,7 +1,9 @@
package com.linkerbot.vibemake.common;
import jakarta.validation.ConstraintViolationException;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.tomcat.util.http.fileupload.impl.FileCountLimitExceededException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
@@ -19,6 +21,9 @@ import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import java.util.ArrayList;
import java.util.List;
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@@ -51,7 +56,23 @@ public class GlobalExceptionHandler {
}
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<ApiResponse<Object>> oversized(Exception error) {
public ResponseEntity<ApiResponse<Object>> oversized(
MaxUploadSizeExceededException error, HttpServletRequest request) {
// Spring 也将 Tomcat 的表单项数量超限包装成大小异常,必须按原因区分。
List<String> causes = new ArrayList<>();
FileCountLimitExceededException countLimit = null;
for (Throwable cause = error;
cause != null && causes.size() < 10;
cause = cause.getCause()) {
causes.add(cause.getClass().getName());
if (cause instanceof FileCountLimitExceededException exceeded) countLimit = exceeded;
}
log.warn("multipart 上传被拒绝 method={} path={} contentLength={} maxParts={} errorTypes={}",
request.getMethod(), request.getRequestURI(), request.getContentLengthLong(),
countLimit == null ? null : countLimit.getLimit(), causes);
if (countLimit != null)
return business(new BusinessException(413, "UPLOAD_TOO_MANY_PARTS",
"上传文件或表单项数量超过限制,请减少文件后重试"));
return business(new BusinessException(413, "UPLOAD_TOO_LARGE", "上传超过大小限制"));
}
@@ -0,0 +1,163 @@
package com.linkerbot.vibemake.common;
import static org.assertj.core.api.Assertions.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
/** 使用真实 Tomcat 解析 multipart;MockMvc 会绕过容器的表单项数量和大小限制。 */
class MultipartUploadTest {
private static final String BOUNDARY = "vibemake-upload-test";
private static final ObjectMapper JSON = new ObjectMapper();
@TempDir Path temporaryDirectory;
@ParameterizedTest
@ValueSource(strings = {"vibemake-api", "vibemake-adminapi"})
void applicationConfigurationAccepts34And500SmallFiles(String module) throws Exception {
try (ServletWebServerApplicationContext context = start(module)) {
assertAccepted(upload(context, module, 34, 3000), 34, 102000);
assertAccepted(upload(context, module, 500, 1), 500, 500);
}
}
@ParameterizedTest
@ValueSource(strings = {"vibemake-api", "vibemake-adminapi"})
void excessivePartsAndOversizedFileHaveDifferentErrors(String module) throws Exception {
try (ServletWebServerApplicationContext context = start(module)) {
assertRejected(upload(context, module, 501, 1), "UPLOAD_TOO_MANY_PARTS");
assertRejected(upload(context, module, 1, 10 * 1024 * 1024 + 1), "UPLOAD_TOO_LARGE");
}
}
@Test
void default50PartLimitReproducesFailureWith34SmallFiles() throws Exception {
try (ServletWebServerApplicationContext context =
start("vibemake-adminapi", "--server.tomcat.max-part-count=50")) {
assertRejected(upload(context, "vibemake-adminapi", 34, 3000), "UPLOAD_TOO_MANY_PARTS");
}
}
private ServletWebServerApplicationContext start(String module, String... overrides) {
String configuration =
Path.of("..", module, "src/main/resources/application.yml")
.toAbsolutePath().normalize().toUri().toString();
List<String> arguments = new java.util.ArrayList<>(List.of(
"--spring.config.location=" + configuration,
"--server.address=127.0.0.1",
"--server.port=0",
"--spring.main.banner-mode=off",
"--logging.level.root=WARN",
"--logging.level.org.springframework=WARN",
"--logging.level.com.linkerbot.vibemake=WARN",
"--logging.file.path=" + temporaryDirectory));
arguments.addAll(List.of(overrides));
return (ServletWebServerApplicationContext)
new SpringApplicationBuilder(UploadApplication.class)
.run(arguments.toArray(String[]::new));
}
private HttpResponse<String> upload(
ServletWebServerApplicationContext context, String module, int files, int fileBytes)
throws Exception {
ByteArrayOutputStream body = new ByteArrayOutputStream();
field(body, "expectedCommitSha", "a".repeat(40));
field(body, "message", "upload test");
field(body, "directory", "");
byte[] content = "x".repeat(fileBytes).getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < files; i++) {
write(body, "--" + BOUNDARY + "\r\n"
+ "Content-Disposition: form-data; name=\"files\"; filename=\"file-" + i + ".txt\"\r\n"
+ "Content-Type: application/octet-stream\r\n\r\n");
body.write(content);
write(body, "\r\n");
field(body, "paths", "folder/file-" + i + ".txt");
}
write(body, "--" + BOUNDARY + "--\r\n");
String prefix = module.equals("vibemake-adminapi") ? "/admin-api" : "/api/workspace";
URI uri = URI.create("http://127.0.0.1:" + context.getWebServer().getPort()
+ prefix + "/projects/4kqb0cs3b6kk/uploads/commit");
HttpRequest request = HttpRequest.newBuilder(uri)
.timeout(Duration.ofSeconds(15))
.header("Content-Type", "multipart/form-data; boundary=" + BOUNDARY)
.POST(HttpRequest.BodyPublishers.ofByteArray(body.toByteArray()))
.build();
try (HttpClient client = HttpClient.newHttpClient()) {
return client.send(request, HttpResponse.BodyHandlers.ofString());
}
}
private void field(ByteArrayOutputStream body, String name, String value) throws IOException {
write(body, "--" + BOUNDARY + "\r\n"
+ "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n" + value + "\r\n");
}
private void write(ByteArrayOutputStream body, String value) throws IOException {
body.write(value.getBytes(StandardCharsets.UTF_8));
}
private void assertAccepted(HttpResponse<String> response, int files, int bytes) throws Exception {
assertThat(response.statusCode()).as(response.body()).isEqualTo(200);
JsonNode result = JSON.readTree(response.body());
assertThat(result.path("files").asInt()).isEqualTo(files);
assertThat(result.path("paths").asInt()).isEqualTo(files);
assertThat(result.path("bytes").asLong()).isEqualTo(bytes);
}
private void assertRejected(HttpResponse<String> response, String code) throws Exception {
assertThat(response.statusCode()).as(response.body()).isEqualTo(413);
assertThat(JSON.readTree(response.body()).path("code").asText()).isEqualTo(code);
assertThat(response.headers().firstValue("X-Request-Id")).isPresent();
assertThat(response.headers().firstValue("Cache-Control")).contains("no-store");
}
@TestConfiguration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import({UploadController.class, GlobalExceptionHandler.class, RequestIdFilter.class})
static class UploadApplication {}
@RestController
@TestComponent
static class UploadController {
@PostMapping({
"/admin-api/projects/{projectCode}/uploads/commit",
"/api/workspace/projects/{projectCode}/uploads/commit"
})
UploadResult upload(
@RequestParam String expectedCommitSha,
@RequestParam String message,
@RequestParam String directory,
@RequestParam("files") List<MultipartFile> files,
@RequestParam List<String> paths) {
return new UploadResult(files.size(), paths.size(),
files.stream().mapToLong(MultipartFile::getSize).sum());
}
}
record UploadResult(int files, int paths, long bytes) {}
}